Skip to content

Commit 0259dad

Browse files
committed
save changes in create view
1 parent 7ef295a commit 0259dad

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

platforms/web/src/TeacherEditor.tsx

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,59 @@
11
import { useEffect, useRef } from 'react';
2-
import { useNavigate } from 'react-router-dom';
2+
import { useNavigate, useLocation } from 'react-router-dom';
33
import { LearningMapEditor, useEditorStore } from '@learningmap/learningmap';
44
import "@learningmap/learningmap/index.css";
55
import * as db from './db';
66
import './TeacherEditor.css';
77
import { Header } from './Header';
88

99
// This component wraps the LearningMapEditor and saves maps to the teacher's collection
10-
// when they are shared
10+
// when they are shared or modified
1111
function TeacherEditorInner() {
12+
const location = useLocation();
1213
const shareLink = useEditorStore(state => state.shareLink);
1314
const shareDialogOpen = useEditorStore(state => state.shareDialogOpen);
1415
const getRoadmapData = useEditorStore(state => state.getRoadmapData);
16+
const nodes = useEditorStore(state => state.nodes);
17+
const edges = useEditorStore(state => state.edges);
18+
const settings = useEditorStore(state => state.settings);
1519
const lastSavedRef = useRef<string | null>(null);
20+
const saveTimeoutRef = useRef<number | null>(null);
21+
22+
// Extract jsonId from URL
23+
const jsonId = location.hash.startsWith('#json=') ? location.hash.replace('#json=', '') : null;
24+
25+
// Auto-save changes with debouncing
26+
useEffect(() => {
27+
if (!jsonId) return;
28+
29+
// Clear any pending save
30+
if (saveTimeoutRef.current) {
31+
clearTimeout(saveTimeoutRef.current);
32+
}
33+
34+
// Debounce saves to avoid too many writes
35+
saveTimeoutRef.current = window.setTimeout(async () => {
36+
const roadmapData = getRoadmapData();
37+
const storageId = roadmapData.settings?.id || jsonId;
38+
39+
try {
40+
// Check if this map exists in teacher's collection
41+
const existingMap = await db.getTeacherMap(storageId);
42+
if (existingMap) {
43+
// Update existing map
44+
await db.addTeacherMap(storageId, roadmapData, existingMap.jsonId);
45+
}
46+
} catch (err) {
47+
console.error('Failed to auto-save map:', err);
48+
}
49+
}, 1000); // Save after 1 second of inactivity
50+
51+
return () => {
52+
if (saveTimeoutRef.current) {
53+
clearTimeout(saveTimeoutRef.current);
54+
}
55+
};
56+
}, [nodes, edges, settings, jsonId, getRoadmapData]);
1657

1758
// When a map is shared, save it to the teacher's collection
1859
useEffect(() => {

0 commit comments

Comments
 (0)