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