Update schedule components

This commit is contained in:
adam-skowronek 2022-11-24 20:00:09 +01:00
parent dd116962e6
commit ee8563fedb
7 changed files with 91 additions and 32 deletions

View File

@ -1,4 +1,5 @@
import axiosInstance from './axiosInstance' import axiosInstance from './axiosInstance'
import { Student } from './students'
interface TermOfDefences { interface TermOfDefences {
id: number id: number
@ -8,7 +9,7 @@ interface TermOfDefences {
members_of_committee: { members_of_committee: {
members: { first_name: string; last_name: string }[] members: { first_name: string; last_name: string }[]
} }
group: { name: string } group: { name: string; students: Student[] }
} }
export const getTermsOfDefences = (scheduleId: number) => { export const getTermsOfDefences = (scheduleId: number) => {
@ -16,6 +17,13 @@ export const getTermsOfDefences = (scheduleId: number) => {
term_of_defences: TermOfDefences[] term_of_defences: TermOfDefences[]
}>(`coordinator/enrollments/${scheduleId}/term-of-defences/`) }>(`coordinator/enrollments/${scheduleId}/term-of-defences/`)
} }
export const getTermsOfDefencesWithGroups = (scheduleId: number) => {
return axiosInstance.get<{
term_of_defences: TermOfDefences[]
}>(
`coordinator/enrollments/${scheduleId}/assigned-group-to-term-of-defences/`,
)
}
export const getStudentsTermsOfDefences = ( export const getStudentsTermsOfDefences = (
scheduleId: number, scheduleId: number,

View File

@ -89,7 +89,19 @@ const EditSchedule = ({
))} ))}
</ul> </ul>
{eventData.resource.group && ( {eventData.resource.group && (
<p className="mt-4">Grupa: {eventData.resource.group.name}</p> <>
<p className="mt-4">Grupa: {eventData.resource.group.name}</p>
<ul className="list-disc">
{eventData.resource.group.students?.map((member: any) => (
<li
key={`${member.first_name} ${member.last_name}`}
className="ml-4"
>
{member.first_name} {member.last_name}
</li>
))}
</ul>
</>
)} )}
</> </>
) : ( ) : (

View File

@ -6,6 +6,7 @@ import {
createEvent, createEvent,
downloadSchedule, downloadSchedule,
getTermsOfDefences, getTermsOfDefences,
getTermsOfDefencesWithGroups,
setDateOfExaminationSchedule, setDateOfExaminationSchedule,
} from '../../api/schedule' } from '../../api/schedule'
import { useParams } from 'react-router-dom' import { useParams } from 'react-router-dom'
@ -92,36 +93,64 @@ const Schedule = () => {
}, },
) )
const { refetch } = useQuery( const { refetch, isFetching } = useQuery(
['schedules'], ['schedules'],
() => getTermsOfDefences(Number(id)), () => getTermsOfDefences(Number(id)),
{ {
onSuccess: (data) => { onSettled: (data) => {
setEvents( if (data) {
data.data.term_of_defences.map( setEvents(
({ data.data.term_of_defences.map(
id, ({
start_date,
end_date,
title = 'Obrona',
members_of_committee,
group,
}) => {
return {
id, id,
title: `${group?.name ?? '-'}`, start_date,
start: new Date(start_date), end_date,
end: new Date(end_date), title = 'Obrona',
resource: { members_of_committee,
members_of_committee, group,
}, }) => {
} return {
}, id,
), title: `${group?.name ?? '-'}`,
) start: new Date(start_date),
end: new Date(end_date),
resource: {
members_of_committee,
group,
},
}
},
),
)
}
}, },
}, },
) )
const { refetch: refetchWithGroups } = useQuery(
['schedules_groups'],
() => getTermsOfDefencesWithGroups(Number(id)),
{
onSettled: (data) => {
data?.data.term_of_defences.forEach((term) => {
const index = events.findIndex((event) => event.id === term.id)
if (index !== -1) {
const eventsCopy = [...events]
eventsCopy[index] = {
...eventsCopy[index],
resource: {
...eventsCopy[index].resource,
group: term.group,
},
}
setEvents(eventsCopy)
}
})
},
enabled: !isFetching,
},
)
const { mutateAsync: mutateCreateEvent } = useMutation( const { mutateAsync: mutateCreateEvent } = useMutation(
['createEvent'], ['createEvent'],
(data: { (data: {
@ -197,6 +226,7 @@ const Schedule = () => {
project_supervisors: data?.project_supervisors, project_supervisors: data?.project_supervisors,
}) })
refetch() refetch()
refetchWithGroups()
reset() reset()
closeModal() closeModal()
} }

View File

@ -132,7 +132,7 @@ const Students = () => {
</option> </option>
))} ))}
</select> </select>
<label className="label justify-start gap-2"> {/* <label className="label justify-start gap-2">
<input <input
type="radio" type="radio"
className="radio radio-xs md:radio-md" className="radio radio-xs md:radio-md"
@ -149,7 +149,7 @@ const Students = () => {
checked={!mode} checked={!mode}
/> />
<span className="text-xs md:text-base">Niestacjonarni</span> <span className="text-xs md:text-base">Niestacjonarni</span>
</label> </label> */}
<label className="label"> <label className="label">
<input <input
type="checkbox" type="checkbox"
@ -172,7 +172,6 @@ const Students = () => {
<th>Nazwisko</th> <th>Nazwisko</th>
<th>Indeks</th> <th>Indeks</th>
<th>Zapisany</th> <th>Zapisany</th>
<th>Tryb</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
@ -185,7 +184,6 @@ const Students = () => {
<td>{last_name}</td> <td>{last_name}</td>
<td>{index}</td> <td>{index}</td>
<td>{group === null ? 'Nie' : 'Tak'}</td> <td>{group === null ? 'Nie' : 'Tak'}</td>
<td>{mode ? 'Stacjonarny' : 'Niestacjonarny'}</td>
<td> <td>
<button onClick={() => mutateDelete(index)}> <button onClick={() => mutateDelete(index)}>
<IconRemove /> <IconRemove />

View File

@ -8,6 +8,7 @@ import { assignGroup } from '../../api/schedule'
const ScheduleAddGroup = ({ const ScheduleAddGroup = ({
eventData, eventData,
scheduleId, scheduleId,
closeModal,
}: { }: {
eventData: { eventData: {
start: Date start: Date
@ -17,6 +18,7 @@ const ScheduleAddGroup = ({
resource: any resource: any
} }
scheduleId: string scheduleId: string
closeModal: () => void
}) => { }) => {
const { register, handleSubmit, reset, control } = useForm<{ const { register, handleSubmit, reset, control } = useForm<{
student_index: number student_index: number
@ -30,6 +32,11 @@ const ScheduleAddGroup = ({
enrollmentId: number enrollmentId: number
studentIndex: number studentIndex: number
}) => assignGroup(data), }) => assignGroup(data),
{
onSuccess: () => {
closeModal()
},
},
) )
const onSubmit = (data: any) => { const onSubmit = (data: any) => {

View File

@ -187,7 +187,11 @@ const StudentSchedule = () => {
style={customStyles} style={customStyles}
> >
{selectedDate && id ? ( {selectedDate && id ? (
<ScheduleAddGroup eventData={selectedDate} scheduleId={id} /> <ScheduleAddGroup
eventData={selectedDate}
scheduleId={id}
closeModal={() => setIsEditModalOpen(false)}
/>
) : null} ) : null}
</Modal> </Modal>
</div> </div>

View File

@ -239,7 +239,7 @@ const SupervisorSchedule = () => {
<button className="btn btn-success mt-4">Dodaj dostępność</button> <button className="btn btn-success mt-4">Dodaj dostępność</button>
</form> </form>
</Modal> </Modal>
<Modal {/* <Modal
isOpen={isEditModalOpen} isOpen={isEditModalOpen}
onRequestClose={() => setIsEditModalOpen(false)} onRequestClose={() => setIsEditModalOpen(false)}
contentLabel="modal" contentLabel="modal"
@ -248,7 +248,7 @@ const SupervisorSchedule = () => {
{selectedDate && id ? ( {selectedDate && id ? (
<EditSchedule eventData={selectedDate} scheduleId={id} /> <EditSchedule eventData={selectedDate} scheduleId={id} />
) : null} ) : null}
</Modal> </Modal> */}
</div> </div>
) )
} }