Skip to content

Commit 933308f

Browse files
committed
update saving and editing map logic
1 parent 58ef742 commit 933308f

2 files changed

Lines changed: 45 additions & 22 deletions

File tree

platforms/web/src/Teach.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@ function Teach() {
4747
};
4848

4949
const handleEditMap = (map: TeacherMapEntry) => {
50-
if (map.jsonId) {
51-
navigate(`/create#json=${map.jsonId}`);
52-
} else {
53-
// For maps without jsonId, we'd need to pass data differently
54-
// For now, prompt to publish first
55-
alert('Please publish this map first to get a shareable link, then you can edit it.');
56-
}
50+
// Use jsonId if published, otherwise use the local storage ID
51+
const editId = map.jsonId || map.id;
52+
navigate(`/create#id=${editId}`);
5753
};
5854

5955
const handleAddMapFromUrl = async () => {

platforms/web/src/TeacherEditor.tsx

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function TeacherEditorInner() {
1313
const shareLink = useEditorStore(state => state.shareLink);
1414
const shareDialogOpen = useEditorStore(state => state.shareDialogOpen);
1515
const getRoadmapData = useEditorStore(state => state.getRoadmapData);
16+
const loadRoadmapData = useEditorStore(state => state.loadRoadmapData);
1617
const reset = useEditorStore(state => state.reset);
1718
const nodes = useEditorStore(state => state.nodes);
1819
const edges = useEditorStore(state => state.edges);
@@ -21,20 +22,46 @@ function TeacherEditorInner() {
2122
const saveTimeoutRef = useRef<number | null>(null);
2223
const initializedRef = useRef(false);
2324

24-
// Extract jsonId from URL
25-
const jsonId = location.hash.startsWith('#json=') ? location.hash.replace('#json=', '') : null;
25+
// Extract id from URL - supports both #id= and #json= for backwards compatibility
26+
const urlHash = location.hash.slice(1); // Remove the #
27+
const idMatch = urlHash.match(/id=([^&]+)/);
28+
const jsonMatch = urlHash.match(/json=([^&]+)/);
29+
const mapId = idMatch ? idMatch[1] : jsonMatch ? jsonMatch[1] : null;
2630

27-
// Reset editor when creating a new map (no jsonId in URL)
31+
// Load map from database when editing an existing map
2832
useEffect(() => {
29-
if (!jsonId && !initializedRef.current) {
30-
reset();
33+
if (!mapId) {
34+
// No ID in URL - reset for new map creation
35+
if (!initializedRef.current) {
36+
reset();
37+
initializedRef.current = true;
38+
}
39+
return;
40+
}
41+
42+
// Load map from database
43+
const loadMap = async () => {
44+
try {
45+
const teacherMap = await db.getTeacherMap(mapId);
46+
if (teacherMap) {
47+
loadRoadmapData(teacherMap.roadmapData);
48+
initializedRef.current = true;
49+
}
50+
} catch (err) {
51+
console.error('Failed to load map:', err);
52+
}
53+
};
54+
55+
if (!initializedRef.current) {
56+
loadMap();
3157
initializedRef.current = true;
3258
}
33-
// Reset the flag when jsonId changes (navigating to edit an existing map)
34-
if (jsonId) {
59+
60+
// Reset the flag when mapId changes (navigating to a different map)
61+
return () => {
3562
initializedRef.current = false;
36-
}
37-
}, [jsonId, reset]);
63+
};
64+
}, [mapId, reset, loadRoadmapData]);
3865

3966
// Auto-save changes with debouncing
4067
useEffect(() => {
@@ -52,11 +79,11 @@ function TeacherEditorInner() {
5279

5380
// Generate a storage ID for this map
5481
let storageId: string;
55-
if (jsonId) {
56-
// Use jsonId if available
57-
storageId = roadmapData.settings?.id || jsonId;
82+
if (mapId) {
83+
// Use mapId if available (editing existing map)
84+
storageId = roadmapData.settings?.id || mapId;
5885
} else {
59-
// For new maps without jsonId, use or create a unique ID
86+
// For new maps without mapId, use or create a unique ID
6087
storageId = roadmapData.settings?.id || `local-${Date.now()}`;
6188
// Update settings with the generated ID if it doesn't have one
6289
if (!roadmapData.settings?.id) {
@@ -68,7 +95,7 @@ function TeacherEditorInner() {
6895
// Check if this map exists in teacher's collection
6996
const existingMap = await db.getTeacherMap(storageId);
7097
// Save or update the map
71-
await db.addTeacherMap(storageId, roadmapData, existingMap?.jsonId || jsonId || undefined);
98+
await db.addTeacherMap(storageId, roadmapData, existingMap?.jsonId || undefined);
7299
} catch (err) {
73100
console.error('Failed to auto-save map:', err);
74101
}
@@ -79,7 +106,7 @@ function TeacherEditorInner() {
79106
clearTimeout(saveTimeoutRef.current);
80107
}
81108
};
82-
}, [nodes, edges, settings, jsonId, getRoadmapData]);
109+
}, [nodes, edges, settings, mapId, getRoadmapData]);
83110

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

0 commit comments

Comments
 (0)