Skip to content

✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.

fix: cloud post/put

AykutSarac6a405b26a193
diff --git a/src/containers/Editor/BottomBar.tsx b/src/containers/Editor/BottomBar.tsx
index 74d278a4..ed39a5fb 100644
--- a/src/containers/Editor/BottomBar.tsx
+++ b/src/containers/Editor/BottomBar.tsx
@@ -13,7 +13,7 @@ import {
} from "react-icons/ai";
import { MdReportGmailerrorred, MdOutlineCheckCircleOutline } from "react-icons/md";
import { VscAccount, VscWorkspaceTrusted } from "react-icons/vsc";
-import { saveToCloud, updateJson } from "src/services/json";
+import { updateJson } from "src/services/json";
import useFile from "src/store/useFile";
import useModal from "src/store/useModal";
import useStored from "src/store/useStored";
@@ -81,7 +81,7 @@ const StyledImg = styled.img<{ light: boolean }>`
`;
export const BottomBar = () => {
- const { replace, query } = useRouter();
+ const { query } = useRouter();
const data = useFile(state => state.fileData);
const error = useFile(state => state.error);
const user = useUser(state => state.user);
@@ -101,33 +101,26 @@ export const BottomBar = () => {
const handleSaveJson = React.useCallback(async () => {
if (!user) return setVisible("login")(true);
+ if (!query?.json) return setVisible("cloud")(true);
- if (
- hasChanges &&
- !error &&
- (typeof query.json === "string" || typeof query.json === "undefined")
- ) {
+ if (typeof query?.json === "string" && hasChanges) {
try {
setIsUpdating(true);
- toast.loading("Saving document...", { id: "fileSave" });
- const res = await saveToCloud(query?.json ?? null, getContents());
+ toast.loading("Saving document...", { id: "fileUpdate" });
+ const res = await updateJson(query?.json, { json: getContents() });
if (res.errors && res.errors.items.length > 0) throw res.errors;
- if (res.data._id) replace({ query: { json: res.data._id } });
-
- toast.success("Document saved to cloud", { id: "fileSave" });
+ toast.success("Document saved to cloud", { id: "fileUpdate" });
setHasChanges(false);
} catch (error: any) {
if (error?.items?.length > 0) {
- return toast.error(error.items[0].message, { id: "fileSave", duration: 5000 });
+ return toast.error(error.items[0].message, { id: "fileUpdate", duration: 5000 });
}
- toast.error("Failed to save document!", { id: "fileSave" });
- } finally {
- setIsUpdating(false);
+ toast.error("Failed to save document!", { id: "fileUpdate" });
}
}
- }, [error, getContents, hasChanges, query, replace, setHasChanges, setVisible, user]);
+ }, [getContents, hasChanges, query, setHasChanges, setVisible, user]);
const handleLoginClick = () => {
if (user) return setVisible("account")(true);
@@ -185,7 +178,7 @@ export const BottomBar = () => {
</StyledBottomBarItem>
<StyledBottomBarItem onClick={handleSaveJson} disabled={isUpdating}>
{hasChanges ? <AiOutlineCloudUpload /> : <AiOutlineCloudSync />}
- {hasChanges ? "Unsaved Changes" : "Saved"}
+ {hasChanges ? (query?.json ? "Unsaved Changes" : "Create Document") : "Saved"}
</StyledBottomBarItem>
{data && (
<>
diff --git a/src/containers/Modals/CloudModal/index.tsx b/src/containers/Modals/CloudModal/index.tsx
index f3ae687e..983402b3 100644
--- a/src/containers/Modals/CloudModal/index.tsx
+++ b/src/containers/Modals/CloudModal/index.tsx
@@ -173,13 +173,13 @@ const CreateCard: React.FC<{ reachedLimit: boolean }> = ({ reachedLimit }) => {
const onCreate = async () => {
try {
toast.loading("Saving document...", { id: "fileSave" });
- const res = await saveToCloud(null, getContents());
+ const res = await saveToCloud(getContents());
if (res.errors && res.errors.items.length > 0) throw res.errors;
toast.success("Document saved to cloud", { id: "fileSave" });
setHasChanges(false);
- replace({ query: { doc: res.data._id } });
+ replace({ query: { json: res.data._id } });
} catch (error: any) {
if (error?.items?.length > 0) {
return toast.error(error.items[0].message, { id: "fileSave", duration: 7000 });
@@ -231,7 +231,7 @@ export const CloudModal: React.FC<ModalProps> = ({ opened, onClose }) => {
data={json}
key={json._id}
refetch={refetch}
- active={query.doc === json._id}
+ active={query?.json === json._id}
/>
))}
</>
diff --git a/src/services/json.ts b/src/services/json.ts
index ee3b1ef4..ac5b699d 100644
--- a/src/services/json.ts
+++ b/src/services/json.ts
@@ -3,11 +3,7 @@ import { decompressFromBase64 } from "lz-string";
import { altogic, AltogicResponse } from "src/api/altogic";
import { File } from "src/store/useFile";
-const saveToCloud = async (
- id: string | null,
- contents: string
-): Promise<AltogicResponse<{ _id: string }>> => {
- if (id) return await altogic.endpoint.put(`json/${id}`, { json: contents });
+const saveToCloud = async (contents: string): Promise<AltogicResponse<{ _id: string }>> => {
return await altogic.endpoint.post("json", { json: contents });
};
diff --git a/src/store/useFile.ts b/src/store/useFile.ts
index 61c5b851..e5d04aa5 100644
--- a/src/store/useFile.ts
+++ b/src/store/useFile.ts
@@ -2,7 +2,7 @@ import debounce from "lodash.debounce";
import { toast } from "react-hot-toast";
import { create } from "zustand";
import { defaultJson } from "src/constants/data";
-import { getFromCloud, saveToCloud } from "src/services/json";
+import { getFromCloud } from "src/services/json";
import useGraph from "./useGraph";
import useJson from "./useJson";
import useUser from "./useUser";
@@ -19,7 +19,6 @@ interface JsonActions {
setError: (error: boolean) => void;
setHasChanges: (hasChanges: boolean) => void;
setContents: (data: SetContents) => void;
- saveToCloud: (isNew?: boolean) => void;
fetchFile: (fileId: string) => void;
fetchUrl: (url: string) => void;
clear: () => void;
@@ -74,30 +73,6 @@ const useFile = create<FileStates & JsonActions>()((set, get) => ({
},
setError: error => set({ error }),
setHasChanges: hasChanges => set({ hasChanges }),
- saveToCloud: async (isNew = true) => {
- try {
- const url = new URL(window.location.href);
- const params = new URLSearchParams(url.search);
- const jsonQuery = params.get("doc");
-
- toast.loading("Saving File...", { id: "fileSave" });
- const res = await saveToCloud(isNew ? null : jsonQuery, get().contents);
-
- if (res.errors && res.errors.items.length > 0) throw res.errors;
-
- toast.success("File saved to cloud", { id: "fileSave" });
- set({ hasChanges: false });
- return res.data._id;
- } catch (error: any) {
- if (error?.items?.length > 0) {
- toast.error(error.items[0].message, { id: "fileSave", duration: 5000 });
- return undefined;
- }
-
- toast.error("Failed to save File!", { id: "fileSave" });
- return undefined;
- }
- },
fetchUrl: async url => {
try {
const res = await fetch(url);