Add creating year groups

This commit is contained in:
adam-skowronek 2022-11-17 22:21:59 +01:00
parent 9c7ae692f5
commit 22815bb405
7 changed files with 107 additions and 8 deletions

View File

@ -20,6 +20,7 @@ import SupervisorSchedules from './views/supervisor/SupervisorSchedules'
import StudentSchedules from './views/student/StudentSchedules' import StudentSchedules from './views/student/StudentSchedules'
import StudentSchedule from './views/student/StudentSchedule' import StudentSchedule from './views/student/StudentSchedule'
import SupervisorSchedule from './views/supervisor/SupervisorSchedule' import SupervisorSchedule from './views/supervisor/SupervisorSchedule'
import Home from './views/coordinator/Home'
const queryClient = new QueryClient({ const queryClient = new QueryClient({
defaultOptions: { defaultOptions: {
@ -36,7 +37,7 @@ function App() {
<Routes> <Routes>
<Route index element={<Login />} /> <Route index element={<Login />} />
<Route path="coordinator" element={<Coordinator />}> <Route path="coordinator" element={<Coordinator />}>
<Route index element={<Navigate to="students" />} /> <Route index element={<Home />} />
<Route path="groups" element={<Groups />} /> <Route path="groups" element={<Groups />} />
<Route path="students" element={<Students />} /> <Route path="students" element={<Students />} />
<Route path="leaders" element={<Leaders />} /> <Route path="leaders" element={<Leaders />} />

View File

@ -12,7 +12,7 @@ export interface Student {
last_name: string last_name: string
index: number index: number
pesel: string pesel: string
mode: boolean mode?: boolean
group?: any group?: any
} }
@ -32,7 +32,7 @@ export const getStudents = (
{ params }, { params },
) )
export const createStudent = (payload: Student) => export const createStudent = (payload: Student & { year_group_id: number }) =>
axiosInstance.post('coordinator/students/', payload) axiosInstance.post('coordinator/students/', payload)
export const uploadStudents = (payload: FormData) => export const uploadStudents = (payload: FormData) =>

View File

@ -23,3 +23,6 @@ export const getYearGroups = (
params, params,
}, },
) )
export const createYearGroup = (payload: CreateYearGroup) =>
axiosInstance.post('coordinator/year-group/', payload)

View File

@ -1,6 +1,7 @@
import { useState } from 'react' import { useState } from 'react'
import { useForm } from 'react-hook-form' import { useForm } from 'react-hook-form'
import { useMutation } from 'react-query' import { useMutation } from 'react-query'
import useLocalStorageState from 'use-local-storage-state'
import { createStudent, Student } from '../../api/students' import { createStudent, Student } from '../../api/students'
import InputError from '../../components/InputError' import InputError from '../../components/InputError'
@ -12,10 +13,11 @@ const AddStudent = () => {
formState: { errors }, formState: { errors },
reset, reset,
} = useForm<Student>({ mode: 'onBlur' }) } = useForm<Student>({ mode: 'onBlur' })
const [yearGroupId] = useLocalStorageState('yearGroupId')
const { mutate: mutateCreateStudent } = useMutation( const { mutate: mutateCreateStudent } = useMutation(
'createStudent', 'createStudent',
(payload: Student) => createStudent(payload), (payload: Student & { year_group_id: number }) => createStudent(payload),
{ {
onSuccess: () => { onSuccess: () => {
reset() reset()
@ -25,7 +27,8 @@ const AddStudent = () => {
) )
const onSubmit = (data: Student) => { const onSubmit = (data: Student) => {
mutateCreateStudent(data) delete data.mode
mutateCreateStudent({ ...data, year_group_id: Number(yearGroupId) })
} }
return ( return (
@ -104,7 +107,7 @@ const AddStudent = () => {
<InputError>Pesel musi mieć 11 cyfr</InputError> <InputError>Pesel musi mieć 11 cyfr</InputError>
)} )}
</div> </div>
<div className="form-control gap-2"> {/* <div className="form-control gap-2">
<label className="label">Tryb studiów</label> <label className="label">Tryb studiów</label>
<div className="flex gap-2"> <div className="flex gap-2">
<input <input
@ -133,7 +136,7 @@ const AddStudent = () => {
{errors.mode?.type === 'required' && ( {errors.mode?.type === 'required' && (
<InputError>Wybierz tryb studiów</InputError> <InputError>Wybierz tryb studiów</InputError>
)} )}
</div> </div> */}
<button className="btn btn-success mt-4">Dodaj studenta</button> <button className="btn btn-success mt-4">Dodaj studenta</button>
</form> </form>
) )

View File

@ -6,6 +6,7 @@ const Coordinator = () => {
<> <>
<TopBar <TopBar
routes={[ routes={[
{ name: 'Home', path: '/coordinator/' },
{ name: 'Grupy', path: '/coordinator/groups' }, { name: 'Grupy', path: '/coordinator/groups' },
{ name: 'Studenci', path: '/coordinator/students' }, { name: 'Studenci', path: '/coordinator/students' },
{ name: 'Opiekunowie', path: '/coordinator/leaders' }, { name: 'Opiekunowie', path: '/coordinator/leaders' },

View File

@ -0,0 +1,92 @@
import { useForm } from 'react-hook-form'
import { useMutation, useQuery } from 'react-query'
import {
createYearGroup,
CreateYearGroup,
getYearGroups,
} from '../../api/yearGroups'
const Home = () => {
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 (
<div>
<form
className="w-full lg:w-1/4 flex flex-col mb-5"
onSubmit={handleSubmit(onSubmit)}
>
<div className="form-control">
<label className="label" htmlFor="title">
Nazwa
</label>
<input
className="input input-bordered"
id="title"
type="text"
{...register('name', { required: true })}
/>
<div className="form-control gap-2">
<label className="label">Tryb studiów</label>
<div className="flex gap-2">
<input
className="radio"
id="mode-1"
type="radio"
{...register('mode', {
required: true,
})}
value="s"
checked
/>
<label htmlFor="mode-1">Stacjonarny</label>
</div>
<div className="flex gap-2">
<input
className="radio"
id="mode-0"
type="radio"
{...register('mode', {
required: true,
})}
value="n"
/>
<label htmlFor="mode-0">Niestacjonarny</label>
</div>
</div>
</div>
<button className="btn btn-success mt-4">Stwórz grupę roku</button>
</form>
<h2 className="text-2xl font-bold mb-2">Grupy:</h2>
{yearGroups &&
yearGroups?.data?.year_groups.map((yearGroup) => (
<h3 className="text-xl " key={yearGroup.name}>
- {yearGroup.name}
</h3>
))}
</div>
)
}
export default Home

View File

@ -91,7 +91,6 @@ const Schedules = () => {
> >
{schedule.title} {schedule.title}
</Link> </Link>
1
</h3> </h3>
))} ))}
</div> </div>