11// search bar
22
3- document . querySelector ( 'form.search-form input' ) . addEventListener ( 'input' , function ( e ) { // If a change in the search bar is detected, run the function
4-
3+ document . querySelector ( 'form.search-form input' ) . addEventListener ( 'input' , function ( e ) { // If a change in the search bar is detected, run the function
4+ search ( e . target . value ) ;
5+ } ) ;
6+
7+ function search ( e ) {
58 var input , filter ; // Inizialize variables
6- input = e . target . value ; // Get the value of the search bar
7- filter = input . toUpperCase ( ) ; // Convert the value to uppercase
9+ filter = e . toUpperCase ( ) ; // Convert the value to uppercase
810
911 document . querySelector ( "#courses-list .courses__container" ) . innerHTML = "" ; // Clear results
1012
11- if ( input ) { // If the input is not empty
12- for ( let i = 0 ; i < COURSES . length ; i ++ ) { // Loop through all courses
13- if ( COURSES [ i ] . title . toUpperCase ( ) . includes ( filter ) ) { // If the course title includes the value of the search bar
13+ if ( e ) { // If the input is not empty
14+ for ( let i = 0 ; i < COURSES . length ; i ++ ) { // Loop through all courses
15+ if ( COURSES [ i ] . title . toUpperCase ( ) . includes ( filter ) ) { // If the course title includes the value of the search bar
1416 productItems ( '#courses-list .courses__container' , COURSES [ i ] ) ; // Show the product
1517
1618 document . querySelector ( '#no_course' ) . classList . add ( 'hidden' ) ; // Hide the no results message
17- } else if ( ! document . querySelector ( "#courses-list .courses__container" ) . innerHTML ) { // Otherwise, if there are no results
19+ } else if ( ! document . querySelector ( "#courses-list .courses__container" ) . innerHTML ) { // Otherwise, if there are no results
1820 document . querySelector ( '#no_course' ) . classList . remove ( 'hidden' ) ; // Show the no results message
1921 }
20- }
22+ }
2123 } else { // Otherwise, if the input is empty
2224 showAllProducts ( '.courses__container' , COURSES ) ; // Show all products
2325 }
24- } ) ;
26+ }
2527
2628function filtre ( type ) { // Search filter function
27- if ( type === "noteCroissant" ) { // If the filter is note croissant
29+ if ( type === "noteCroissant" ) { // If the filter is note croissant
2830 COURSES . sort ( ( a , b ) => a . mark - b . mark ) // Sort the courses by note
29- } else if ( type === "noteDecroissant" ) { // If the filter is note decroissant
31+ } else if ( type === "noteDecroissant" ) { // If the filter is note decroissant
3032 COURSES . sort ( ( a , b ) => b . mark - a . mark ) // Sort the courses by note
31- } else if ( type === "prixCroissant" ) { // If the filter is prix croissant
33+ } else if ( type === "prixCroissant" ) { // If the filter is prix croissant
3234 COURSES . sort ( ( a , b ) => a . price - b . price ) // Sort the courses by price
33- } else if ( type === "prixDecroissant" ) { // If the filter is prix decroissant
35+ } else if ( type === "prixDecroissant" ) { // If the filter is prix decroissant
3436 COURSES . sort ( ( a , b ) => b . price - a . price ) // Sort the courses by price
3537 }
3638
37- document . querySelector ( '.courses__container' ) . innerHTML = "" ; // Clear products
38- showAllProducts ( '.courses__container' , COURSES ) ; // Show all products
39- }
39+ if ( document . querySelector ( 'form.search-form input' ) . value ) {
40+ search ( document . querySelector ( 'form.search-form input' ) . value ) ;
41+ } else {
42+ document . querySelector ( '.courses__container' ) . innerHTML = "" ; // Clear products
43+ showAllProducts ( '.courses__container' , COURSES ) ; // Show all products
44+ }
45+ }
0 commit comments