connect groups endpoint's to frontend

This commit is contained in:
Patryk Drzewiński 2022-06-13 19:47:24 +02:00
parent dfd1171c53
commit 0a04a86a00
4 changed files with 135 additions and 45 deletions

View File

@ -39,14 +39,16 @@ def create_group(data: dict) -> dict:
project_supervisor_id = data['project_supervisor_id'] project_supervisor_id = data['project_supervisor_id']
# can assign a new group to project_supervisor # can assign a new group to project_supervisor
result = db.session.query(ProjectSupervisor.count_groups - db.func.count(ProjectSupervisor.id)). \
join(Group).filter(ProjectSupervisor.id == project_supervisor_id). \
group_by(ProjectSupervisor.id).scalar()
if result is None: # nie działa to poprawnie
abort(400, "Project Supervisor doesnt exist") # result = db.session.query(ProjectSupervisor.count_groups - db.func.count(ProjectSupervisor.id)). \
elif result <= 0: # join(Group).filter(ProjectSupervisor.id == project_supervisor_id). \
abort(400, "Can't create new group, project supervisor achieved a limit of groups") # group_by(ProjectSupervisor.id).scalar()
# if result is None:
# abort(400, "Project Supervisor doesnt exist")
# elif result <= 0:
# abort(400, "Can't create new group, project supervisor achieved a limit of groups")
group = Group(name=name, project_supervisor_id=project_supervisor_id) group = Group(name=name, project_supervisor_id=project_supervisor_id)

View File

@ -0,0 +1,50 @@
import axiosInstance from './axiosInstance'
import { Leader } from './leaders'
import { Student } from './students'
type OrderType = 'asc' | 'desc'
interface GroupResponse {
max_pages: number
groups: Group[]
}
export interface Group {
id: number
name: string
project_supervisor: Leader
points_for_first_term: number
points_for_second_term: number
}
export interface GroupPost {
name: string
project_supervisor_id: number
students: number[]
}
export const getGroups = (
params: Partial<{
name: string
page: number
per_page: number
}> = {},
) =>
axiosInstance.get<GroupResponse>(
'http://127.0.0.1:5000/api/coordinator/groups',
{ params },
)
export const createGroup = (payload: Group) =>
axiosInstance.post('http://127.0.0.1:5000/api/coordinator/groups/', payload)
export const uploadGroups = (payload: FormData) =>
axiosInstance.post(
'http://127.0.0.1:5000/api/coordinator/groups/upload/',
payload,
)
export const deleteGroup = (payload: Number) =>
axiosInstance.delete(
'http://127.0.0.1:5000/api/coordinator/groups/'+payload.toString()+'/',
)

View File

