From 76ef62d13fb48b05017390d15f8a793379600ed9 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Fri, 24 Nov 2023 18:28:39 +0200 Subject: [PATCH] Autosave --- src/components/ControlPanel.jsx | 59 ++++++++++++------------ src/data/data.js | 8 ++++ src/pages/editor.jsx | 79 ++++++++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 30 deletions(-) diff --git a/src/components/ControlPanel.jsx b/src/components/ControlPanel.jsx index 62f3036..2a4e529 100644 --- a/src/components/ControlPanel.jsx +++ b/src/components/ControlPanel.jsx @@ -54,7 +54,7 @@ import { UndoRedoContext, } from "../pages/Editor"; import { IconAddTable, IconAddArea, IconAddNote } from "./CustomIcons"; -import { ObjectType, Action, Tab } from "../data/data"; +import { ObjectType, Action, Tab, State } from "../data/data"; import jsPDF from "jspdf"; import { useHotkeys } from "react-hotkeys-hook"; import { Validator } from "jsonschema"; @@ -68,6 +68,10 @@ export default function ControlPanel({ setDiagramId, title, setTitle, + state, + setState, + lastSaved, + setLastSaved, }) { const MODAL = { NONE: 0, @@ -672,31 +676,7 @@ export default function ControlPanel({ copy(); del(); }; - const save = () => { - if (diagramId === 0) { - db.diagrams - .add({ - name: title, - lastModified: new Date(), - tables: tables, - references: relationships, - types: types, - notes: notes, - areas: areas, - }) - .then((id) => setDiagramId(id)); - } else { - db.diagrams.update(diagramId, { - name: title, - lastModified: new Date(), - tables: tables, - references: relationships, - types: types, - notes: notes, - areas: areas, - }); - } - }; + const save = () => setState(State.SAVING); const open = () => setVisible(MODAL.OPEN); const saveDiagramAs = () => setVisible(MODAL.SAVEAS); const loadDiagram = async (id) => { @@ -1682,6 +1662,21 @@ export default function ControlPanel({ ); } + function getState() { + switch (state) { + case State.NONE: + return "No changes" + case State.LOADING: + return "Loading . . ."; + case State.SAVED: + return `Last saved ${lastSaved}`; + case State.SAVING: + return "Saving . . ."; + default: + return ""; + } + } + function header() { return (