Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change downloadFolders requests to use useQuery hook #2920

Merged
merged 7 commits into from
Jul 25, 2024
Cristhianzl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { usePostUploadFolders } from "@/controllers/API/queries/folders";
import { useGetDownloadFolders } from "@/controllers/API/queries/folders/use-get-download-folders";
import { useEffect, useRef, useState } from "react";
import { useLocation } from "react-router-dom";
import { FolderType } from "../../../../pages/MainPage/entities";
import { addFolder, updateFolder } from "../../../../pages/MainPage/services";
import { handleDownloadFolderFn } from "../../../../pages/MainPage/utils/handle-download-folder";
import useAlertStore from "../../../../stores/alertStore";
import useFlowsManagerStore from "../../../../stores/flowsManagerStore";
import { useFolderStore } from "../../../../stores/foldersStore";
Expand Down Expand Up @@ -106,8 +106,32 @@ const SideBarFoldersButtonsComponent = ({
};
};

const { mutate: mutateDownloadFolder } = useGetDownloadFolders();

const handleDownloadFolder = (id: string) => {
handleDownloadFolderFn(id);
mutateDownloadFolder(
{
folderId: id,
},
{
onSuccess: (data) => {
const folder = folders.find((f) => f.id === data.folderId);

data.folder_name = folder?.name || "folder";
data.folder_description = folder?.description || "";

const jsonString = `data:text/json;charset=utf-8,${encodeURIComponent(
JSON.stringify(data),
)}`;

const link = document.createElement("a");
link.href = jsonString;
link.download = `${data.folder_name}.json`;

link.click();
},
},
);
};

function addNewFolder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import {
WRONG_FILE_ERROR_ALERT,
} from "../../../constants/alerts_constants";
import { updateFlowInDatabase } from "../../../controllers/API";
import {
uploadFlowToFolder,
uploadFlowsFromFolders,
} from "../../../pages/MainPage/services";
import { uploadFlowToFolder } from "../../../pages/MainPage/services";
import useAlertStore from "../../../stores/alertStore";
import useFlowsManagerStore from "../../../stores/flowsManagerStore";
import { useFolderStore } from "../../../stores/foldersStore";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useMutationFunctionType } from "@/types/api";
import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

interface IGetDownloadFolders {
folderId: string;
}

export const useGetDownloadFolders: useMutationFunctionType<
undefined,
IGetDownloadFolders
> = (options?) => {
const { mutate } = UseRequestProcessor();

const downloadFoldersFn = async (
data: IGetDownloadFolders,
): Promise<void> => {
const res = await api.get(`${getURL("FOLDERS")}/download/${data.folderId}`);
return res.data;
};

const mutation = mutate(["useGetFolders"], downloadFoldersFn, options);
return mutation;
};
50 changes: 0 additions & 50 deletions src/frontend/src/pages/MainPage/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,56 +61,6 @@ export async function getFolderById(folderId: string): Promise<FolderType> {
}
}

export async function downloadFlowsFromFolders(folderId: string): Promise<{
flows: FlowType[];
folder_name: string;
folder_description: string;
}> {
try {
const response = await api.get(
`${BASE_URL_API}folders/download/${folderId}`,
);
if (response?.status !== 200) {
throw new Error(`HTTP error! status: ${response?.status}`);
}
console.log(response.data);
return response.data;
} catch (error) {
console.error(error);
throw error;
}
}

export async function uploadFlowsFromFolders(
flows: FormData,
): Promise<FlowType[]> {
try {
const response = await api.post(`${BASE_URL_API}folders/upload/`, flows);

if (response?.status !== 201) {
throw new Error(`HTTP error! status: ${response?.status}`);
}
return response.data;
} catch (error) {
console.error(error);
throw error;
}
}

export async function moveFlowToFolder(
flowId: string,
folderId: string,
): Promise<FlowType> {
try {
const response = await api.patch(
`${BASE_URL_API}folders/move_to_folder/${flowId}/${folderId}`,
);
return response?.data;
} catch (error) {
throw error;
}
}

export async function uploadFlowToFolder(
flows: FormData,
folderId: string,
Expand Down
6 changes: 1 addition & 5 deletions src/frontend/src/stores/foldersStore.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { create } from "zustand";
import { DEFAULT_FOLDER, STARTER_FOLDER_NAME } from "../constants/constants";
import {
getFolderById,
getFolders,
uploadFlowsFromFolders,
} from "../pages/MainPage/services";
import { getFolderById, getFolders } from "../pages/MainPage/services";
import { FoldersStoreType } from "../types/zustand/folders";
import useFlowsManagerStore from "./flowsManagerStore";
import { useTypesStore } from "./typesStore";
Expand Down
Loading