import { Paper, Select, MenuItem, FormControl, InputLabel, makeStyles, Grid, createStyles } from '@material-ui/core';
import { Field, Form, Formik, FormikProps } from 'formik';
import { GetServerSideProps } from 'next';
import Make from '../interface/Make';
import Model from '../interface/Model';
import getMakes from './database/getMakes';
import getModels from './database/getModels';
import getAsString from '../getAsString';
import { useEffect, useState } from 'react';
import { useRouter } from 'next/router';
import useSWR from 'swr';
const useStyles = makeStyles((theme) => createStyles({
paper: {
margin: 'auto',
maxWidth: 500,
padding: theme.spacing(3),
}
}));
interface HomeProps {
makes: Make[],
model: string
}
const prices = [500, 1000, 5000, 15000, 25000, 50000, 250000];
export interface ModelSelectProps extends SelectProps {
name: string;
models: Model[];
make: string;
initialMake: string;
}
export default function Home({makes}:HomeProps) {
const classes = useStyles();
const { query } = useRouter();
const [initialValues] = useState({
make: 'all',
model: 'all',
minPrice: 'all',
maxPrice: 'all'
});
const ModelSelect = ({ initialMake, make, ...props }: ModelSelectProps) => {
const { data: newModels } = useSWR < Model[] > ('/api/models?make=' + make, {
dedupingInterval: 60000
});
if (!newModels) return <div>loading...</div>;
return (
<Field as={Select} labelId="model" label="Model" name="model">
<MenuItem value='all'>Model</MenuItem>
{
newModels.map((model)=>(
<MenuItem value={model.model} key={model.model}>{`${model.model} (${model.count})`}</MenuItem>
))
}
</Field>
)
}
return (
<Formik
initialValues={initialValues}
onSubmit={(value) => {
return null;
}}
>
{({values}) => (
<Form className={classes.paper}>
<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">
{
makes?.map((make) => {
return <MenuItem key={make.make} value={make.make}>{make.make + " (" + make.count + " )"}</MenuItem>
})
}
</Field>
</FormControl>
</Grid>
<Grid item xs={6}>
<ModelSelect initialMake={initialValues.make} name="model" make={values.make}/>
</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">
{
prices?.map(price => {
return <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">
{
prices?.map(price => {
return <MenuItem key={price} value={price}>{price}</MenuItem>
})
}
</Field>
</FormControl>
</Grid>
</Grid>
</Paper>
</Form>
)}
</Formik>
)
}
export const getServerSideProps: GetServerSideProps = async () => {
const [makes] = await Promise.all([getMakes()]);
return {
props: {makes}
}
}