fix: getall is being called unnecessarily every time the user accesses 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>
This commit is contained in:
Cristhian Zanforlin Lousa 2024-09-04 13:48:09 -03:00 committed by GitHub
commit a670a7cb0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 9 additions and 7 deletions

View file

@ -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;
};

View file

@ -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;
@ -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);
}
};

View file

@ -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(() => {
@ -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);
}
};

View file

@ -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(() => {
@ -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);
}
};

View file

@ -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 },