|
| 1 | +import { Box } from '@material-ui/core'; |
| 2 | +import { Formik, useFormikContext } from 'formik'; |
| 3 | +import { useSnackbar } from 'notistack'; |
| 4 | +import { useEffect, useMemo } from 'react'; |
| 5 | +import { useHistory } from 'react-router'; |
| 6 | +import { ICreateStudentsDTO, IStudent, StudentStatus } from 'shared/model/Student'; |
| 7 | +import FormikDebugDisplay from '../../../components/forms/components/FormikDebugDisplay'; |
| 8 | +import { useImportCSVContext } from '../../../components/import-csv/ImportCSV.context'; |
| 9 | +import { |
| 10 | + NextStepInformation, |
| 11 | + useStepper, |
| 12 | +} from '../../../components/stepper-with-buttons/context/StepperContext'; |
| 13 | +import { createManyStudents } from '../../../hooks/fetching/Student'; |
| 14 | +import { useCustomSnackbar } from '../../../hooks/snackbar/useCustomSnackbar'; |
| 15 | +import { FormikSubmitCallback } from '../../../types'; |
| 16 | +import { StudentColumns } from '../ImportStudents'; |
| 17 | +import StudentDataBox from './components/StudentDataBox'; |
| 18 | +import { convertCSVDataToFormData } from './components/StudentDataBox.helpers'; |
| 19 | + |
| 20 | +export interface StudentFormStateValue { |
| 21 | + rowNr: number; |
| 22 | + firstname: string; |
| 23 | + lastname: string; |
| 24 | + email?: string; |
| 25 | + status: StudentStatus; |
| 26 | + team?: string; |
| 27 | + iliasName?: string; |
| 28 | + matriculationNo?: string; |
| 29 | + courseOfStudies?: string; |
| 30 | +} |
| 31 | + |
| 32 | +export interface StudentFormState { |
| 33 | + [id: string]: StudentFormStateValue; |
| 34 | +} |
| 35 | + |
| 36 | +interface Props { |
| 37 | + tutorialId: string; |
| 38 | +} |
| 39 | + |
| 40 | +function convertValuesToDTO(values: StudentFormState, tutorialId: string): ICreateStudentsDTO { |
| 41 | + return { |
| 42 | + tutorial: tutorialId, |
| 43 | + students: Object.values(values), |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +function AdjustImportedUserDataFormContent(): JSX.Element { |
| 48 | + const { setNextCallback, removeNextCallback } = useStepper(); |
| 49 | + const { values, isValid, validateForm, submitForm } = useFormikContext<StudentFormState>(); |
| 50 | + const { enqueueSnackbar } = useSnackbar(); |
| 51 | + const history = useHistory(); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + setNextCallback(async (): Promise<NextStepInformation> => { |
| 55 | + const errors = await validateForm(); |
| 56 | + |
| 57 | + if (Object.entries(errors).length > 0) { |
| 58 | + enqueueSnackbar('Nutzerdaten sind ungültig.', { variant: 'error' }); |
| 59 | + return { goToNext: false, error: true }; |
| 60 | + } |
| 61 | + |
| 62 | + const isSuccess: any = await submitForm(); |
| 63 | + |
| 64 | + if (!!isSuccess) { |
| 65 | + return { goToNext: true }; |
| 66 | + } else { |
| 67 | + return { goToNext: false, error: true }; |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + return () => removeNextCallback(); |
| 72 | + }, [ |
| 73 | + setNextCallback, |
| 74 | + removeNextCallback, |
| 75 | + isValid, |
| 76 | + values, |
| 77 | + enqueueSnackbar, |
| 78 | + history, |
| 79 | + submitForm, |
| 80 | + validateForm, |
| 81 | + ]); |
| 82 | + |
| 83 | + return ( |
| 84 | + <Box display='flex' flex={1}> |
| 85 | + <StudentDataBox /> |
| 86 | + |
| 87 | + <FormikDebugDisplay showErrors /> |
| 88 | + </Box> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +function AdjustImportedStudentDataForm({ tutorialId }: Props): JSX.Element { |
| 93 | + const { |
| 94 | + csvData, |
| 95 | + mapColumnsHelpers: { mappedColumns }, |
| 96 | + } = useImportCSVContext<StudentColumns, string>(); |
| 97 | + const { enqueueSnackbar, enqueueSnackbarWithList } = useCustomSnackbar(); |
| 98 | + |
| 99 | + const initialValues: StudentFormState = useMemo(() => { |
| 100 | + return convertCSVDataToFormData({ |
| 101 | + data: csvData, |
| 102 | + values: mappedColumns, |
| 103 | + }); |
| 104 | + }, [csvData, mappedColumns]); |
| 105 | + |
| 106 | + const handleSubmit: FormikSubmitCallback<StudentFormState> = async (values) => { |
| 107 | + const dto: ICreateStudentsDTO = convertValuesToDTO(values, tutorialId); |
| 108 | + |
| 109 | + try { |
| 110 | + const response: IStudent[] = await createManyStudents(dto); |
| 111 | + |
| 112 | + enqueueSnackbar(`${response.length} Studierende wurden erstellt.`, { |
| 113 | + variant: 'success', |
| 114 | + }); |
| 115 | + return true; |
| 116 | + } catch (errors) { |
| 117 | + if (Array.isArray(errors)) { |
| 118 | + enqueueSnackbarWithList({ |
| 119 | + title: 'Studierende konnten nicht erstellt werden.', |
| 120 | + textBeforeList: |
| 121 | + 'Da bei einigen Studierenden ein Fehler aufgetreten ist, wurde kein/e Studierende erstellt. Folgende Studierende konnte nicht erstellt werden:', |
| 122 | + items: errors, |
| 123 | + variant: 'error', |
| 124 | + }); |
| 125 | + } else { |
| 126 | + enqueueSnackbar(`Es konnten keine Studierenden erstellt werden.`, { |
| 127 | + variant: 'error', |
| 128 | + }); |
| 129 | + } |
| 130 | + return false; |
| 131 | + } |
| 132 | + }; |
| 133 | + |
| 134 | + return ( |
| 135 | + <Formik initialValues={initialValues} onSubmit={handleSubmit}> |
| 136 | + <AdjustImportedUserDataFormContent /> |
| 137 | + </Formik> |
| 138 | + ); |
| 139 | +} |
| 140 | + |
| 141 | +export default AdjustImportedStudentDataForm; |
0 commit comments