@ -1,9 +1,11 @@
import { useState } from 'react' import { useState } from 'react'
import { Controller, useForm } from 'react-hook-form' import { Controller, useForm } from 'react-hook-form'
import { useQuery } from 'react-query' import { useQuery, useMutation } from 'react-query'
import { getStudents } from '../../api/students' import { getStudents } from '../../api/students'
import InputError from '../../components/InputError' import InputError from '../../components/InputError'
import Select from 'react-select' import Select from 'react-select'
import { getLeaders } from '../../api/leaders'
import { createGroup, Group } from '../../api/groups'
type SelectValue = { type SelectValue = {
value: string | number value: string | number
@ -20,13 +22,25 @@ const AddGroup = () => {
control, control,
} = useForm<{ } = useForm<{
name: string name: string
supervisor: any project_supervisor_id: any
students: any students: any
}>({ }>({
mode: 'onBlur', mode: 'onBlur',
}) })
const { mutate: mutateCreateGroup } = useMutation(
'createStudent',
(payload: any) => createGroup(payload),
{
onSuccess: () => {
reset()
setIsAlertVisible(true)
},
},
)
const onSubmit = (data: any) => { const onSubmit = (data: any) => {
data.project_supervisor_id = data.project_supervisor_id.value
data.students = data.students.map((st: any) => st.value)
mutateCreateGroup(data)
console.log(data) console.log(data)
} }
@ -49,16 +63,26 @@ const AddGroup = () => {
}, },
) )
const supervisorOptions = [ const [leadersOptions, setLeadersOptions] = useState<SelectValue[]>([])
{ value: '1', label: 'Patryk Żywica' },
{ value: '2', label: 'Krzysztof Krzywdziński' },
{ value: '3', label: 'Krzysztof Dyczkowski' },
{ value: '4', label: 'Rafał Witkowski' },
{ value: '5', label: 'Wojciech Wawrzyniak' },
{ value: '6', label: 'Marcin Witkowski' },
]
if (areStudentsLoading) { const { isLoading: areLeadersLoading } = useQuery(
'project_supervisors',
() => getLeaders({ per_page: 1000 }),
{
onSuccess: (data) => {
setLeadersOptions(
data?.data.project_supervisors.map(({ id, first_name, last_name }) => {
return {
value: id,
label: `${first_name} ${last_name}`,
}
}),
)
},
},
)
if (areStudentsLoading || areLeadersLoading) {
return <>Ładowanie</> return <>Ładowanie</>
} }
return ( return (
@ -87,16 +111,16 @@ const AddGroup = () => {
)} )}
</div> </div>
<div className="form-control"> <div className="form-control">
<label className="label" htmlFor="supervisor"> <label className="label" htmlFor="project_supervisor_id">
Opiekun Opiekun
</label> </label>
<Controller <Controller
control={control} control={control}
name="supervisor" name="project_supervisor_id"
rules={{ required: true }} rules={{ required: true }}
render={({ field: { onChange, onBlur } }) => ( render={({ field: { onChange, onBlur } }) => (
<Select <Select
options={supervisorOptions} options={leadersOptions}
placeholder="Wybierz opiekuna" placeholder="Wybierz opiekuna"
onChange={(val) => onChange(val)} onChange={(val) => onChange(val)}
onBlur={onBlur} onBlur={onBlur}
@ -110,7 +134,7 @@ const AddGroup = () => {
/> />
)} )}
/> />
{errors.supervisor?.type === 'required' && ( {errors.project_supervisor_id?.type === 'required' && (
<InputError>Wybierz opiekuna</InputError> <InputError>Wybierz opiekuna</InputError>
)} )}
</div> </div>

View File

@ -1,27 +1,44 @@
import { useEffect, useState } from 'react'
import { useQuery } from 'react-query'
import { useNavigate } from 'react-router-dom' import { useNavigate } from 'react-router-dom'
import { getGroups, deleteGroup } from '../../api/groups'
const Groups = () => { const Groups = () => {
let navigate = useNavigate() let navigate = useNavigate()
const [page, setPage] = useState(1)
const [perPage, setPerPage] = useState(10)
const data = [ const perPageOptions = [
{ {
id: 1, value: 10,
name: 'Zarządzanie budżetem', label: '10 rekordów',
project_supervisor: 'Wojciech Wawrzyniak',
members_count: 3,
first_term: 0,
second_term: 0,
}, },
{ {
id: 2, value: 20,
name: 'GatherUp', label: '20 rekordów',
project_supervisor: 'Wojciech Wawrzyniak', },
members_count: 4, {
first_term: 0, value: 50,
second_term: 0, label: '50 rekordów',
},
{
value: 1000,
label: 'Pokaż wszystkie',
}, },
] ]
const {
isLoading: isGroupsLoading,
data: groups,
refetch: refetchGroups,
} = useQuery(['groups', page, perPage], () =>
getGroups({ page, per_page: perPage }),
)
useEffect(() => {
setPage(1)
}, [perPage])
return ( return (
<div> <div>
<button <button
@ -36,27 +53,24 @@ const Groups = () => {
<tr className="bg-gray-50"> <tr className="bg-gray-50">
<th>Nazwa</th> <th>Nazwa</th>
<th>Opiekun</th> <th>Opiekun</th>
<th>Liczba osób</th>
<th>Semestr 1</th> <th>Semestr 1</th>
<th>Semestr 2</th> <th>Semestr 2</th>
</tr> </tr>
</thead> </thead>
<tbody className="divide-y divide-gray-100"> <tbody className="divide-y divide-gray-100">
{data.map( {groups?.data?.groups.map(
({ ({
id, id,
name, name,
project_supervisor, project_supervisor,
members_count, points_for_first_term,
first_term, points_for_second_term,
second_term,
}) => ( }) => (
<tr key={id}> <tr key={id}>
<td>{name}</td> <td>{name}</td>
<td>{project_supervisor}</td> <td>{project_supervisor.first_name + ' ' + project_supervisor.last_name}</td>
<td>{members_count}</td> <td>{points_for_first_term}</td>
<td>{first_term}</td> <td>{points_for_second_term}</td>
<td>{second_term}</td>
</tr> </tr>
), ),
)} )}