99 useTheme ,
1010 useToast ,
1111} from '@chakra-ui/react' ;
12+ import { LessonPlanFile } from '@utils/types' ;
1213import { uploadFile } from '@utils/uploadFile' ;
13- import { useRef , useState } from 'react' ;
14+ import { useEffect , useRef , useState } from 'react' ;
1415import { FiFileText } from 'react-icons/fi' ;
1516
1617const formatFileSize = ( sizeInBytes : number ) => {
@@ -153,6 +154,13 @@ const LessonPlanView = ({
153154 const fileInputRef = useRef < HTMLInputElement | null > ( null ) ;
154155 const toast = useToast ( ) ;
155156 const [ isUploading , setIsUploading ] = useState ( false ) ;
157+ const [ localLessonPlan , setLocalLessonPlan ] = useState < LessonPlanFile | null > (
158+ lessonPlan
159+ ) ;
160+
161+ useEffect ( ( ) => {
162+ setLocalLessonPlan ( lessonPlan ) ;
163+ } , [ lessonPlan ] ) ;
156164
157165 const handleSwap = ( e : React . MouseEvent ) => {
158166 e . preventDefault ( ) ;
@@ -193,13 +201,23 @@ const LessonPlanView = ({
193201 } ) ;
194202
195203 if ( res . ok ) {
204+ const updatedPlan : LessonPlanFile = {
205+ id : - 1 ,
206+ name : file . name ,
207+ size : file . size ,
208+ url : fileUrl ,
209+ } ;
210+
211+ setLocalLessonPlan ( updatedPlan ) ;
212+
196213 toast ( {
197214 title : 'Lesson Plan Updated' ,
198215 description : 'Your lesson plan was successfully swapped.' ,
199216 status : 'success' ,
200217 duration : 5000 ,
201218 isClosable : true ,
202219 } ) ;
220+
203221 await fetchAbsences ?.( ) ;
204222 } else {
205223 toast ( {
@@ -214,12 +232,12 @@ const LessonPlanView = ({
214232 setIsUploading ( false ) ;
215233 } ;
216234
217- return lessonPlan ? (
235+ return localLessonPlan ? (
218236 < >
219237 < LessonPlanDisplay
220- href = { lessonPlan . url }
221- fileName = { lessonPlan . name }
222- fileSize = { lessonPlan . size }
238+ href = { localLessonPlan . url }
239+ fileName = { localLessonPlan . name }
240+ fileSize = { localLessonPlan . size }
223241 isUserAbsentTeacher = { isUserAbsentTeacher }
224242 isAdminMode = { isAdminMode }
225243 onSwap = { handleSwap }
0 commit comments