Skip to content

Commit

Permalink
feat: Refactor Upload Folders to Use Mutations for HTTP Request Manag…
Browse files Browse the repository at this point in the history
…ement (langflow-ai#2810)

* ✨ (frontend): Add new API queries for user authentication and messages handling
📝 (frontend): Update API queries for user authentication to include new functionalities like logout, reset password, update user, add user, login user, and refresh access token
📝 (frontend): Update API queries for messages to change the update messages functionality to use PUT method instead of PATCH

* ✨ (sideBarFolderButtons/index.tsx): Add usePostUploadFolders query to handle uploading folders and files
🔧 (constants.ts): Add FOLDERS constant to API URLs for folder-related endpoints
🔧 (folders/index.tsx): Add use-post-upload-folders query to handle uploading folders and files

🔧 (use-update-messages.ts): Remove unused file use-update-messages.ts to clean up the project
🔧 (foldersStore.tsx): Remove unused uploadFolder function to improve code maintainability and reduce complexity
🔧 (folders/index.ts): Remove unused uploadFolder function to simplify the codebase and improve readability

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
Cristhianzl and autofix-ci[bot] committed Jul 23, 2024
1 parent 90508b2 commit 65acbd5
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { usePostUploadFolders } from "@/controllers/API/queries/folders";
import { useEffect, useRef, useState } from "react";
import { useLocation } from "react-router-dom";
import { FolderType } from "../../../../pages/MainPage/entities";
Expand Down Expand Up @@ -33,7 +34,6 @@ const SideBarFoldersButtonsComponent = ({
const [editFolders, setEditFolderName] = useState(
folders.map((obj) => ({ name: obj.name, edit: false })),
);
const uploadFolder = useFolderStore((state) => state.uploadFolder);
const currentFolder = pathname.split("/");
const urlWithoutPath = pathname.split("/").length < 4;
const myCollectionId = useFolderStore((state) => state.myCollectionId);
Expand Down Expand Up @@ -61,21 +61,49 @@ const SideBarFoldersButtonsComponent = ({
handleFolderChange,
);

const { mutate } = usePostUploadFolders();

const handleUploadFlowsToFolder = () => {
uploadFolder(folderId)
.then(() => {
getFolderById(folderId);
setSuccessData({
title: "Uploaded successfully",
});
})
.catch((err) => {
console.log(err);
setErrorData({
title: `Error on upload`,
list: [err["response"]["data"]],
const input = document.createElement("input");
input.type = "file";
input.accept = ".json";
input.click();

input.onchange = (event: Event) => {
if (
(event.target as HTMLInputElement).files![0].type === "application/json"
) {
const file = (event.target as HTMLInputElement).files![0];
const formData = new FormData();
formData.append("file", file);
file.text().then(async (text) => {
const data = JSON.parse(text);
if (data.data?.nodes) {
await useFlowsManagerStore.getState().addFlow(true, data);
getFolderById(folderId);
} else {
mutate(
{ formData },
{
onSuccess: () => {
getFolderById(folderId);
setSuccessData({
title: "Uploaded successfully",
});
},
onError: (err) => {
console.log(err);
setErrorData({
title: `Error on upload`,
list: [err["response"]["data"]],
});
},
},
);
}
});
});
}
};
};

const handleDownloadFolder = (id: string) => {
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/controllers/API/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const URLs = {
AUTOLOGIN: "auto_login",
REFRESH: "refresh",
BUILD: `build`,
FOLDERS: `folders`,
} as const;

export function getURL(key: keyof typeof URLs, params: any = {}) {
Expand Down
9 changes: 9 additions & 0 deletions src/frontend/src/controllers/API/queries/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export * from "./use-delete-users";
export * from "./use-get-autologin";
export * from "./use-get-user";
export * from "./use-get-users-page";
export * from "./use-patch-logout";
export * from "./use-patch-reset-password";
export * from "./use-patch-update-user";
export * from "./use-post-add-user";
export * from "./use-post-login-user";
export * from "./use-post-refresh-access";
1 change: 1 addition & 0 deletions src/frontend/src/controllers/API/queries/folders/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./use-post-upload-folders";
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import useFlowsManagerStore from "@/stores/flowsManagerStore";
import { useFolderStore } from "@/stores/foldersStore";
import { Users, useMutationFunctionType } from "@/types/api";
import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

interface IPostAddUploadFolders {
formData: FormData;
}

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

const uploadFoldersFn = async (
payload: IPostAddUploadFolders,
): Promise<void> => {
const res = await api.post(
`${getURL("FOLDERS")}/upload/`,
payload.formData,
);
await useFolderStore.getState().getFoldersApi(true);
return res.data;
};

const mutation = mutate(["usePostUploadFolders"], uploadFoldersFn, options);

return mutation;
};
2 changes: 1 addition & 1 deletion src/frontend/src/controllers/API/queries/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./use-delete-messages";
export * from "./use-get-messages";
export * from "./use-update-messages";
export * from "./use-put-update-messages";
44 changes: 0 additions & 44 deletions src/frontend/src/stores/foldersStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,50 +133,6 @@ export const useFolderStore = create<FoldersStoreType>((set, get) => ({
setFolderDragging: (folder) => set(() => ({ folderDragging: folder })),
folderIdDragging: "",
setFolderIdDragging: (id) => set(() => ({ folderIdDragging: id })),
uploadFolder: () => {
return new Promise<void>((resolve, reject) => {
const input = document.createElement("input");
input.type = "file";
input.accept = ".json";
input.onchange = (event: Event) => {
if (
(event.target as HTMLInputElement).files![0].type ===
"application/json"
) {
const file = (event.target as HTMLInputElement).files![0];
const formData = new FormData();
formData.append("file", file);
file.text().then((text) => {
const data = JSON.parse(text);
if (data.data?.nodes) {
useFlowsManagerStore
.getState()
.addFlow(true, data)
.then(() => {
resolve();
})
.catch((error) => {
reject(error);
});
} else {
uploadFlowsFromFolders(formData)
.then(() => {
get()
.getFoldersApi(true)
.then(() => {
resolve();
});
})
.catch((error) => {
reject(error);
});
}
});
}
};
input.click();
});
},
starterProjectId: "",
setStarterProjectId: (id) => set(() => ({ starterProjectId: id })),
}));
1 change: 0 additions & 1 deletion src/frontend/src/types/zustand/folders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export type FoldersStoreType = {
setFolderUrl: (folderUrl: string) => void;
folderDragging: boolean;
setFolderDragging: (set: boolean) => void;
uploadFolder: (folderId: string) => Promise<void>;
folderIdDragging: string;
setFolderIdDragging: (id: string) => void;
starterProjectId: string;
Expand Down

0 comments on commit 65acbd5

Please sign in to comment.