import React, { useState, useEffect } from 'react';
import { Paper, Select, MenuItem, FormControl, InputLabel, makeStyles, Grid, Button } from '@material-ui/core';
import { Field, Form, Formik, FormikProps, useField, useFormikContext } from 'formik';
import { GetServerSideProps } from 'next';
import router, { useRouter } from 'next/router';
import useSWR from 'swr';
import { getMakes, Make } from './database/getMakes';
import { getModels, Model } from './database/getModels';
import { getAsString } from '../getAsString';
interface HomeProps {
makes: Make[],
models: Model[],
}
const useStyles = makeStyles((theme) => ({
paper: {
margin: 'auto',
maxWidth: 500,
padding: theme.spacing(3),
}
}));
const ModelSelect = ({ make, ...props }) => {
const { data: newModels } = useSWR < Model[] > ('/api/models?make=' + make);
if (!newModels) return 'loading...';
return (
<Field as={Select} labelId="model" label="Model" name="model">
<MenuItem value='all'>Model</MenuItem>
{
newModels.map((model)=>(
<MenuItem value={model.count} key={model.model}>{`${model.model} (${model.count})`}</MenuItem>
))
}
</Field>
)
}
const prices = [500, 1000, 5000, 15000, 25000, 50000, 250000];
export default function Home({ makes, models }: HomeProps) {
const classes = useStyles();
const { query } = useRouter();
const [initialValues] = useState({
make: getAsString(query.make) || 'all',
model: getAsString(query.model) || 'all',
minPrice: getAsString(query.minPrice) || 'all',
maxPrice: getAsString(query.maxPrice) || 'all',
});
return (
<Formik
initialValues={initialValues}
onSubmit={(values) => {
router.push({
pathname: '/cars',
query: { ...values, page: 1 }
})
}}
>
{({values}) => (
<Form
className={classes.paper}
variant="outlined"
>
<Paper>
<Grid container spacing={2}>
<Grid item xs={6}>
<FormControl fullWidth variant='outlined'>
<InputLabel id="make">Make</InputLabel>
<Field as={Select} labelId="make" label="Make" name="make">
<MenuItem value='all'>All Make</MenuItem>
{
makes.map((make)=>(
<MenuItem value={make.make} key={make.make}>{`${make.make} (${make.count})`}</MenuItem>
))
}
</Field>
</FormControl>
</Grid>
<Grid item xs={6}>
<FormControl fullWidth variant='outlined'>
<InputLabel id="model">Model</InputLabel>
<ModelSelect models={models} name="model" make={values.make} />
</FormControl>
</Grid>
<Grid item xs={6}>
<FormControl fullWidth variant='outlined'>
<InputLabel id="minPrice">Min Price</InputLabel>
<Field as={Select} labelId="minPrice" label="Min Price" name="minPrice">
<MenuItem value='all'>No Min</MenuItem>
{
prices.map((price)=> (
<MenuItem key={price} value={price}>{price}</MenuItem>
))
}
</Field>
</FormControl>
</Grid>
<Grid item xs={6}>
<FormControl fullWidth variant='outlined'>
<InputLabel id="maxPrice">Max Price</InputLabel>
<Field as={Select} labelId="maxPrice" label="Max Price" name="maxPrice">
<MenuItem value='all'>No Max</MenuItem>
{
prices.map((price)=> (
<MenuItem key={price} value={price}>{price}</MenuItem>
))
}
</Field>
</FormControl>
</Grid>
<Grid item xs={12}>
<Button type="submit" variant="contained" color="primary" fullWidth>Search</Button>
</Grid>
</Grid>
</Paper>
</Form>
)}
</Formik>
)
}
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const { make } = ctx.query;
const [makes, models] = await Promise.all([getMakes(), getModels(make)]);
return {
props: { makes, models }
}
}