Skip to content

Commit

Permalink
fix(client): move some pages away from getServerSideProps
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivo committed Apr 23, 2023
1 parent 78f63cf commit 9b03c48
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 122 deletions.
100 changes: 100 additions & 0 deletions apps/client/src/components/Forms/SignIn.tsx
@@ -0,0 +1,100 @@
import { useEffect, useState } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { Lock, LogIn, UserPlus } from "lucide-react";
import { useRouter } from "next/router";
import { signIn, useSession } from "next-auth/react";
import { useTranslation } from "next-i18next";

import { Button } from "@app/components/Button";
import { TextButton } from "@app/components/Button/Text";
import { Input } from "@app/components/Input";
import { Spinner } from "@app/components/Spinner";
import {
AuthFormSchema,
AuthFormSchemaType
} from "@app/schemas/components/auth/auth.zod";

export function SignInForm() {
const { t } = useTranslation(["signin"]);
const { status } = useSession();
const [isAuthenticating, setIsAuthenticating] = useState(false);
const {
register,
reset,
formState: { errors },
handleSubmit
} = useForm<AuthFormSchemaType>({
resolver: zodResolver(AuthFormSchema)
});
const router = useRouter();

useEffect(() => {
if (status === "authenticated") {
router.push("/my-dynamics");
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [status]);

const handleAuth: SubmitHandler<AuthFormSchemaType> = async ({
identifier,
password
}) => {
setIsAuthenticating(true);
signIn("credentials", {
identifier,
password,
redirect: false
})
.then(() => reset())
.finally(() => setIsAuthenticating(false));
};

return (
<form
className="flex flex-col gap-y-2.5 lg:mx-auto lg:w-1/2"
onSubmit={handleSubmit(handleAuth)}
>
<Input
error={errors.identifier}
label={t("navigation:forms.identifier.title")}
placeholder={t("navigation:forms.identifier.placeholder")}
{...register("identifier")}
/>
<Input
error={errors.password}
label={t("navigation:forms.password.title")}
placeholder={t("navigation:forms.password.placeholder")}
type="password"
{...register("password")}
/>
<Button
disabled={isAuthenticating}
LeftIcon={!isAuthenticating ? LogIn : undefined}
type="submit"
>
{isAuthenticating ? <Spinner /> : null}
{t("navigation:forms.submit")}
</Button>
<div className="flex gap-x-2">
<TextButton
className="text-sm"
iconClassName="h-4 w-4"
LeftIcon={Lock}
type="button"
>
{t("navigation:forms.lost-password")}
</TextButton>
<TextButton
className="text-sm"
iconClassName="h-4 w-4"
LeftIcon={UserPlus}
onClick={() => router.push("/register")}
type="button"
>
{t("navigation:forms.new-user")}
</TextButton>
</div>
</form>
);
}
60 changes: 60 additions & 0 deletions apps/client/src/hocs/withSPTranslations.ts
@@ -0,0 +1,60 @@
import { GetStaticPropsContext, GetStaticPropsResult } from "next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";

type IncomingGSP<P> = (ctx: GetStaticPropsContext) => Promise<P>;

type WithSPTranslationsPropsResult = GetStaticPropsResult<{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}>;

type WithSPTranslationsOptions = {
namespaces: string[];
// any options you eventually would like to pass (required role...)
};

export function withSPTranslations(
incomingGSP?: IncomingGSP<WithSPTranslationsPropsResult> | null,
options?: WithSPTranslationsOptions
) {
return async (
ctx: GetStaticPropsContext
): Promise<WithSPTranslationsPropsResult> => {
const namespaces = options ? options.namespaces : [];

if (incomingGSP) {
const incomingGSPResult = await incomingGSP(ctx);

if ("props" in incomingGSPResult) {
return {
props: {
...incomingGSPResult.props,
...(await serverSideTranslations(ctx.locale ?? "en-US", [
"common",
"navigation",
...namespaces
]))
}
};
}

if ("redirect" in incomingGSPResult) {
return { redirect: { ...incomingGSPResult.redirect } };
}

if ("notFound" in incomingGSPResult) {
return { notFound: incomingGSPResult.notFound };
}
}

return {
props: {
...(await serverSideTranslations(ctx.locale ?? "en-US", [
"common",
"navigation",
...namespaces
]))
}
};
};
}
4 changes: 2 additions & 2 deletions apps/client/src/pages/404.tsx
Expand Up @@ -4,9 +4,9 @@ import { useTranslation } from "next-i18next";

import { Button } from "@app/components/Button";
import { SEO } from "@app/components/SEO";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import { withSPTranslations } from "@app/hocs/withSPTranslations";

export const getStaticProps = withSSRTranslations();
export const getStaticProps = withSPTranslations();

export default function Custom404() {
const router = useRouter();
Expand Down
4 changes: 2 additions & 2 deletions apps/client/src/pages/about.tsx
Expand Up @@ -2,9 +2,9 @@ import { useTranslation } from "next-i18next";

import { MaintainerCard } from "@app/components/MaintainerCard";
import { SEO } from "@app/components/SEO";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import { withSPTranslations } from "@app/hocs/withSPTranslations";

export const getServerSideProps = withSSRTranslations(undefined, {
export const getStaticProps = withSPTranslations(undefined, {
namespaces: ["about"]
});

Expand Down
12 changes: 10 additions & 2 deletions apps/client/src/pages/blog/[slug]/index.tsx
@@ -1,13 +1,21 @@
import { allPosts } from "contentlayer/generated";
import { Calendar, Clock } from "lucide-react";
import { GetStaticPaths } from "next";
import { useRouter } from "next/router";
import { useMDXComponent } from "next-contentlayer/hooks";
import { useTranslation } from "next-i18next";

import { components } from "@app/components/MDX/MDXComponents";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import { withSPTranslations } from "@app/hocs/withSPTranslations";

export const getServerSideProps = withSSRTranslations(async ({ params }) => {
export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: allPosts.map((p) => ({ params: { slug: p.slug } })),
fallback: false
};
};

export const getStaticProps = withSPTranslations(async ({ params }) => {
return {
props: {
post: allPosts.find((p) => p.slug === params?.slug)
Expand Down
4 changes: 2 additions & 2 deletions apps/client/src/pages/blog/index.tsx
@@ -1,9 +1,9 @@
import { allPosts } from "contentlayer/generated";

import BlogCard from "@app/components/BlogCard";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import { withSPTranslations } from "@app/hocs/withSPTranslations";

export const getServerSideProps = withSSRTranslations();
export const getStaticProps = withSPTranslations();

export default function Blog() {
return (
Expand Down
4 changes: 2 additions & 2 deletions apps/client/src/pages/index.tsx
Expand Up @@ -2,9 +2,9 @@ import { CloudCog, Code2 } from "lucide-react";
import { useTranslation } from "next-i18next";

import { SEO } from "@app/components/SEO";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import { withSPTranslations } from "@app/hocs/withSPTranslations";

export const getStaticProps = withSSRTranslations(undefined, {
export const getStaticProps = withSPTranslations(undefined, {
namespaces: ["home"]
});

Expand Down
127 changes: 15 additions & 112 deletions apps/client/src/pages/signin.tsx
@@ -1,59 +1,29 @@
import { useEffect, useState } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { Lock, LogIn, UserPlus } from "lucide-react";
import { useRouter } from "next/router";
import { signIn, useSession } from "next-auth/react";
import dynamic from "next/dynamic";
import { useTranslation } from "next-i18next";

import { Button } from "@app/components/Button";
import { TextButton } from "@app/components/Button/Text";
import { Input } from "@app/components/Input";
import { SEO } from "@app/components/SEO";
import { Spinner } from "@app/components/Spinner";
import { withSSRGuest } from "@app/hocs/withSSRGuest";
import { withSSRTranslations } from "@app/hocs/withSSRTranslations";
import {
AuthFormSchema,
AuthFormSchemaType
} from "@app/schemas/components/auth/auth.zod";

const SignInForm = dynamic(
() => import("@app/components/Forms/SignIn").then((mod) => mod.SignInForm),
{
loading: () => (
<div className="flex flex-1 items-center justify-center">
<Spinner />
</div>
),
ssr: false
}
);

export const getServerSideProps = withSSRTranslations(withSSRGuest(), {
namespaces: ["signin"]
});

export default function SignIn() {
const { t } = useTranslation(["signin"]);
const { status } = useSession();
const [isAuthenticating, setIsAuthenticating] = useState(false);
const {
register,
reset,
formState: { errors },
handleSubmit
} = useForm<AuthFormSchemaType>({
resolver: zodResolver(AuthFormSchema)
});
const router = useRouter();

useEffect(() => {
if (status === "authenticated") {
router.push("/my-dynamics");
}
}, [status, router]);

const handleAuth: SubmitHandler<AuthFormSchemaType> = async ({
identifier,
password
}) => {
setIsAuthenticating(true);
signIn("credentials", {
identifier,
password,
redirect: false
})
.then(() => reset())
.finally(() => setIsAuthenticating(false));
};

return (
<>
Expand All @@ -64,74 +34,7 @@ export default function SignIn() {
<h2 className="text-center text-2xl font-bold uppercase text-primary-700 dark:text-primary-400">
{t("signin:title")}
</h2>
<form
className="flex flex-col gap-y-2.5 lg:mx-auto lg:w-1/2"
onSubmit={handleSubmit(handleAuth)}
>
<Input
error={errors.identifier}
label={t("navigation:forms.identifier.title")}
placeholder={t("navigation:forms.identifier.placeholder")}
{...register("identifier")}
/>
<Input
error={errors.password}
label={t("navigation:forms.password.title")}
placeholder={t("navigation:forms.password.placeholder")}
type="password"
{...register("password")}
/>
<Button
disabled={isAuthenticating}
LeftIcon={!isAuthenticating ? LogIn : undefined}
type="submit"
>
{isAuthenticating ? (
<div
className="z-0"
role="status"
>
<svg
aria-hidden="true"
className="h-5 w-5 animate-spin fill-primary-950 text-primary-100"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
) : null}
{t("navigation:forms.submit")}
</Button>
<div className="flex gap-x-2">
<TextButton
className="text-sm"
iconClassName="h-4 w-4"
LeftIcon={Lock}
type="button"
>
{t("navigation:forms.lost-password")}
</TextButton>
<TextButton
className="text-sm"
iconClassName="h-4 w-4"
LeftIcon={UserPlus}
onClick={() => router.push("/register")}
type="button"
>
{t("navigation:forms.new-user")}
</TextButton>
</div>
</form>
<SignInForm />
</>
);
}

0 comments on commit 9b03c48

Please sign in to comment.