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,12 +1,12 @@
import {
usePatchFolders,
usePostFolders,
usePostUploadFolders,
} from "@/controllers/API/queries/folders";
import { usePostFolders } from "@/controllers/API/queries/folders/use-post-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 { 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 @@ -110,8 +110,37 @@ 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();
},
onError: () => {
setErrorData({
title: `An error occurred while downloading folder.`,
});
},
},
);
};

const { mutate: mutateAddFolder } = usePostFolders();
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 @@ -21,56 +21,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
2 changes: 1 addition & 1 deletion src/frontend/tests/end-to-end/generalBugs-shard-6.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class CustomComponent(Component):
await page.locator("textarea").press("Control+a");
await page.locator("textarea").fill(customCodeWithError);

await page.getByRole("button", { name: "Check & Save" }).click();
await page.getByText("Check & Save").last().click();

await page.waitForTimeout(1000);

Expand Down