-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: page component now includes loader & action like in remix, …
…keep files in folders flat
- Loading branch information
Showing
23 changed files
with
251 additions
and
258 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import reactjs from '#assets/images/reactjs.svg'; | ||
import { authApi, loginSchema } from '#auth/apis/auth.api'; | ||
import { LoginForm } from '#auth/components/login-form'; | ||
import { useUserStore } from '#auth/hooks/use-user-store.hook'; | ||
import { homePath } from '#home/routes'; | ||
import { useI18n } from '#shared/hooks/use-i18n/use-i18n.hook'; | ||
import { checkAuthUser } from '#shared/utils/checker.util'; | ||
import { Link } from 'react-aria-components'; | ||
import { unstable_batchedUpdates } from 'react-dom'; | ||
import type { ActionFunctionArgs, LoaderFunction } from 'react-router-dom'; | ||
import { json, redirect } from 'react-router-dom'; | ||
import { toast } from 'react-toastify'; | ||
|
||
export async function action({ request }: ActionFunctionArgs) { | ||
if (request.method === 'POST') { | ||
const payload = Object.fromEntries(await request.formData()); | ||
|
||
// if `payload` is not correct, return error object | ||
const parsed = loginSchema.safeParse(payload); | ||
if (!parsed.success) return json(parsed.error, { status: 400 }); | ||
|
||
// will throw if `login` returns 500 error, therefore `errorElement` will be rendered | ||
const loginResponse = await authApi.login(parsed.data); | ||
|
||
if ('message' in loginResponse) | ||
// on 400 error | ||
return json(loginResponse); | ||
|
||
// see https://docs.pmnd.rs/zustand/recipes/recipes#calling-actions-outside-a-react-event-handler | ||
unstable_batchedUpdates(() => { | ||
useUserStore.getState().setUser(loginResponse); // set user data to store | ||
}); | ||
|
||
return redirect(homePath.root); | ||
} | ||
|
||
toast.warning('Not Implemented'); | ||
return new Response('Not Implemented', { status: 501 }); | ||
} | ||
|
||
export const loader: LoaderFunction = () => { | ||
const authed = checkAuthUser(); | ||
|
||
// redirect auth user to home | ||
if (authed) { | ||
toast.info('Already Logged In'); | ||
return redirect(homePath.root); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export function Element() { | ||
const [t] = useI18n(); | ||
|
||
return ( | ||
<div id="LoginPage" className="min-h-screen w-full flex"> | ||
{/* form */} | ||
<section className="min-h-screen w-full flex flex-col justify-center px-10 xl:px-20 md:w-1/2"> | ||
<h1 className="text-center text-3xl text-primary">{t('welcome')}</h1> | ||
|
||
<LoginForm /> | ||
|
||
<p className="py-12 text-center"> | ||
{t('noAccount')}{' '} | ||
<Link | ||
aria-label={t('registerHere')} | ||
className="link link-primary" | ||
href="/does-not-exists" | ||
> | ||
{t('registerHere')} | ||
</Link> | ||
</p> | ||
</section> | ||
|
||
{/* image */} | ||
<section className="hidden md:block w-1/2 shadow-2xl"> | ||
<span className="relative h-screen w-full md:flex md:items-center md:justify-center"> | ||
<img | ||
src={reactjs} | ||
alt="cool react logo with rainbow shadow" | ||
loading="lazy" | ||
className="h-full object-cover" | ||
aria-label="cool react logo" | ||
/> | ||
</span> | ||
</section> | ||
</div> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,35 @@ | ||
import { authPath } from '#auth/routes'; | ||
import { HomeClock } from '#home/components/home-clock'; | ||
import { useI18n } from '#shared/hooks/use-i18n/use-i18n.hook'; | ||
import { checkAuthUser } from '#shared/utils/checker.util'; | ||
import { useAutoAnimate } from '@formkit/auto-animate/react'; | ||
import { redirect, type LoaderFunction } from 'react-router-dom'; | ||
import { toast } from 'react-toastify'; | ||
|
||
export const loader: LoaderFunction = () => { | ||
const authed = checkAuthUser(); | ||
|
||
// redirect NOT authed user to login | ||
if (!authed) { | ||
toast.error('Unauthorized'); | ||
return redirect(authPath.login); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export function Element() { | ||
const [t] = useI18n(); | ||
const [parentRef] = useAutoAnimate(); | ||
|
||
return ( | ||
<div | ||
ref={parentRef} | ||
className="container mx-auto flex flex-col items-center py-24 duration-300" | ||
> | ||
<h1 className="text-3xl sm:text-4xl">{t('title')}</h1> | ||
|
||
<HomeClock /> | ||
</div> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
File renamed without changes.
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
Oops, something went wrong.