77 createViewWeek ,
88 viewWeek ,
99} from "@schedule-x/calendar" ;
10- import { createDragAndDropPlugin } from "@schedule-x/drag-and-drop" ;
10+ // import { createDragAndDropPlugin } from "@schedule-x/drag-and-drop";
1111import { createEventModalPlugin } from "@schedule-x/event-modal" ;
1212import "@schedule-x/theme-default/dist/index.css" ;
1313import { Button } from "@/components/ui/button" ;
@@ -42,18 +42,21 @@ import {
4242 useGetEventsQuery ,
4343 useDeleteEventMutation ,
4444} from "@/api/eventsApiSlice" ;
45+ import { useGetOfferingsQuery } from "@/api/offeringsApiSlice" ;
4546import { useGetOfferingEventsQuery } from "@/api/offeringsApiSlice" ;
4647import { useNavigate , useSearchParams } from "react-router-dom" ;
4748import {
4849 Event ,
4950 Timetable ,
5051 TimetableEvents ,
5152 Restriction ,
53+ Offering ,
5254} from "@/utils/type-utils" ;
5355import { TimetableForm } from "@/models/timetable-form" ;
5456import { getSemesterStartAndEndDates } from "@/utils/semester-utils" ;
5557
5658interface CalendarProps {
59+ setShowLoadingPage : React . Dispatch < React . SetStateAction < boolean > > ;
5760 isChoosingSectionsManually : boolean ;
5861 semester : string ;
5962 selectedCourses : TimetableForm [ "courses" ] ;
@@ -83,6 +86,7 @@ function parseEvent(id: number, event: Event, calendarId: string) {
8386
8487const Calendar = React . memo < CalendarProps > (
8588 ( {
89+ setShowLoadingPage,
8690 semester,
8791 selectedCourses,
8892 newOfferingIds,
@@ -105,6 +109,10 @@ const Calendar = React.memo<CalendarProps>(
105109 const semesterStartDate = getSemesterStartAndEndDates ( semester ) . start ;
106110 const semesterEndDate = getSemesterStartAndEndDates ( semester ) . end ;
107111
112+ const { data : offeringsData } = useGetOfferingsQuery ( { } ) as {
113+ data : Offering [ ] ;
114+ } ;
115+
108116 const { data : courseEventsData } = useGetOfferingEventsQuery ( {
109117 offering_ids : newOfferingIds . join ( "," ) ,
110118 semester_start_date : semesterStartDate ,
@@ -196,12 +204,13 @@ const Calendar = React.memo<CalendarProps>(
196204 } ,
197205 ) ;
198206
199- const totalNumberOfSections = ! selectedCourses . length
207+ const totalNumberOfRequiredSections = ! selectedCourses . length
200208 ? 0
201209 : ( numberOfSectionsData ?. totalNumberOfCourseSections ?? 0 ) ;
202-
203- const allOfferingSectionsHaveBeenSelected =
204- newOfferingIds . length === totalNumberOfSections ;
210+ const totalNumberOfSelectedSections = [ ...new Set ( offeringsData ?. filter (
211+ ( offering ) => newOfferingIds . includes ( offering . id ) ,
212+ ) . map ( ( offering ) => `${ offering . code } ${ offering . offering } ${ offering . meeting_section } ` ) ) ] . length ;
213+ const allOfferingSectionsHaveBeenSelected = totalNumberOfSelectedSections === totalNumberOfRequiredSections ;
205214
206215 useEffect ( ( ) => {
207216 if ( ! isEditingTimetable ) {
@@ -211,7 +220,6 @@ const Calendar = React.memo<CalendarProps>(
211220
212221 const handleCreate = async ( ) => {
213222 const timetableTitle = timetableTitleRef . current ?. value ?? "" ;
214-
215223 // Create timetable
216224 const { data, error } = await createTimetable ( {
217225 timetable_title : timetableTitle ,
@@ -221,7 +229,6 @@ const Calendar = React.memo<CalendarProps>(
221229 console . error ( error ) ;
222230 return ;
223231 }
224-
225232 // Create course events for the newly created timetable
226233 const newTimetableId = data ?. id ;
227234 for ( const offeringId of newOfferingIds ) {
@@ -235,7 +242,6 @@ const Calendar = React.memo<CalendarProps>(
235242 console . error ( offeringError ) ;
236243 }
237244 }
238-
239245 // Create restrictions for the newly created timetable
240246 for ( const restriction of restrictions ) {
241247 const restrictionObject = {
@@ -253,19 +259,18 @@ const Calendar = React.memo<CalendarProps>(
253259 console . error ( restrictionError ) ;
254260 }
255261 }
256-
257262 // Redirect to the home page to see the newly created timetable
258263 navigate ( "/home" ) ;
259264 } ;
260265
261266 const handleUpdate = async ( ) => {
267+ setShowLoadingPage ( true ) ;
262268 const offeringIdsToDelete = oldOfferingIds . filter (
263269 ( offeringId ) => ! newOfferingIds . includes ( offeringId ) ,
264270 ) ;
265271 const offeringIdsToAdd = newOfferingIds . filter (
266272 ( offeringId ) => ! oldOfferingIds . includes ( offeringId ) ,
267273 ) ;
268-
269274 // Delete course events
270275 for ( const offeringId of offeringIdsToDelete ) {
271276 const { error : deleteError } = await deleteEvent ( {
@@ -278,7 +283,6 @@ const Calendar = React.memo<CalendarProps>(
278283 console . error ( deleteError ) ;
279284 }
280285 }
281-
282286 // Create course events
283287 for ( const offeringId of offeringIdsToAdd ) {
284288 const { error : createError } = await createEvent ( {
@@ -291,9 +295,7 @@ const Calendar = React.memo<CalendarProps>(
291295 console . error ( createError ) ;
292296 }
293297 }
294-
295298 form . setValue ( "offeringIds" , newOfferingIds ) ;
296-
297299 // Delete restrictions
298300 for ( const restriction of oldRestrictions ) {
299301 const { error : deleteError } = await deleteRestriction ( {
@@ -304,7 +306,6 @@ const Calendar = React.memo<CalendarProps>(
304306 console . error ( deleteError ) ;
305307 }
306308 }
307-
308309 // Create restrictions
309310 for ( const restriction of restrictions ) {
310311 const restrictionObject = {
@@ -322,7 +323,6 @@ const Calendar = React.memo<CalendarProps>(
322323 console . error ( restrictionError ) ;
323324 }
324325 }
325-
326326 navigate ( "/home" ) ;
327327 } ;
328328
0 commit comments