SarahXia0405 commited on
Commit
c7743cb
·
verified ·
1 Parent(s): 90a4d01

Update web/src/App.tsx

Browse files
Files changed (1) hide show
  1. web/src/App.tsx +19 -7
web/src/App.tsx CHANGED
@@ -663,6 +663,9 @@ function App() {
663
 
664
  const handleStartQuiz = () => handleNextQuestion();
665
 
 
 
 
666
  const handleFileUpload = (files: File[]) => {
667
  const newFiles: UploadedFile[] = files.map((file) => ({ file, type: "other" as FileType }));
668
  setUploadedFiles((prev) => [...prev, ...newFiles]);
@@ -672,17 +675,26 @@ function App() {
672
  setUploadedFiles((prev) => prev.filter((_, i) => i !== index));
673
  };
674
 
 
 
 
 
675
  const handleFileTypeChange = async (index: number, type: FileType) => {
676
  if (!user) return;
677
 
678
- const next = uploadedFiles.map((f, i) => (i === index ? { ...f, type } : f));
679
- setUploadedFiles(next);
 
 
 
 
 
 
 
680
 
681
- const target = next[index];
682
- const file = target?.file;
683
- if (!file) return;
684
 
685
- const fp = `${file.name}::${file.size}::${file.lastModified}`;
686
  if (uploadedFingerprintsRef.current.has(fp)) return;
687
  uploadedFingerprintsRef.current.add(fp);
688
 
@@ -690,7 +702,7 @@ function App() {
690
  await apiUpload({
691
  user_id: user.email,
692
  doc_type: DOC_TYPE_MAP[type] || "Other Course Document",
693
- file,
694
  });
695
  toast.success("File uploaded to backend");
696
  } catch (e: any) {
 
663
 
664
  const handleStartQuiz = () => handleNextQuestion();
665
 
666
+ // =========================
667
+ // ✅ File Upload (FIXED)
668
+ // =========================
669
  const handleFileUpload = (files: File[]) => {
670
  const newFiles: UploadedFile[] = files.map((file) => ({ file, type: "other" as FileType }));
671
  setUploadedFiles((prev) => [...prev, ...newFiles]);
 
675
  setUploadedFiles((prev) => prev.filter((_, i) => i !== index));
676
  };
677
 
678
+ // ✅ CRITICAL FIX:
679
+ // - use functional setState to avoid stale closure
680
+ // - do NOT overwrite newly-added files
681
+ // - safely pick the target file inside the updater
682
  const handleFileTypeChange = async (index: number, type: FileType) => {
683
  if (!user) return;
684
 
685
+ let targetFile: File | undefined;
686
+
687
+ setUploadedFiles((prev) => {
688
+ if (index < 0 || index >= prev.length) return prev;
689
+
690
+ const next = prev.map((f, i) => (i === index ? { ...f, type } : f));
691
+ targetFile = next[index]?.file;
692
+ return next;
693
+ });
694
 
695
+ if (!targetFile) return;
 
 
696
 
697
+ const fp = `${targetFile.name}::${targetFile.size}::${targetFile.lastModified}`;
698
  if (uploadedFingerprintsRef.current.has(fp)) return;
699
  uploadedFingerprintsRef.current.add(fp);
700
 
 
702
  await apiUpload({
703
  user_id: user.email,
704
  doc_type: DOC_TYPE_MAP[type] || "Other Course Document",
705
+ file: targetFile,
706
  });
707
  toast.success("File uploaded to backend");
708
  } catch (e: any) {