@@ -3,6 +3,10 @@ import { useSelector, useDispatch } from 'react-redux'
3
3
import { useTranslation } from 'react-i18next'
4
4
import { change } from 'redux-form' // tslint:disable
5
5
import get from 'lodash/get'
6
+ import includes from 'lodash/includes'
7
+ import some from 'lodash/some'
8
+ import keys from 'lodash/keys'
9
+ import isEmpty from 'lodash/isEmpty'
6
10
import toNumber from 'lodash/toNumber'
7
11
import {
8
12
SearchForm ,
@@ -18,40 +22,113 @@ import Creators from '../../store/listing/creators'
18
22
import styles from './search.module.scss'
19
23
import { useRouter } from 'next/router'
20
24
import { fieldTypes } from '~/utils/formValidators'
25
+ import { makesSelector } from '~/store/vehicles/selectors'
21
26
22
27
const form = 'searchForm'
23
28
29
+ const normalizeSearchQuery = ( query : any ) => {
30
+ console . log ( 'query' , query )
31
+ return {
32
+ ...( query . pricemin ? { 'price[$gte]' : query . pricemin } : { } ) ,
33
+ ...( query . pricemax
34
+ ? { 'price[$lte]' : toNumber ( get ( query , 'pricemax' , 0 ) ) + 1 }
35
+ : { } ) ,
36
+ ...( query . model ? { 'vehicle.modelId' : query . model } : { } ) , // create one for make
37
+ ...( query . transmission
38
+ ? { 'vehicle.transmission' : query . transmission }
39
+ : { } ) ,
40
+ ...( query . fuel ? { 'vehicle.fuel' : query . fuel } : { } ) ,
41
+ ...( query . color ? { 'vehicle.color' : query . color } : { } ) ,
42
+ ...( query . bodyType
43
+ ? {
44
+ 'vehicle.bodyType' : {
45
+ $in : query . bodyType ,
46
+ } ,
47
+ }
48
+ : { } ) ,
49
+ }
50
+ }
51
+
52
+ // roomId: {
53
+ // $in: [ 2, 5 ]
54
+ // }
55
+
24
56
const SearchPage : React . FunctionComponent = ( ) => {
25
57
const { query } = useRouter ( )
26
58
const { t } = useTranslation ( )
27
59
const dispatch = useDispatch ( )
28
60
const pagination = useSelector ( listingPaginationSelector )
29
61
const listings = useSelector ( listingsSelector )
62
+ const makes = useSelector ( makesSelector )
30
63
const count = get ( pagination , 'total' , 0 )
31
64
32
65
const fetch = ( resetPagination = false ) => {
33
- dispatch ( Creators . fetchListings ( {
66
+ dispatch (
67
+ Creators . fetchListings ( {
34
68
resetPagination,
35
- ...( query . pricemin ? { 'price[$gte]' : query . pricemin } : { } ) ,
36
- ... ( query . pricemax ? { 'price[$lte]' : toNumber ( get ( query , 'pricemax' , 0 ) ) + 1 } : { } ) ,
37
- } ) )
69
+ ...normalizeSearchQuery ( query ) ,
70
+ } )
71
+ )
38
72
}
39
73
useEffect ( ( ) => {
40
74
if ( query ) {
41
- fetch ( ) ;
75
+ fetch ( )
42
76
if ( query . pricemin ) {
43
77
dispatch ( change ( form , `${ fieldTypes . price } .min` , query . pricemin ) )
44
78
}
45
79
if ( query . pricemax ) {
46
80
dispatch ( change ( form , `${ fieldTypes . price } .max` , query . pricemax ) )
47
81
}
82
+ if ( query . yearmax ) {
83
+ dispatch ( change ( form , `${ fieldTypes . year } .max` , query . yearmax ) )
84
+ }
85
+ if ( query . yearmin ) {
86
+ dispatch ( change ( form , `${ fieldTypes . year } .min` , query . yearmin ) )
87
+ }
88
+ if ( query . color ) {
89
+ dispatch ( change ( form , `${ fieldTypes . color } ` , query . color ) )
90
+ }
91
+ if ( query . bodyType ) {
92
+ dispatch ( change ( form , `${ fieldTypes . bodyType } ` , query . bodyType ) )
93
+ }
94
+ if ( query . transmission ) {
95
+ dispatch ( change ( form , `${ fieldTypes . gearbox } ` , query . transmission ) )
96
+ }
97
+ if ( query . fuel ) {
98
+ dispatch ( change ( form , `${ fieldTypes . fuel } ` , query . fuel ) )
99
+ }
100
+ if ( query . make ) {
101
+ dispatch ( change ( form , `${ fieldTypes . make } ` , query . make ) )
102
+ }
48
103
}
49
104
} , [ query ] )
105
+
106
+ useEffect ( ( ) => {
107
+ if ( ! isEmpty ( makes ) && query ) {
108
+ if ( query . model ) {
109
+ setTimeout ( ( ) => {
110
+ dispatch ( change ( form , `${ fieldTypes . model } ` , query . model ) )
111
+ } , 300 )
112
+ }
113
+ }
114
+ } , [ makes ] )
115
+
116
+ const queryKeys = keys ( query )
117
+
118
+ const defaultExpanded = some (
119
+ [ 'color' , 'pricemin' , 'pricemax' , 'yearmin' , 'yearmax' ] ,
120
+ ( key ) => includes ( queryKeys , key )
121
+ )
122
+
50
123
return (
51
124
< Layout background = "white" fullscreen >
52
125
< div className = { styles . search } >
53
126
< h1 > { `${ t ( 'titles.foundVehicles' ) } : ${ count } ` } </ h1 >
54
- < SearchForm cb = { ( ) => fetch ( true ) } fluid />
127
+ < SearchForm
128
+ cb = { ( ) => fetch ( true ) }
129
+ defaultExpanded = { defaultExpanded }
130
+ fluid
131
+ />
55
132
</ div >
56
133
< ListingsCarousel type = "FEATURED" title = { t ( 'titles.featuredVehicles' ) } />
57
134
< InfinitePagination
0 commit comments