From ff08984b55438a0ff2f3e8903a4d7c025842d563 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Tue, 19 Sep 2023 15:49:29 +0300 Subject: [PATCH] Add import from json --- src/components/control_panel.jsx | 104 +++++++++++++++++++++++++++---- 1 file changed, 93 insertions(+), 11 deletions(-) diff --git a/src/components/control_panel.jsx b/src/components/control_panel.jsx index 2f776d7..989cbf3 100644 --- a/src/components/control_panel.jsx +++ b/src/components/control_panel.jsx @@ -53,16 +53,27 @@ export default function ControlPanel(props) { CODE: 2, IMPORT: 3, }; + const ERROR = { + NONE: 0, + WARNING: 1, + ERROR: 2, + OK: 3, + }; const [visible, setVisible] = useState(MODAL.NONE); const [exportData, setExportData] = useState({ data: "", filename: `diagram_${new Date().toISOString()}`, extension: "", }); - const [error, setError] = useState(null); + const [error, setError] = useState({ + type: ERROR.NONE, + message: "", + }); + const [data, setData] = useState(null); const { layout, setLayout } = useContext(LayoutContext); const { setSettings } = useContext(SettingsContext); - const { relationships, tables, setTables } = useContext(TableContext); + const { relationships, tables, setTables, setRelationships } = + useContext(TableContext); const { notes, setNotes } = useContext(NoteContext); const { areas, setAreas } = useContext(AreaContext); @@ -331,6 +342,22 @@ export default function ControlPanel(props) { }, }; + const diagramIsEmpty = () => { + return ( + tables.length === 0 && + relationships.length === 0 && + notes.length === 0 && + areas.length === 0 + ); + }; + + const overwriteDiagram = () => { + setTables(data.tables); + setRelationships(data.relationships); + setAreas(data.subjectAreas); + setNotes(data.notes); + }; + return (
{layout.header && header()} @@ -516,6 +543,11 @@ export default function ControlPanel(props) { }); saveAs(blob, `${exportData.filename}.${exportData.extension}`); } else if (visible === MODAL.IMPORT) { + if (error.type !== ERROR.ERROR) { + overwriteDiagram(); + setData(null); + setVisible(MODAL.NONE); + } } }} afterClose={() => { @@ -524,12 +556,17 @@ export default function ControlPanel(props) { extension: "", filename: `diagram_${new Date().toISOString()}`, })); - setError(null); + setError({ + type: ERROR.NONE, + message: "", + }); + setData(null); }} onCancel={() => setVisible(MODAL.NONE)} centered closeOnEsc={true} okText={`${visible === MODAL.IMPORT ? "Import" : "Export"}`} + okButtonProps={{ disabled: error.type === ERROR.ERROR }} cancelText="Cancel" width={520} > @@ -550,16 +587,35 @@ export default function ControlPanel(props) { try { jsonObject = JSON.parse(event.target.result); } catch (error) { - setError("Invalid JSON. The file contains an error."); + setError({ + type: ERROR.ERROR, + message: "Invalid JSON. The file contains an error.", + }); return; } if (!diagramObjectIsValid(jsonObject)) { - setError( - "The file is missing necessary properties for a diagram." - ); + setError({ + type: ERROR.ERROR, + message: + "The file is missing necessary properties for a diagram.", + }); return; } + + setData(jsonObject); + if (diagramIsEmpty()) { + setError({ + type: ERROR.OK, + message: "Everything looks good. You can now import.", + }); + } else { + setError({ + type: ERROR.WARNING, + message: + "The current diagram is not empty. Importing a new diagram will overwrite the current changes.", + }); + } } }; reader.readAsText(f); @@ -575,16 +631,42 @@ export default function ControlPanel(props) { dragMainText="Click to upload the file or drag and drop the file here" dragSubText="Support json" accept="application/json,.txt" - onRemove={() => setError(null)} - onFileChange={() => setError(null)} + onRemove={() => + setError({ + type: ERROR.NONE, + message: "", + }) + } + onFileChange={() => + setError({ + type: ERROR.NONE, + message: "", + }) + } limit={1} > - {error && ( + {error.type === ERROR.ERROR ? ( {error}
} + description={ +
{error.message}
+ } /> + ) : error.type === ERROR.OK ? ( + {error.message}} + /> + ) : ( + error.type === ERROR.WARNING && ( + {error.message}} + /> + ) )} ) : exportData.data !== "" || exportData.data ? (