Преглед изворни кода

Merge pull request #60 from AykutSarac/download-json

Convert Save JSON to Download
Aykut Saraç пре 3 година
родитељ
комит
3026447788
3 измењених фајлова са 7 додато и 8 уклоњено
  1. 6 2
      src/components/Sidebar/index.tsx
  2. 0 1
      src/containers/ClearModal/index.tsx
  3. 1 5
      src/hocs/config.tsx

+ 6 - 2
src/components/Sidebar/index.tsx

@@ -140,8 +140,12 @@ export const Sidebar: React.FC = () => {
   const [clearVisible, setClearVisible] = React.useState(false);
 
   const handleSave = () => {
-    localStorage.setItem("json", json);
-    toast.success("Saved JSON successfully!");
+    const a = document.createElement("a");
+    const file = new Blob([json], { type: "text/plain" });
+
+    a.href = window.URL.createObjectURL(file);
+    a.download = "jsonvisio.json";
+    a.click();
   };
 
   const toggleExpandCollapse = () => {

+ 0 - 1
src/containers/ClearModal/index.tsx

@@ -10,7 +10,6 @@ export const ClearModal: React.FC<ModalProps> = ({ visible, setVisible }) => {
 
   const handleClear = () => {
     dispatch({ type: ConfigActionType.SET_JSON, payload: "{}" });
-    localStorage.removeItem("json");
     toast.success(`Cleared JSON and removed from memory.`);
     setVisible(false);
   };

+ 1 - 5
src/hocs/config.tsx

@@ -43,12 +43,8 @@ const WithConfig: ReactComponent = ({ children }) => {
   };
 
   React.useEffect(() => {
-    const jsonStored = localStorage.getItem("json");
-    if (jsonStored) {
-      dispatch({ type: ConfigActionType.SET_JSON, payload: jsonStored });
-    }
-
     const configStored = localStorage.getItem("config");
+
     if (configStored) {
       dispatch({
         type: ConfigActionType.SET_CONFIG,