Skip to content

Commit

Permalink
remove submitForm util
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Oct 10, 2024
1 parent ffac629 commit 1d0c217
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 164 deletions.
7 changes: 3 additions & 4 deletions src/features/footer/RegisterToNewsletterForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as forms from "codeforlife/components/form"
import { FormHelperText, Stack, useMediaQuery, useTheme } from "@mui/material"
import { type FC } from "react"
import { submitForm } from "codeforlife/utils/form"
import { useNavigate } from "react-router-dom"

import { useRegisterToNewsletterMutation } from "../../api/user"
Expand All @@ -11,7 +10,6 @@ export interface RegisterToNewsletterFormProps {}
const RegisterToNewsletterForm: FC<RegisterToNewsletterFormProps> = () => {
const theme = useTheme()
const onlyXS = useMediaQuery(theme.breakpoints.only("xs"))
const [registerToNewsletter] = useRegisterToNewsletterMutation()
const navigate = useNavigate()

return (
Expand All @@ -25,7 +23,8 @@ const RegisterToNewsletterForm: FC<RegisterToNewsletterFormProps> = () => {
email: "",
over18: false,
}}
onSubmit={submitForm(registerToNewsletter, {
useMutation={useRegisterToNewsletterMutation}
submitOptions={{
exclude: ["over18"],
then: () => {
navigate(".", {
Expand Down Expand Up @@ -54,7 +53,7 @@ const RegisterToNewsletterForm: FC<RegisterToNewsletterFormProps> = () => {
},
})
},
})}
}}
>
<Stack spacing={2}>
<forms.EmailField
Expand Down
7 changes: 3 additions & 4 deletions src/pages/login/IndyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as form from "codeforlife/components/form"
import { Stack, Typography } from "@mui/material"
import type { FC } from "react"
import { Link } from "codeforlife/components/router"
import { submitForm } from "codeforlife/utils/form"
import { useNavigate } from "codeforlife/hooks"

import BaseForm from "./BaseForm"
Expand All @@ -12,7 +11,6 @@ import { useLoginWithEmailMutation } from "../../api/sso"
export interface IndyFormProps {}

const IndyForm: FC<IndyFormProps> = () => {
const [loginWithEmail] = useLoginWithEmailMutation()
const navigate = useNavigate()

return (
Expand All @@ -21,11 +19,12 @@ const IndyForm: FC<IndyFormProps> = () => {
header="Welcome"
subheader="Please enter your login details."
initialValues={{ email: "", password: "" }}
onSubmit={submitForm(loginWithEmail, {
useMutation={useLoginWithEmailMutation}
submitOptions={{
then: () => {
navigate(paths.indy.dashboard._)
},
})}
}}
>
<form.EmailField required />
<form.PasswordField required />
Expand Down
7 changes: 3 additions & 4 deletions src/pages/login/studentForms/FirstName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { type FC, useEffect } from "react"
import { useNavigate, useParams } from "codeforlife/hooks"
import { ChevronRight as ChevronRightIcon } from "@mui/icons-material"
import { Stack } from "@mui/material"
import { submitForm } from "codeforlife/utils/form"

import BaseForm from "../BaseForm"
import { classIdSchema } from "../../../app/schemas"
Expand All @@ -13,7 +12,6 @@ import { useLoginAsStudentMutation } from "../../../api/sso"
export interface FirstNameProps {}

const FirstName: FC<FirstNameProps> = () => {
const [loginAsStudent] = useLoginAsStudentMutation()
const navigate = useNavigate()

const params = useParams({ classId: classIdSchema.required() })
Expand Down Expand Up @@ -47,11 +45,12 @@ const FirstName: FC<FirstNameProps> = () => {
password: "",
class_id: params.classId,
}}
onSubmit={submitForm(loginAsStudent, {
useMutation={useLoginAsStudentMutation}
submitOptions={{
then: () => {
navigate(paths.student.dashboard._)
},
})}
}}
>
<form.FirstNameField required />
<form.PasswordField required />
Expand Down
7 changes: 3 additions & 4 deletions src/pages/login/teacherForms/Email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as form from "codeforlife/components/form"
import { Stack, Typography } from "@mui/material"
import type { FC } from "react"
import { Link } from "codeforlife/components/router"
import { submitForm } from "codeforlife/utils/form"
import { useNavigate } from "codeforlife/hooks"

import BaseForm from "../BaseForm"
Expand All @@ -12,7 +11,6 @@ import { useLoginWithEmailMutation } from "../../../api/sso"
export interface EmailProps {}

const Email: FC<EmailProps> = () => {
const [loginWithEmail] = useLoginWithEmailMutation()
const navigate = useNavigate()

return (
Expand All @@ -21,15 +19,16 @@ const Email: FC<EmailProps> = () => {
header="Welcome"
subheader="Please enter your login details."
initialValues={{ email: "", password: "" }}
onSubmit={submitForm(loginWithEmail, {
useMutation={useLoginWithEmailMutation}
submitOptions={{
then: ({ auth_factors }) => {
navigate(
auth_factors.includes("otp")
? paths.login.teacher.otp._
: paths.teacher.dashboard.tab.school._,
)
},
})}
}}
>
<form.EmailField required />
<form.PasswordField required />
Expand Down
7 changes: 3 additions & 4 deletions src/pages/login/teacherForms/Otp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useNavigate, useSession } from "codeforlife/hooks"
import { type FC } from "react"
import { LinkButton } from "codeforlife/components/router"
import { Stack } from "@mui/material"
import { submitForm } from "codeforlife/utils/form"

import BaseForm from "../BaseForm"
import { paths } from "../../../routes"
Expand All @@ -12,7 +11,6 @@ import { useLoginWithOtpMutation } from "../../../api/sso"
export interface OtpProps {}

const Otp: FC<OtpProps> = () => {
const [loginWithOtp] = useLoginWithOtpMutation()
const navigate = useNavigate()

return useSession(
Expand All @@ -22,11 +20,12 @@ const Otp: FC<OtpProps> = () => {
header="Welcome"
subheader="Please enter the token generated by your token generator."
initialValues={{ otp: "" }}
onSubmit={submitForm(loginWithOtp, {
useMutation={useLoginWithOtpMutation}
submitOptions={{
then: () => {
navigate(paths.teacher.dashboard.tab.school._)
},
})}
}}
>
<form.OtpField />
{otp_bypass_token_exists && (
Expand Down
7 changes: 3 additions & 4 deletions src/pages/login/teacherForms/OtpBypassToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Stack, Typography, useTheme } from "@mui/material"
import { useNavigate, useSession } from "codeforlife/hooks"
import type { FC } from "react"
import { LinkButton } from "codeforlife/components/router"
import { submitForm } from "codeforlife/utils/form"

import BaseForm from "../BaseForm"
import { paths } from "../../../routes"
Expand All @@ -13,7 +12,6 @@ import { useLoginWithOtpBypassTokenMutation } from "../../../api/sso"
export interface OtpBypassTokenProps {}

const OtpBypassToken: FC<OtpBypassTokenProps> = () => {
const [loginWithOtpBypassToken] = useLoginWithOtpBypassTokenMutation()
const navigate = useNavigate()
const theme = useTheme()

Expand All @@ -22,11 +20,12 @@ const OtpBypassToken: FC<OtpBypassTokenProps> = () => {
themedBoxProps={{ userType: "teacher" }}
header="Welcome"
initialValues={{ token: "" }}
onSubmit={submitForm(loginWithOtpBypassToken, {
useMutation={useLoginWithOtpBypassTokenMutation}
submitOptions={{
then: () => {
navigate(paths.teacher.dashboard.tab.school._)
},
})}
}}
>
<Typography marginBottom={theme.spacing(6)}>
Use this form for entering backup tokens for logging in. These tokens
Expand Down
7 changes: 3 additions & 4 deletions src/pages/register/IndyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ChevronRight as ChevronRightIcon } from "@mui/icons-material"
import { type FC } from "react"
import { Link } from "codeforlife/components/router"
import dayjs from "dayjs"
import { submitForm } from "codeforlife/utils/form"
import { useNavigate } from "react-router-dom"

import { LastNameField, NewPasswordField } from "../../components/form"
Expand All @@ -16,7 +15,6 @@ export interface IndyFormProps {}

const IndyForm: FC<IndyFormProps> = () => {
const navigate = useNavigate()
const [createIndependentUser] = useCreateIndependentUserMutation()

const EmailApplicableAge = 13
const ReceiveUpdateAge = 18
Expand All @@ -39,12 +37,13 @@ const IndyForm: FC<IndyFormProps> = () => {
password: "",
password_repeat: "",
}}
onSubmit={submitForm(createIndependentUser, {
useMutation={useCreateIndependentUserMutation}
submitOptions={{
exclude: ["password_repeat", "meets_criteria"],
then: () => {
navigate(paths.register.emailVerification.userType.indy._)
},
})}
}}
>
{form => {
const yearsOfAge = form.values.date_of_birth
Expand Down
7 changes: 3 additions & 4 deletions src/pages/register/TeacherForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ChevronRight as ChevronRightIcon } from "@mui/icons-material"
import { type FC } from "react"
import { Link } from "codeforlife/components/router"
import { Stack } from "@mui/material"
import { submitForm } from "codeforlife/utils/form"
import { useNavigate } from "codeforlife/hooks"

import { LastNameField, NewPasswordField } from "../../components/form"
Expand All @@ -15,7 +14,6 @@ export interface TeacherFormProps {}

const TeacherForm: FC<TeacherFormProps> = () => {
const navigate = useNavigate()
const [createTeacher] = useCreateTeacherMutation()

return (
<BaseForm
Expand All @@ -36,12 +34,13 @@ const TeacherForm: FC<TeacherFormProps> = () => {
meets_criteria: false,
},
}}
onSubmit={submitForm(createTeacher, {
useMutation={useCreateTeacherMutation}
submitOptions={{
exclude: ["user.password_repeat", "user.meets_criteria"],
then: () => {
navigate(paths.register.emailVerification.userType.teacher._)
},
})}
}}
>
<form.FirstNameField name="user.first_name" required />
<LastNameField name="user.last_name" />
Expand Down
11 changes: 6 additions & 5 deletions src/pages/studentAccount/UpdateAccountForm.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as forms from "codeforlife/components/form"
import { Stack, Typography } from "@mui/material"
import { getDirty, isDirty, submitForm } from "codeforlife/utils/form"
import { getDirty, isDirty } from "codeforlife/utils/form"
import { type FC } from "react"
import { LinkButton } from "codeforlife/components/router"
import { useNavigate } from "codeforlife/hooks"

import {
type RetrieveUserResult,
type UpdateUserArg,
type UpdateUserResult,
useUpdateUserMutation,
} from "../../api/user"
import { indyPasswordSchema, studentPasswordSchema } from "../../app/schemas"
Expand All @@ -18,7 +19,6 @@ export interface UpdateAccountFormProps {
}

const UpdateAccountForm: FC<UpdateAccountFormProps> = ({ user }) => {
const [updateUser] = useUpdateUserMutation()
const navigate = useNavigate()

const initialValues = user.student
Expand Down Expand Up @@ -69,7 +69,8 @@ const UpdateAccountForm: FC<UpdateAccountFormProps> = ({ user }) => {
)}
<forms.Form
initialValues={initialValues}
onSubmit={submitForm(updateUser, {
useMutation={useUpdateUserMutation}
submitOptions={{
exclude: ["password_repeat"],
clean: (values: typeof initialValues) => {
const arg: UpdateUserArg = { id: values.id }
Expand All @@ -87,7 +88,7 @@ const UpdateAccountForm: FC<UpdateAccountFormProps> = ({ user }) => {

return arg
},
then: (_, values) => {
then: (_: UpdateUserResult, values: typeof initialValues) => {
const messages = [
"Your account details have been changed successfully.",
]
Expand All @@ -111,7 +112,7 @@ const UpdateAccountForm: FC<UpdateAccountFormProps> = ({ user }) => {
},
})
},
})}
}}
>
{form => {
const dirty = getDirty(form.values, initialValues, [
Expand Down
7 changes: 3 additions & 4 deletions src/pages/teacherDashboard/classes/CreateClassForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Stack, Typography } from "@mui/material"
import { type FC } from "react"
import { type SchoolTeacherUser } from "codeforlife/api"
import { generatePath } from "react-router"
import { submitForm } from "codeforlife/utils/form"
import { useNavigate } from "codeforlife/hooks"

import {
Expand All @@ -20,7 +19,6 @@ export interface CreateClassFormProps {
}

const CreateClassForm: FC<CreateClassFormProps> = ({ authUser }) => {
const [createClass] = useCreateClassMutation()
const navigate = useNavigate()

return (
Expand All @@ -39,7 +37,8 @@ const CreateClassForm: FC<CreateClassFormProps> = ({ authUser }) => {
teacher: authUser.teacher.id,
read_classmates_data: false,
}}
onSubmit={submitForm(createClass, {
useMutation={useCreateClassMutation}
submitOptions={{
then: ({ id }, { name }) => {
navigate(
generatePath(paths.teacher.dashboard.tab.classes.class._, {
Expand All @@ -58,7 +57,7 @@ const CreateClassForm: FC<CreateClassFormProps> = ({ authUser }) => {
},
)
},
})}
}}
>
<Stack gap={2}>
<Stack direction={{ sm: "row" }} gap={2}>
Expand Down
Loading

0 comments on commit 1d0c217

Please sign in to comment.