import { Student } from "@/data"; import { Transformer } from "@/serialization"; import React, { useMemo } from "react"; import { Field, Formik, useFormikContext } from "formik"; import api from "@/api"; import { Button, Grid, Typography } from "@material-ui/core"; import { TextField as TextFieldFormik } from "formik-material-ui"; import { useTranslation } from "react-i18next"; import { Actions } from "@/components"; import { Nullable } from "@/helpers"; import * as Yup from "yup"; import { StudentActions, useDispatch } from "@/state/actions"; import { route } from "@/routing"; import { useHistory } from "react-router-dom"; interface StudentFormValues { firstName: string; lastName: string; email: string; albumNumber: number | ""; semester: number | ""; } type StudentFormProps = { student: Student; } const studentToFormValuesTransformer: Transformer, StudentFormValues, { current: Student }> = { transform(subject: Nullable, context: { current: Student }): StudentFormValues { return { firstName: subject.name || "", lastName: subject.surname || "", albumNumber: subject.albumNumber || "", semester: subject.semester || "", email: subject.email || "", }; }, reverseTransform(subject: StudentFormValues, { current }: { current: Student }): Nullable { return { ...current, name: subject.firstName, surname: subject.lastName, albumNumber: subject.albumNumber ? subject.albumNumber : null, semester: subject.semester ? subject.semester : null, email: subject.email, }; }, } export const StudentForm = ({ student }: StudentFormProps) => { const { t } = useTranslation(); const dispatch = useDispatch(); const history = useHistory(); const validationSchema = useMemo(() => Yup.object({ semester: Yup.number().required().min(1).max(10), albumNumber: Yup.number().required(), email: Yup.string().required(), firstName: Yup.string().required(), lastName: Yup.string().required(), }), []); const initialValues: StudentFormValues = useMemo( () => studentToFormValuesTransformer.transform(student, { current: student }), [ student ] ) const handleFormSubmit = async (values: StudentFormValues) => { const update = studentToFormValuesTransformer.reverseTransform(values, { current: student }) as Student; const updated = await api.student.update(update); dispatch({ type: StudentActions.Set, student: updated, }) history.push(route("home")); } const InnerForm = () => { const { handleSubmit } = useFormikContext(); return
{ t("forms.student.sections.personal") } { t("forms.student.sections.studies")}
} return } export default StudentForm;