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.model} 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 && 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 }
}
}
import { Grid, Card, CardHeader, Avatar, IconButton, CardMedia, Typography, makeStyles } from '@material-ui/core';
import MoreVertIcon from '@material-ui/icons/MoreVert';
import CardContent from '@material-ui/core/CardContent';
import deepEqual from 'fast-deep-equal';
import { GetServerSideProps } from 'next';
import { useRouter } from 'next/router';
import { stringify } from 'querystring';
import { useState } from 'react';
import useSWR from 'swr';
import Search from '.';
import { CarModel } from './api/Car';
import Link from 'next/link';
import Pagination from '@material-ui/lab/Pagination';
import { getMakes, Make } from './database/getMakes';
import { getModels, Model } from './database/getModels';
import getPaginatedCars from './database/getPaginatedCars';
import getAsString from '../getAsString';
export interface CarsListProps {
makes: Make[];
models: Model[];
cars: CarModel[];
totalPages: number;
}
const useStyles = makeStyles((theme) => ({
media: {
height: 0,
paddingTop: '56.25%', // 16:9
},
expand: {
transform: 'rotate(0deg)',
marginLeft: 'auto',
transition: theme.transitions.create('transform', {
duration: theme.transitions.duration.shortest,
}),
},
expandOpen: {
transform: 'rotate(180deg)',
},
avatar: {
backgroundColor: 'red',
},
achorTag: {
textDecoration: 'none'
}
}));
export default function CarsList({makes, models, cars, totalPages }:CarsListProps) {
const classes = useStyles();
const { query } = useRouter();
const [serverQuery] = useState(query);
const { data } = useSWR('/api/cars?' + stringify(query), {
dedupingInterval: 15000,
initialData: deepEqual(query, serverQuery) ? { cars, totalPages } : undefined
});
return (
<Grid container spacing={3}>
<Grid item xs={12} sm={5} md={3} lg={2}>
<Search makes={makes} models={models} />
</Grid>
<Grid container item xs={12} sm={7} md={9} lg={10} spacing={3}>
<Grid item xs={12}>
<Pagination count={10} />
</Grid>
<Grid item xs={12} sm={6}>
<Link
href="#"
as={null}
>
<a className={classes.achorTag}>
<Card>
<CardHeader
avatar={
<Avatar aria-label="recipe" className={classes.avatar}>
R
</Avatar>
}
action={
<IconButton aria-label="settings">
<MoreVertIcon />
</IconButton>
}
title={'car.mode'}
subheader={5}
/>
<CardMedia
className={classes.media}
image='https://thumbs.dreamstime.com/z/imge-mint-image-green-leaves-79631310.jpg'
title='aaaaaaaaaa'
/>
<CardContent>
<Typography variant="body2" color="textSecondary" component="p">
{'car.details'}
</Typography>
</CardContent>
</Card>
</a>
</Link>
</Grid>
<Grid item xs={12}>
<Pagination count={10} />
</Grid>
</Grid>
</Grid>
);
}
export const getServerSideProps: GetServerSideProps<CarsListProps> = async (ctx) => {
const make = getAsString(ctx.query.make);
const [makes, models, pagination] = await Promise.all([getMakes(),getModels(make),getPaginatedCars(ctx.query)]);
console.log(pagination);
return {
props: {makes, models}
}
}