Skip to content

Commit

Permalink
fix: getall is being called unnecessarily every time the user accesse…
Browse files Browse the repository at this point in the history
…s the My Collection page (#3681)

* ✨ (use-get-folders.ts): Update useGetFoldersQuery to check if types are empty before calling getTypes to avoid unnecessary API calls
♻️ (typesStore.ts): Remove unnecessary useAlertStore setState call to improve code readability and maintainability

* feat: Update getTypes function to always force refresh

The `getTypes` function in `typesStore.ts` has been updated to always force a refresh of the types data. This change ensures that the latest data is fetched from the API, avoiding unnecessary API calls.

* Changed types check in other pages

---------

Co-authored-by: Lucas Oliveira <lucas.edu.oli@hotmail.com>
  • Loading branch information
Cristhianzl and lucaseduoli authored Sep 4, 2024
1 parent de1fdff commit a670a7c
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export const useGetFoldersQuery: useQueryFunctionType<
const myCollectionId = data?.find((f) => f.name === DEFAULT_FOLDER)?.id;
setMyCollectionId(myCollectionId);

const { getTypes } = useTypesStore.getState();
const { getTypes, types } = useTypesStore.getState();

await refreshFlows(undefined);
await getTypes();
if (!types || Object.keys(types).length === 0) await getTypes();

return foldersWithoutStarterProjects;
};
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/pages/FlowPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function FlowPage({ view }: { view?: boolean }): JSX.Element {
const { mutateAsync: refreshFlows } = useGetRefreshFlows();
const setIsLoading = useFlowsManagerStore((state) => state.setIsLoading);
const getTypes = useTypesStore((state) => state.getTypes);
const types = useTypesStore((state) => state.types);

const updatedAt = currentSavedFlow?.updated_at;

Expand Down Expand Up @@ -74,7 +75,7 @@ export default function FlowPage({ view }: { view?: boolean }): JSX.Element {
} else if (!flows) {
setIsLoading(true);
await refreshFlows(undefined);
await getTypes();
if (!types || Object.keys(types).length === 0) await getTypes();
setIsLoading(false);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/pages/Playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function PlaygroundPage() {
const { mutateAsync: refreshFlows } = useGetRefreshFlows();
const setIsLoading = useFlowsManagerStore((state) => state.setIsLoading);
const getTypes = useTypesStore((state) => state.getTypes);
const types = useTypesStore((state) => state.types);

// Set flow tab id
useEffect(() => {
Expand All @@ -49,7 +50,7 @@ export default function PlaygroundPage() {
} else if (!flows) {
setIsLoading(true);
await refreshFlows(undefined);
await getTypes();
if (!types || Object.keys(types).length === 0) await getTypes();
setIsLoading(false);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/pages/ViewPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function ViewPage() {
const { mutateAsync: refreshFlows } = useGetRefreshFlows();
const setIsLoading = useFlowsManagerStore((state) => state.setIsLoading);
const getTypes = useTypesStore((state) => state.getTypes);
const types = useTypesStore((state) => state.types);

// Set flow tab id
useEffect(() => {
Expand All @@ -33,7 +34,7 @@ export default function ViewPage() {
} else if (!flows) {
setIsLoading(true);
await refreshFlows(undefined);
await getTypes();
if (!types || Object.keys(types).length === 0) await getTypes();
setIsLoading(false);
}
};
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/src/stores/typesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ export const useTypesStore = create<TypesStoreType>((set, get) => ({
types: {},
templates: {},
data: {},
getTypes: (force_refresh: boolean = false) => {
getTypes: (force_refresh: boolean = true) => {
return new Promise<void>(async (resolve, reject) => {
const setLoading = useFlowsManagerStore.getState().setIsLoading;
getAll(force_refresh)
.then((response) => {
const data = response?.data;
useAlertStore.setState({ loading: false });
set((old) => ({
types: typesGenerator(data),
data: { ...old.data, ...data },
Expand Down

0 comments on commit a670a7c

Please sign in to comment.