-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Fix create, update, and delete schedules
- Loading branch information
Showing
15 changed files
with
345 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,16 @@ | ||
import { useState } from "react"; | ||
|
||
import { useQuery } from "@apollo/client"; | ||
import { Outlet } from "react-router"; | ||
import { useNavigate, useParams } from "react-router-dom"; | ||
|
||
import { Boundary, LoadingIndicator } from "@repo/theme"; | ||
|
||
import { GET_SCHEDULE, GetScheduleResponse, IClass, ISection } from "@/lib/api"; | ||
|
||
import { ScheduleContextType } from "./schedule"; | ||
import { useSchedule } from "@/hooks/schedules/useSchedule"; | ||
import { ScheduleIdentifier } from "@/lib/api"; | ||
|
||
export default function Schedule() { | ||
const { scheduleId } = useParams(); | ||
|
||
const navigate = useNavigate(); | ||
|
||
const { data } = useQuery<GetScheduleResponse>(GET_SCHEDULE, { | ||
const { data: schedule } = useSchedule(scheduleId as ScheduleIdentifier, { | ||
onError: () => navigate("/schedules"), | ||
variables: { id: scheduleId }, | ||
}); | ||
|
||
const [selectedSections, setSelectedSections] = useState<ISection[]>([]); | ||
const [classes, setClasses] = useState<IClass[]>([]); | ||
const [expanded, setExpanded] = useState<boolean[]>([]); | ||
|
||
return data ? ( | ||
<Outlet | ||
context={ | ||
{ | ||
selectedSections, | ||
setSelectedSections, | ||
classes, | ||
setClasses, | ||
expanded, | ||
setExpanded, | ||
} satisfies ScheduleContextType | ||
} | ||
/> | ||
) : ( | ||
<Boundary> | ||
<LoadingIndicator size="lg" /> | ||
</Boundary> | ||
); | ||
return schedule ? schedule.name : <></>; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createContext } from "react"; | ||
|
||
import { ISchedule } from "@/lib/api"; | ||
|
||
export interface ScheduleContextType { | ||
schedule: ISchedule; | ||
} | ||
|
||
const ScheduleContext = createContext<ScheduleContextType | null>(null); | ||
|
||
export default ScheduleContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useCallback } from "react"; | ||
|
||
import { gql, useMutation } from "@apollo/client"; | ||
|
||
import { | ||
CREATE_SCHEDULE, | ||
CreateScheduleResponse, | ||
IScheduleInput, | ||
} from "@/lib/api"; | ||
|
||
export const useCreateSchedule = () => { | ||
const mutation = useMutation<CreateScheduleResponse>(CREATE_SCHEDULE, { | ||
update(cache, { data }) { | ||
const schedule = data?.createSchedule; | ||
|
||
if (!schedule) return; | ||
|
||
cache.modify({ | ||
fields: { | ||
schedules: (existingSchedules = []) => { | ||
const reference = cache.writeFragment({ | ||
data: schedule, | ||
fragment: gql` | ||
fragment CreatedSchedule on Schedule { | ||
_id | ||
name | ||
public | ||
createdBy | ||
year | ||
semester | ||
classes { | ||
class { | ||
subject | ||
courseNumber | ||
number | ||
} | ||
selectedSections | ||
} | ||
} | ||
`, | ||
}); | ||
|
||
return [...existingSchedules, reference]; | ||
}, | ||
}, | ||
}); | ||
}, | ||
}); | ||
|
||
const createSchedule = useCallback( | ||
async (schedule: IScheduleInput) => { | ||
const mutate = mutation[0]; | ||
|
||
return await mutate({ variables: { schedule } }); | ||
}, | ||
[mutation] | ||
); | ||
|
||
return [createSchedule, mutation[1]] as [ | ||
mutate: typeof createSchedule, | ||
result: (typeof mutation)[1], | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useCallback } from "react"; | ||
|
||
import { useMutation } from "@apollo/client"; | ||
|
||
import { | ||
DELETE_SCHEDULE, | ||
DeleteScheduleResponse, | ||
ScheduleIdentifier, | ||
} from "@/lib/api"; | ||
|
||
export const useDeleteSchedule = () => { | ||
const mutation = useMutation<DeleteScheduleResponse>(DELETE_SCHEDULE, { | ||
update(cache, { data }) { | ||
const id = data?.deleteSchedule; | ||
|
||
if (!id) return; | ||
|
||
cache.modify({ | ||
fields: { | ||
schedules: (existingSchedules = [], { readField }) => | ||
existingSchedules.filter( | ||
(reference: any) => readField("_id", reference) !== id | ||
), | ||
}, | ||
}); | ||
}, | ||
}); | ||
|
||
const deleteSchedule = useCallback( | ||
async (id: ScheduleIdentifier) => { | ||
const mutate = mutation[0]; | ||
|
||
return await mutate({ variables: { id } }); | ||
}, | ||
[mutation] | ||
); | ||
|
||
return [deleteSchedule, mutation[1]] as [ | ||
mutate: typeof deleteSchedule, | ||
result: (typeof mutation)[1], | ||
]; | ||
}; |
Oops, something went wrong.