Skip to content

Commit 398521e

Browse files
committed
search form
1 parent f9411f9 commit 398521e

File tree

5 files changed

+113
-22
lines changed

5 files changed

+113
-22
lines changed

components/Forms/SearchForm.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-underscore-dangle */
2-
import React, { useState } from 'react'
2+
import React, { useState, useEffect } from 'react'
33
import { reduxForm } from 'redux-form' // tslint:disable
44
import classNames from 'classnames'
55
import useFetchVehicleModels from '../../hooks/useFetchVehicleModels'
@@ -20,31 +20,38 @@ type Props = {
2020
className?: string | null
2121
fluid?: boolean
2222
cb?: () => void
23+
defaultExpanded?: boolean
2324
handleSubmit: (fn: any) => void
2425
}
2526

2627
const SearchFormComponent: React.FunctionComponent<Props> = ({
2728
className,
2829
fluid,
2930
cb,
31+
defaultExpanded,
3032
handleSubmit,
3133
}) => {
3234
const router = useRouter()
3335
useFetchVehicleModels(form)
34-
const [openExpand, setOpenExpand] = useState(false)
36+
const [openExpand, setOpenExpand] = useState(defaultExpanded || false)
37+
38+
useEffect(() => {
39+
if (defaultExpanded) setOpenExpand(true)
40+
}, [defaultExpanded])
41+
3542
/* @ts-ignore */
3643
const [_, __, closeModal] = useModal()
3744
const { t } = useTranslation()
3845
const onSubmit = (values: any) => {
3946
const query = toQueryBasic(values)
40-
closeModal();
47+
closeModal()
4148
if (query) {
4249
router.push({
4350
pathname: '/search',
4451
query,
4552
})
4653
}
47-
if (cb) cb();
54+
if (cb) cb()
4855
}
4956

5057
return (

components/Input/Select.module.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
font-size: $font-size-p;
4545
line-height: $line-height-h5;
4646
color: $color-black;
47+
@media (max-width: $tablet) {
48+
white-space: nowrap;
49+
max-width: 23vw;
50+
}
4751
}
4852
&:focus {
4953
border: $border-thick-primary;

components/Pickers/ColorPicker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ ColorPicker.propTypes = {
136136

137137
ColorPicker.defaultProps = {
138138
visibleColors: 3,
139-
allowEmpty: false,
139+
allowEmpty: true,
140140
label: null,
141141
}
142142

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
@import 'vars.scss';
22

33
.container {
4-
display: flex;
5-
.divider {
6-
margin-top: spacing(6);
7-
padding: 0 spacing(0.5);
8-
.line {
9-
width: 100%;
10-
min-width: 56px;
11-
height: 2px;
12-
background: $color-light-gray;
13-
}
14-
}
4+
display: grid;
5+
grid-template-columns: 1fr 1fr 1fr;
6+
.divider {
7+
margin-top: spacing(6);
8+
padding: 0 spacing(0.5);
9+
.line {
10+
width: 100%;
11+
height: 2px;
12+
background: $color-light-gray;
13+
@media (min-width: $tablet) {
14+
min-width: 56px;
15+
}
16+
}
17+
}
1518
}

pages/search/index.tsx

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { useSelector, useDispatch } from 'react-redux'
33
import { useTranslation } from 'react-i18next'
44
import { change } from 'redux-form' // tslint:disable
55
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'
610
import toNumber from 'lodash/toNumber'
711
import {
812
SearchForm,
@@ -18,40 +22,113 @@ import Creators from '../../store/listing/creators'
1822
import styles from './search.module.scss'
1923
import { useRouter } from 'next/router'
2024
import { fieldTypes } from '~/utils/formValidators'
25+
import { makesSelector } from '~/store/vehicles/selectors'
2126

2227
const form = 'searchForm'
2328

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+
2456
const SearchPage: React.FunctionComponent = () => {
2557
const { query } = useRouter()
2658
const { t } = useTranslation()
2759
const dispatch = useDispatch()
2860
const pagination = useSelector(listingPaginationSelector)
2961
const listings = useSelector(listingsSelector)
62+
const makes = useSelector(makesSelector)
3063
const count = get(pagination, 'total', 0)
3164

3265
const fetch = (resetPagination = false) => {
33-
dispatch(Creators.fetchListings({
66+
dispatch(
67+
Creators.fetchListings({
3468
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+
)
3872
}
3973
useEffect(() => {
4074
if (query) {
41-
fetch();
75+
fetch()
4276
if (query.pricemin) {
4377
dispatch(change(form, `${fieldTypes.price}.min`, query.pricemin))
4478
}
4579
if (query.pricemax) {
4680
dispatch(change(form, `${fieldTypes.price}.max`, query.pricemax))
4781
}
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+
}
48103
}
49104
}, [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+
50123
return (
51124
<Layout background="white" fullscreen>
52125
<div className={styles.search}>
53126
<h1>{`${t('titles.foundVehicles')}: ${count}`}</h1>
54-
<SearchForm cb={() => fetch(true)} fluid />
127+
<SearchForm
128+
cb={() => fetch(true)}
129+
defaultExpanded={defaultExpanded}
130+
fluid
131+
/>
55132
</div>
56133
<ListingsCarousel type="FEATURED" title={t('titles.featuredVehicles')} />
57134
<InfinitePagination

0 commit comments

Comments
 (0)