-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add nextjs api & react hook form example
- Loading branch information
Showing
10 changed files
with
199 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
export const GET = async (req: Request) => { | ||
const { searchParams } = new URL(req.url); | ||
const reqData = Object.fromEntries(searchParams); | ||
return NextResponse.json({ | ||
message: 'Test getApiResponse GET success!', | ||
method: 'GET', | ||
reqData, | ||
}); | ||
}; | ||
|
||
export const POST = async (req: Request) => { | ||
const reqData = await req.json(); | ||
return NextResponse.json({ | ||
message: 'Test postApiResponse POST success!', | ||
method: 'POST', | ||
reqData, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use client'; | ||
|
||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { Box, Button, FormHelperText } from '@mui/material'; | ||
import React from 'react'; | ||
import { Controller, SubmitHandler, useForm } from 'react-hook-form'; | ||
import { z } from 'zod'; | ||
|
||
import { consoleLog } from '@/util/shared/console-log'; | ||
import { getApiResponse } from '@/util/shared/get-api-response'; | ||
|
||
const zodSchema = z.object({ | ||
name: z.string().min(5).nonempty({ message: 'Name is required' }), | ||
email: z.string().min(10).email({ message: 'Invalid email address' }), | ||
}); | ||
|
||
type FormValues = z.infer<typeof zodSchema>; | ||
|
||
const ReactHookForm: React.FC = () => { | ||
const apiEndpoint = '/api/test'; | ||
const [apiResult, setApiResult] = React.useState<FormValues>(); | ||
|
||
const { | ||
handleSubmit, | ||
control, | ||
formState: { errors }, | ||
} = useForm<FormValues>({ | ||
resolver: zodResolver(zodSchema), | ||
}); | ||
|
||
const onSubmit: SubmitHandler<FormValues> = async (data) => { | ||
try { | ||
const result = await getApiResponse<{ | ||
reqData: FormValues; | ||
}>({ | ||
apiEndpoint, | ||
method: 'POST', | ||
requestData: JSON.stringify(data), | ||
}); | ||
setApiResult(result?.reqData); | ||
consoleLog('getApiResponse result', result, errors); | ||
} catch (error) { | ||
consoleLog('handleSubmit ERROR', error); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<Box sx={{ m: 2 }}> | ||
<label>Name:</label> | ||
<Controller | ||
name='name' | ||
control={control} | ||
defaultValue='' | ||
render={({ field }) => <input {...field} />} | ||
/> | ||
{errors.name && ( | ||
<FormHelperText sx={{ textAlign: 'center' }}> | ||
{errors.name.message} | ||
</FormHelperText> | ||
)} | ||
</Box> | ||
|
||
<Box sx={{ m: 2 }}> | ||
<label>Email:</label> | ||
<Controller | ||
name='email' | ||
control={control} | ||
defaultValue='' | ||
render={({ field }) => <input {...field} />} | ||
/> | ||
{errors.email && ( | ||
<FormHelperText sx={{ textAlign: 'center' }}> | ||
{errors.email.message} | ||
</FormHelperText> | ||
)} | ||
</Box> | ||
{apiResult && ( | ||
<Box sx={{ m: 2, color: 'green' }}> | ||
API result from {apiEndpoint}: {apiResult.name} & {apiResult.email} | ||
</Box> | ||
)} | ||
<Button variant='contained' type='submit'> | ||
Test react hook form with zod | ||
</Button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default ReactHookForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import dayjs from 'dayjs'; | ||
import timezone from 'dayjs/plugin/timezone'; | ||
import utc from 'dayjs/plugin/utc'; | ||
|
||
dayjs.extend(utc); | ||
dayjs.extend(timezone); | ||
|
||
const ServerDateTime = ({ | ||
cityTimezone, | ||
timeFormat = 'dddd, MMMM D, YYYY h:mm:ss A', | ||
color, | ||
date, | ||
}: { | ||
cityTimezone: string; | ||
timeFormat?: string; | ||
color?: string; | ||
date?: string; | ||
}) => { | ||
return ( | ||
<span className='date-time' style={{ color }}> | ||
{dayjs(date).tz(cityTimezone).format(timeFormat)} | ||
</span> | ||
); | ||
}; | ||
|
||
export default ServerDateTime; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters