Add leader to year group after create

This commit is contained in:
adam-skowronek 2022-12-16 01:31:36 +01:00
parent 817095cc3f
commit 25355be9ff
4 changed files with 37 additions and 9 deletions

View File

@ -69,7 +69,7 @@ def create_project_supervisor(data: dict) -> dict:
db.session.add(project_supervisor) db.session.add(project_supervisor)
db.session.commit() db.session.commit()
return {"message": "Project Supervisor was created!"} return {"message": "Project Supervisor was created!", "id": project_supervisor.id}
@bp.get("/<int:id>/detail") @bp.get("/<int:id>/detail")

View File

@ -52,7 +52,7 @@ class StudentEditSchema(ma.Schema):
class MessageSchema(ma.Schema): class MessageSchema(ma.Schema):
message = fields.Str(required=True) message = fields.Str(required=True)
id = fields.Str(required=False)
class FileSchema(ma.Schema): class FileSchema(ma.Schema):
file = fields.Raw(type='file', required=True) file = fields.Raw(type='file', required=True)

View File

@ -12,7 +12,7 @@ export interface Leader {
first_name: string first_name: string
last_name: string last_name: string
email: string email: string
limit_group: number limit_group?: number
count_groups: number count_groups: number
} }
@ -36,3 +36,13 @@ export const createLeader = (payload: Partial<Leader>) =>
export const deleteLeader = (id: number) => export const deleteLeader = (id: number) =>
axiosInstance.delete(`coordinator/project_supervisor/${id}/`) axiosInstance.delete(`coordinator/project_supervisor/${id}/`)
export const addLeaderToGroup = (
id: number,
year_group_id: number,
payload: { limit_group: number },
) =>
axiosInstance.post(
`coordinator/project_supervisor/${id}/year-group/${year_group_id}`,
payload,
)

View File

@ -1,7 +1,8 @@
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 { createLeader, Leader } from '../../api/leaders' import useLocalStorageState from 'use-local-storage-state'
import { addLeaderToGroup, createLeader, Leader } from '../../api/leaders'
import InputError from '../../components/InputError' import InputError from '../../components/InputError'
const AddLeader = () => { const AddLeader = () => {
@ -11,18 +12,35 @@ const AddLeader = () => {
handleSubmit, handleSubmit,
formState: { errors }, formState: { errors },
reset, reset,
getValues,
} = useForm<Leader>() } = useForm<Leader>()
const [yearGroupId] = useLocalStorageState('yearGroupId')
const { mutate: mutateCreateLeader } = useMutation( const { mutate: mutateCreateLeader } = useMutation(
'createLeader', 'createLeader',
(payload: Leader) => createLeader(payload), (payload: Leader) => {
delete payload.limit_group
return createLeader(payload)
},
{ {
onSuccess: () => { onSuccess: (data) => {
reset()
setIsAlertVisible(true) setIsAlertVisible(true)
mutateAddToYearGroup({
id: data?.data?.id,
year_group_id: Number(yearGroupId),
limit_group: getValues('limit_group') ?? 0,
})
reset()
}, },
}, },
) )
const { mutate: mutateAddToYearGroup } = useMutation(
'addLeaderToGroup',
(payload: { id: any; year_group_id: number; limit_group: number }) =>
addLeaderToGroup(payload.id, payload.year_group_id, {
limit_group: payload.limit_group,
}),
)
const onSubmit = (data: Leader) => { const onSubmit = (data: Leader) => {
mutateCreateLeader(data) mutateCreateLeader(data)
@ -83,7 +101,7 @@ const AddLeader = () => {
<InputError>Email jest wymagany</InputError> <InputError>Email jest wymagany</InputError>
)} )}
</div> </div>
{/* <div className="form-control"> <div className="form-control">
<label className="label" htmlFor="limit_group"> <label className="label" htmlFor="limit_group">
Limit grup Limit grup
</label> </label>
@ -99,7 +117,7 @@ const AddLeader = () => {
{errors.limit_group?.type === 'pattern' && ( {errors.limit_group?.type === 'pattern' && (
<InputError>Limit grup musi być liczbą dodatnią</InputError> <InputError>Limit grup musi być liczbą dodatnią</InputError>
)} )}
</div> */} </div>
<button className="btn btn-success mt-4">Dodaj opiekuna</button> <button className="btn btn-success mt-4">Dodaj opiekuna</button>
</form> </form>
) )