import { Form, Link, useActionData, useLoaderData, useNavigation } from '@remix-run/react'; import { json, redirect } from '@remix-run/server-runtime'; import { getSignedInUser, sessionStorage } from '../auth.server'; import { TextInput } from '../components/TextInput'; import { validateFormData } from '../utils/utils'; import { z } from 'zod'; import { Button } from '../components/Button'; import type { ActionArgs, LoaderArgs } from '@remix-run/server-runtime'; import { getUserByEmail } from '../data/zippo.server'; const firstPageFormModel = z.object({ firstName: z.string().min(1, 'First name is required'), lastName: z.string().min(1, 'Last name is required'), email: z.string().email('Please enter a valid email address'), }); type ActionInput = z.TypeOf; type Errors = Partial<{ general: string } & Record>; export async function loader({ request }: LoaderArgs) { const [user, headers] = await getSignedInUser(request); if (user) { throw redirect('/apps', { headers }); } const session = await sessionStorage.getSession(request.headers.get('Cookie')); const createAccountSession = session.get('createAccount'); return json( createAccountSession ? { firstName: createAccountSession.firstName as string, lastName: createAccountSession.lastName as string, email: createAccountSession.email as string, } : null, 200, ); } export async function action({ request }: ActionArgs) { const [user, headers] = await getSignedInUser(request); if (user) { throw redirect('/apps', { headers }); } const formData = await request.formData(); const { body, errors } = validateFormData({ formData: formData, schema: firstPageFormModel, }); if (errors) { return json({ errors: errors as Errors, values: body }); } const lowerCasedEmail = body.email.toLowerCase(); const userResult = await getUserByEmail({ email: lowerCasedEmail }); if (userResult.result === 'SUCCESS') { return json({ errors: { email: 'This email is already in use', } as Errors, values: body, }); } else if (userResult.result === 'ERROR' && userResult.error.message !== 'User not found') { return json({ errors: { email: 'An error occurred while creating an account', } as Errors, values: body, }); } const session = await sessionStorage.getSession(request.headers.get('Cookie')); session.set('createAccount', { firstName: body.firstName, lastName: body.lastName, email: lowerCasedEmail, }); throw redirect('/create-account/set-password', { headers: { 'Set-Cookie': await sessionStorage.commitSession(session), }, }); } export const handle = { showNavSwitch: true, }; export default function CreateAccount() { const actionData = useActionData(); const maybeSessionInfo = useLoaderData(); const navigation = useNavigation(); return (

Create an account

Do you have an account?{' '} Login

); }