@@ -20,6 +20,7 @@ import { getCourses, getCoursesByKd } from "services/api";
2020import { clearSchedule } from "redux/modules/schedules" ;
2121import SelectedCourses from "containers/SelectedCourses" ;
2222import { BauhausSide } from "components/Bauhaus" ;
23+ import { useSchedulePersistence } from "hooks/useSchedulePersistence" ; // Import the custom hook
2324import Checkout from "./Checkout" ;
2425import Course from "./Course" ;
2526import Detail from "./Detail" ;
@@ -39,6 +40,10 @@ function BuildSchedule() {
3940 const isMobile = useSelector ( ( state ) => state . appState . isMobile ) ;
4041 const auth = useSelector ( ( state ) => state . auth ) ;
4142 const dispatch = useDispatch ( ) ;
43+
44+ const { saveSchedulesToSessionStorage, restoreSchedulesFromSessionStorage } =
45+ useSchedulePersistence ( ) ;
46+
4247 const [ majorSelected , setMajorSelected ] = useState ( ) ;
4348 const [ detailData , setDetailData ] = useState ( null ) ;
4449 const [ courses , setCourses ] = useState ( null ) ;
@@ -50,47 +55,105 @@ function BuildSchedule() {
5055 const theme = useColorModeValue ( "light" , "dark" ) ;
5156 const isInitialMount = useRef ( true ) ;
5257
58+ const fetchedMajorId = useRef ( null ) ;
59+ const fetchedMajorSelected = useRef ( null ) ;
60+ const hasInitialData = useRef ( false ) ;
61+ const coursesLoaded = useRef ( false ) ;
62+
5363 const fetchCourses = useCallback (
54- async ( majorId , majorSelected ) => {
64+ async ( majorId , majorSelected , shouldClearSchedule = false ) => {
65+ const isSameMajor = fetchedMajorId . current === majorId ;
66+ const isSameMajorSelected =
67+ fetchedMajorSelected . current === majorSelected ;
68+
69+ if ( isSameMajor && isSameMajorSelected && hasInitialData . current ) {
70+ return ;
71+ }
72+
5573 dispatch ( setLoading ( true ) ) ;
5674
5775 try {
5876 const { data } = majorSelected
5977 ? await getCoursesByKd ( majorSelected . kd_org )
6078 : await getCourses ( majorId ) ;
6179
62- if ( ! majorSelected ) {
80+ if ( shouldClearSchedule && coursesLoaded . current ) {
81+ saveSchedulesToSessionStorage ( ) ;
6382 dispatch ( clearSchedule ( ) ) ;
6483 }
6584
6685 setCourses ( data . courses ) ;
6786 setCoursesDetail ( data . is_detail ) ;
6887 setLastUpdated ( new Date ( data . last_update_at ) ) ;
6988 dispatch ( reduxSetCourses ( data . courses ) ) ;
89+
90+ fetchedMajorId . current = majorId ;
91+ fetchedMajorSelected . current = majorSelected ;
92+ hasInitialData . current = true ;
93+ coursesLoaded . current = true ;
94+
95+ if ( ! shouldClearSchedule && data . courses ) {
96+ setTimeout ( ( ) => {
97+ restoreSchedulesFromSessionStorage ( ) ;
98+ } , 100 ) ;
99+ }
70100 } catch ( e ) {
71- /** TODO: handle error */
101+ console . error ( "Error fetching courses:" , e ) ;
72102 }
73103
74104 setTimeout ( ( ) => dispatch ( setLoading ( false ) ) , 1000 ) ;
75105 } ,
76- [ dispatch ] ,
106+ [
107+ dispatch ,
108+ saveSchedulesToSessionStorage ,
109+ restoreSchedulesFromSessionStorage ,
110+ ] ,
77111 ) ;
78112
79113 useEffect ( ( ) => {
80- document . getElementById ( "input" ) . value = "" ;
81- setValue ( "" ) ;
82- const majorId = auth . majorId ;
83- fetchCourses ( majorId , majorSelected ) ;
84- } , [ auth . majorId , majorSelected , dispatch , fetchCourses , setValue ] ) ;
114+ if ( ! hasInitialData . current ) {
115+ document . getElementById ( "input" ) ?. value &&
116+ ( document . getElementById ( "input" ) . value = "" ) ;
117+ setValue ( "" ) ;
118+ const majorId = auth . majorId ;
119+ const restored = restoreSchedulesFromSessionStorage ( ) ;
120+ if ( ! restored ) {
121+ fetchCourses ( majorId , majorSelected , false ) ;
122+ } else {
123+ fetchCourses ( majorId , majorSelected , false ) ;
124+ }
125+ }
126+ } , [ auth . majorId , fetchCourses , restoreSchedulesFromSessionStorage ] ) ;
127+
128+ useEffect ( ( ) => {
129+ if (
130+ hasInitialData . current &&
131+ majorSelected !== fetchedMajorSelected . current
132+ ) {
133+ document . getElementById ( "input" ) ?. value &&
134+ ( document . getElementById ( "input" ) . value = "" ) ;
135+ setValue ( "" ) ;
136+ const majorId = auth . majorId ;
137+
138+ fetchCourses ( majorId , majorSelected , true ) ;
139+ }
140+ } , [ majorSelected , auth . majorId , fetchCourses ] ) ;
141+
142+ useEffect ( ( ) => {
143+ const handleFocus = ( ) => {
144+ if ( hasInitialData . current && courses ) {
145+ restoreSchedulesFromSessionStorage ( ) ;
146+ }
147+ } ;
85148
86- // const handleChange = (e) => setValueTemporary(e.target.value);
149+ window . addEventListener ( "focus" , handleFocus ) ;
150+ return ( ) => window . removeEventListener ( "focus" , handleFocus ) ;
151+ } , [ courses , restoreSchedulesFromSessionStorage ] ) ;
87152
88153 let filteredCourse = courses ?. filter ( ( c ) => {
89154 if ( value === "" ) {
90- //if value is empty
91155 return c ;
92156 } else if ( c . name . toLowerCase ( ) . includes ( value . toLowerCase ( ) ) ) {
93- //returns filtered array
94157 return c ;
95158 } else {
96159 return null ;
0 commit comments