From 22815bb405cced509e990c31974716b84b7ec4a1 Mon Sep 17 00:00:00 2001 From: adam-skowronek Date: Thu, 17 Nov 2022 22:21:59 +0100 Subject: [PATCH] Add creating year groups --- frontend/src/App.tsx | 3 +- frontend/src/api/students.ts | 4 +- frontend/src/api/yearGroups.ts | 3 + frontend/src/views/coordinator/AddStudent.tsx | 11 ++- .../src/views/coordinator/Coordinator.tsx | 1 + frontend/src/views/coordinator/Home.tsx | 92 +++++++++++++++++++ frontend/src/views/coordinator/Schedules.tsx | 1 - 7 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 frontend/src/views/coordinator/Home.tsx diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index ec47ce0..07077de 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -20,6 +20,7 @@ import SupervisorSchedules from './views/supervisor/SupervisorSchedules' import StudentSchedules from './views/student/StudentSchedules' import StudentSchedule from './views/student/StudentSchedule' import SupervisorSchedule from './views/supervisor/SupervisorSchedule' +import Home from './views/coordinator/Home' const queryClient = new QueryClient({ defaultOptions: { @@ -36,7 +37,7 @@ function App() { } /> }> - } /> + } /> } /> } /> } /> diff --git a/frontend/src/api/students.ts b/frontend/src/api/students.ts index d4f8ef3..2de2b3a 100644 --- a/frontend/src/api/students.ts +++ b/frontend/src/api/students.ts @@ -12,7 +12,7 @@ export interface Student { last_name: string index: number pesel: string - mode: boolean + mode?: boolean group?: any } @@ -32,7 +32,7 @@ export const getStudents = ( { params }, ) -export const createStudent = (payload: Student) => +export const createStudent = (payload: Student & { year_group_id: number }) => axiosInstance.post('coordinator/students/', payload) export const uploadStudents = (payload: FormData) => diff --git a/frontend/src/api/yearGroups.ts b/frontend/src/api/yearGroups.ts index 1599d4a..6df40b1 100644 --- a/frontend/src/api/yearGroups.ts +++ b/frontend/src/api/yearGroups.ts @@ -23,3 +23,6 @@ export const getYearGroups = ( params, }, ) + +export const createYearGroup = (payload: CreateYearGroup) => + axiosInstance.post('coordinator/year-group/', payload) diff --git a/frontend/src/views/coordinator/AddStudent.tsx b/frontend/src/views/coordinator/AddStudent.tsx index b6374da..832ccb1 100644 --- a/frontend/src/views/coordinator/AddStudent.tsx +++ b/frontend/src/views/coordinator/AddStudent.tsx @@ -1,6 +1,7 @@ import { useState } from 'react' import { useForm } from 'react-hook-form' import { useMutation } from 'react-query' +import useLocalStorageState from 'use-local-storage-state' import { createStudent, Student } from '../../api/students' import InputError from '../../components/InputError' @@ -12,10 +13,11 @@ const AddStudent = () => { formState: { errors }, reset, } = useForm({ mode: 'onBlur' }) + const [yearGroupId] = useLocalStorageState('yearGroupId') const { mutate: mutateCreateStudent } = useMutation( 'createStudent', - (payload: Student) => createStudent(payload), + (payload: Student & { year_group_id: number }) => createStudent(payload), { onSuccess: () => { reset() @@ -25,7 +27,8 @@ const AddStudent = () => { ) const onSubmit = (data: Student) => { - mutateCreateStudent(data) + delete data.mode + mutateCreateStudent({ ...data, year_group_id: Number(yearGroupId) }) } return ( @@ -104,7 +107,7 @@ const AddStudent = () => { Pesel musi mieć 11 cyfr )} -
+ {/*
{ {errors.mode?.type === 'required' && ( Wybierz tryb studiów )} -
+
*/} ) diff --git a/frontend/src/views/coordinator/Coordinator.tsx b/frontend/src/views/coordinator/Coordinator.tsx index f7a5663..54015c9 100644 --- a/frontend/src/views/coordinator/Coordinator.tsx +++ b/frontend/src/views/coordinator/Coordinator.tsx @@ -6,6 +6,7 @@ const Coordinator = () => { <> { + const { register, handleSubmit } = useForm<{ + name: string + mode: string + }>({ + mode: 'onBlur', + }) + + const { data: yearGroups, refetch } = useQuery(['yearGroups'], () => + getYearGroups({ per_page: 100 }), + ) + + const { mutate: mutateCreateYearGroup } = useMutation( + ['createYearGroup'], + (payload: CreateYearGroup) => createYearGroup(payload), + { + onSuccess: () => { + refetch() + }, + }, + ) + const onSubmit = (data: any) => { + mutateCreateYearGroup(data) + } + + return ( +
+
+
+ + +
+ +
+ + +
+
+ + +
+
+
+ +
+

Grupy:

+ {yearGroups && + yearGroups?.data?.year_groups.map((yearGroup) => ( +

+ - {yearGroup.name} +

+ ))} +
+ ) +} + +export default Home diff --git a/frontend/src/views/coordinator/Schedules.tsx b/frontend/src/views/coordinator/Schedules.tsx index 7b6673b..5bb5b66 100644 --- a/frontend/src/views/coordinator/Schedules.tsx +++ b/frontend/src/views/coordinator/Schedules.tsx @@ -91,7 +91,6 @@ const Schedules = () => { > {schedule.title} - 1 ))}