Skip to content

Commit 05b490e

Browse files
authored
Merge pull request #217 from mp-access/tests-not-saved-bugfix
FIX: File changes no longer lost when changing from one file to another
2 parents d0c04a2 + 02586f1 commit 05b490e

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/pages/Task.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ export default function Task({ type }: { type: "task" | "example" }) {
224224
return enableSubmitCommand
225225
}, [enableSubmitCommand, type])
226226

227+
const getPath = (id: number) => `${id}/${user.email}`
228+
227229
const derivedEditorContent = useMemo(() => {
228230
// example case: submission not yet fully processed, so content only available from pending subnmission
229231
if (
@@ -338,6 +340,7 @@ export default function Task({ type }: { type: "task" | "example" }) {
338340
}
339341
}
340342
}, [submissionId])
343+
341344
useEffect(() => {
342345
if (task) {
343346
if (currentFile == undefined || taskId != task.id) {
@@ -351,7 +354,16 @@ export default function Task({ type }: { type: "task" | "example" }) {
351354
"id",
352355
),
353356
)
354-
setCurrentFile((file) => file && find(editableFiles, { id: file.id }))
357+
358+
setCurrentFile((file) => {
359+
if (!file) return file
360+
361+
const updatedEditableFile = find(editableFiles, { id: file.id })
362+
363+
if (updatedEditableFile) return updatedEditableFile
364+
365+
return find(task.files, { id: file.id })
366+
})
355367
}
356368
setTaskId(task.id)
357369
setEditorReload((prev) => prev + 1)
@@ -363,6 +375,27 @@ export default function Task({ type }: { type: "task" | "example" }) {
363375
setOpenFiles((files) => unionBy(files, [currentFile], "id"))
364376
}, [currentFile])
365377

378+
const saveCurrentFileContent = (currFile: TaskFileProps | undefined) => {
379+
if (currFile && !currFile.binary && currFile.editable) {
380+
const currentContent = editor.getContent(getPath(currFile.id))
381+
setEditableFiles((prevFiles) =>
382+
prevFiles.map((file) =>
383+
file.id === currFile.id && currentContent
384+
? { ...file, content: currentContent }
385+
: file,
386+
),
387+
)
388+
}
389+
}
390+
391+
const handleFileSelect = (newFile: TaskFileProps) => {
392+
if (newFile.id !== currentFile?.id) {
393+
saveCurrentFileContent(currentFile)
394+
const fileWithContent = find(editableFiles, { id: newFile.id }) || newFile
395+
setCurrentFile(fileWithContent)
396+
}
397+
}
398+
366399
if (!task || !currentFile) return <Placeholder />
367400

368401
const commands: string[] = compact([
@@ -371,7 +404,6 @@ export default function Task({ type }: { type: "task" | "example" }) {
371404
"grade",
372405
])
373406

374-
const getPath = (id: number) => `${id}/${user.email}/${submissionId}`
375407
const getTemplate = (name: string) => {
376408
if (!name.startsWith("/")) {
377409
name = "/" + name
@@ -622,9 +654,7 @@ export default function Task({ type }: { type: "task" | "example" }) {
622654
<FileTree
623655
files={task.files}
624656
selected={currentFile.path}
625-
onSelect={(file) =>
626-
setCurrentFile(find(editableFiles, { id: file.id }) || file)
627-
}
657+
onSelect={(file) => handleFileSelect(file)}
628658
/>
629659
</AccordionPanel>
630660
</AccordionItem>
@@ -634,7 +664,7 @@ export default function Task({ type }: { type: "task" | "example" }) {
634664
<FileTabs
635665
id={currentFile.id}
636666
files={openFiles}
637-
onSelect={setCurrentFile}
667+
onSelect={handleFileSelect}
638668
onReorder={setOpenFiles}
639669
/>
640670
{currentFile.binary || (

0 commit comments

Comments
 (0)