generated from theodorusclarence/ts-nextjs-tailwind-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bf4782
commit 14ea6ba
Showing
6 changed files
with
210 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 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,31 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { addLink, checkSlugIsTaken } from '@/lib/notion'; | ||
|
||
export default async function NewLinkHandler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
if (req.method === 'POST') { | ||
const url = req.body as { link: string; slug: string }; | ||
|
||
if (!url.link || !url.slug) { | ||
return res.status(400).json({ | ||
message: 'Link and slug are required', | ||
}); | ||
} | ||
|
||
const taken = await checkSlugIsTaken(url.slug); | ||
if (taken) { | ||
return res.status(409).json({ | ||
message: 'Slug is already taken', | ||
}); | ||
} | ||
|
||
await addLink(url.slug, url.link); | ||
|
||
res.status(201).send('OK'); | ||
} else { | ||
res.status(405).json({ message: 'Method Not Allowed' }); | ||
} | ||
} |
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,98 @@ | ||
import axios from 'axios'; | ||
import * as React from 'react'; | ||
import { FormProvider, SubmitHandler, useForm } from 'react-hook-form'; | ||
import toast from 'react-hot-toast'; | ||
|
||
import Accent from '@/components/Accent'; | ||
import Button from '@/components/buttons/Button'; | ||
import Input from '@/components/forms/Input'; | ||
import Layout from '@/components/layout/Layout'; | ||
import Seo from '@/components/Seo'; | ||
|
||
import { DEFAULT_TOAST_MESSAGE } from '@/constant/toast'; | ||
|
||
type NewLinkFormData = { | ||
slug: string; | ||
link: string; | ||
}; | ||
|
||
export default function NewLinkPage() { | ||
//#region //*=========== Form =========== | ||
const methods = useForm<NewLinkFormData>({ | ||
mode: 'onTouched', | ||
}); | ||
const { handleSubmit } = methods; | ||
//#endregion //*======== Form =========== | ||
|
||
//#region //*=========== Form Submit =========== | ||
const onSubmit: SubmitHandler<NewLinkFormData> = (data) => { | ||
toast.promise(axios.post('/api/new', data), { | ||
...DEFAULT_TOAST_MESSAGE, | ||
success: 'Link successfully shortened', | ||
}); | ||
}; | ||
//#endregion //*======== Form Submit =========== | ||
|
||
return ( | ||
<Layout> | ||
<Seo templateTitle='Shorten!' /> | ||
|
||
<main> | ||
<section> | ||
<div className='layout flex flex-col items-center py-20 min-h-screen'> | ||
<h1 className='h0'> | ||
<Accent>Shorten New Link</Accent> | ||
</h1> | ||
|
||
<FormProvider {...methods}> | ||
<form | ||
onSubmit={handleSubmit(onSubmit)} | ||
className='mt-8 w-full max-w-sm' | ||
> | ||
<div className='space-y-4'> | ||
<Input | ||
id='slug' | ||
label='Slug' | ||
placeholder='slug' | ||
validation={{ | ||
required: 'Slug must be filled', | ||
pattern: { | ||
value: /^\S+$/, | ||
message: 'Cannot include whitespace', | ||
}, | ||
}} | ||
/> | ||
<Input | ||
id='link' | ||
label='Link' | ||
helperText='Must include http or https' | ||
placeholder='https://google.com' | ||
validation={{ | ||
required: 'Link must be filled', | ||
pattern: { | ||
value: | ||
/^(?:https?:\/\/|s?ftps?:\/\/)(?!www | www\.)[A-Za-z0-9_-]+\.+[A-Za-z0-9./%#*&=?_:;-]+$/, | ||
message: 'Please input a valid link', | ||
}, | ||
}} | ||
/> | ||
</div> | ||
|
||
<div className='flex flex-col mt-5'> | ||
<Button | ||
className='justify-center w-full md:ml-auto md:w-auto' | ||
variant='outline' | ||
isDarkBg | ||
type='submit' | ||
> | ||
Shorten! | ||
</Button> | ||
</div> | ||
</form> | ||
</FormProvider> | ||
</div> | ||
</section> | ||
</main> | ||
</Layout> | ||
); | ||
} |
14ea6ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: