refactor: Update getFoldersApi to handle startup application and improve code readability
This commit is contained in:
parent
9dd56b0412
commit
33b3a9ee7e
1 changed files with 57 additions and 21 deletions
|
|
@ -7,15 +7,16 @@ import {
|
||||||
} from "../pages/MainPage/services";
|
} from "../pages/MainPage/services";
|
||||||
import { FoldersStoreType } from "../types/zustand/folders";
|
import { FoldersStoreType } from "../types/zustand/folders";
|
||||||
import useFlowsManagerStore from "./flowsManagerStore";
|
import useFlowsManagerStore from "./flowsManagerStore";
|
||||||
|
import { useTypesStore } from "./typesStore";
|
||||||
|
|
||||||
export const useFolderStore = create<FoldersStoreType>((set, get) => ({
|
export const useFolderStore = create<FoldersStoreType>((set, get) => ({
|
||||||
folders: [],
|
folders: [],
|
||||||
getFoldersApi: (refetch = false) => {
|
getFoldersApi: (refetch = false, startupApplication: boolean = false) => {
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
if (get()?.folders.length === 0 || refetch === true) {
|
if (get()?.folders.length === 0 || refetch === true) {
|
||||||
get().setLoading(true);
|
get().setIsLoadingFolders(true);
|
||||||
getFolders().then(
|
getFolders().then(
|
||||||
(res) => {
|
async (res) => {
|
||||||
const foldersWithoutStarterProjects = res?.filter(
|
const foldersWithoutStarterProjects = res?.filter(
|
||||||
(folder) => folder.name !== STARTER_FOLDER_NAME,
|
(folder) => folder.name !== STARTER_FOLDER_NAME,
|
||||||
);
|
);
|
||||||
|
|
@ -33,38 +34,73 @@ export const useFolderStore = create<FoldersStoreType>((set, get) => ({
|
||||||
|
|
||||||
set({ myCollectionId });
|
set({ myCollectionId });
|
||||||
|
|
||||||
if (refetch === true) {
|
const { refreshFlows } = useFlowsManagerStore.getState();
|
||||||
useFlowsManagerStore.getState().refreshFlows();
|
const { getTypes } = useTypesStore.getState();
|
||||||
useFlowsManagerStore.getState().setAllFlows;
|
const { setIsLoadingFolders } = get();
|
||||||
}
|
|
||||||
|
if (refetch) {
|
||||||
|
if (startupApplication) {
|
||||||
|
await refreshFlows();
|
||||||
|
await getTypes();
|
||||||
|
} else {
|
||||||
|
refreshFlows();
|
||||||
|
getTypes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setIsLoadingFolders(false);
|
||||||
|
|
||||||
get().setLoading(false);
|
|
||||||
resolve();
|
resolve();
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
set({ folders: [] });
|
set({ folders: [] });
|
||||||
get().setLoading(false);
|
get().setIsLoadingFolders(false);
|
||||||
reject(error);
|
reject(error);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
refreshFolders: () => {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
getFolders().then(
|
||||||
|
async (res) => {
|
||||||
|
const foldersWithoutStarterProjects = res?.filter(
|
||||||
|
(folder) => folder.name !== STARTER_FOLDER_NAME,
|
||||||
|
);
|
||||||
|
|
||||||
|
const starterProjects = res?.find(
|
||||||
|
(folder) => folder.name === STARTER_FOLDER_NAME,
|
||||||
|
);
|
||||||
|
|
||||||
|
set({ starterProjectId: starterProjects!.id ?? "" });
|
||||||
|
set({ folders: foldersWithoutStarterProjects });
|
||||||
|
|
||||||
|
const myCollectionId = res?.find(
|
||||||
|
(f) => f.name === DEFAULT_FOLDER,
|
||||||
|
)?.id;
|
||||||
|
|
||||||
|
set({ myCollectionId });
|
||||||
|
|
||||||
|
resolve();
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
set({ folders: [] });
|
||||||
|
get().setIsLoadingFolders(false);
|
||||||
|
reject(error);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
},
|
||||||
setFolders: (folders) => set(() => ({ folders: folders })),
|
setFolders: (folders) => set(() => ({ folders: folders })),
|
||||||
loading: false,
|
isLoadingFolders: false,
|
||||||
setLoading: (loading) => set(() => ({ loading: loading })),
|
setIsLoadingFolders: (isLoadingFolders) => set(() => ({ isLoadingFolders })),
|
||||||
getFolderById: (id) => {
|
getFolderById: (id) => {
|
||||||
if (id) {
|
if (id) {
|
||||||
getFolderById(id).then(
|
getFolderById(id).then((res) => {
|
||||||
(res) => {
|
|
||||||
const setAllFlows = useFlowsManagerStore.getState().setAllFlows;
|
const setAllFlows = useFlowsManagerStore.getState().setAllFlows;
|
||||||
setAllFlows(res.flows);
|
setAllFlows(res.flows);
|
||||||
set({ selectedFolder: res });
|
set({ selectedFolder: res });
|
||||||
},
|
});
|
||||||
() => {
|
|
||||||
get().getFoldersApi(true);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
selectedFolder: null,
|
selectedFolder: null,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue