Skip to content

Commit 2fff6f9

Browse files
Update LessonPlanView.tsx
1 parent 9f15c8e commit 2fff6f9

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/components/LessonPlanView.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import {
99
useTheme,
1010
useToast,
1111
} from '@chakra-ui/react';
12+
import { LessonPlanFile } from '@utils/types';
1213
import { uploadFile } from '@utils/uploadFile';
13-
import { useRef, useState } from 'react';
14+
import { useEffect, useRef, useState } from 'react';
1415
import { FiFileText } from 'react-icons/fi';
1516

1617
const 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

Comments
 (0)