diff --git a/src/pages/commonFeed/hooks/useCommonData/index.ts b/src/pages/commonFeed/hooks/useCommonData/index.ts index 633bfc49c5..20e5efdbba 100644 --- a/src/pages/commonFeed/hooks/useCommonData/index.ts +++ b/src/pages/commonFeed/hooks/useCommonData/index.ts @@ -71,7 +71,7 @@ export const useCommonData = (userId?: string): Return => { const [parentCommons, subCommons, rootCommonGovernance] = await Promise.all([ CommonService.getAllParentCommonsForCommon(common, true), - CommonService.getCommonsByDirectParentIds([common.id]), + CommonService.getCommonsByDirectParentId(common.id, true), rootCommonId ? GovernanceService.getGovernanceByCommonId(rootCommonId, true) : null, diff --git a/src/services/Common.ts b/src/services/Common.ts index 7415f283de..4ba3541c9a 100644 --- a/src/services/Common.ts +++ b/src/services/Common.ts @@ -101,6 +101,26 @@ class CommonService { .reduce((acc, items) => [...acc, ...items], []); }; + public getCommonsByDirectParentId = async ( + parentCommonId: string, + cached = false, + ): Promise => { + const snapshot = await firebase + .firestore() + .collection(Collection.Daos) + .where("state", "==", CommonState.ACTIVE) + .where("directParent.commonId", "==", parentCommonId) + .withConverter(converter) + .get({ source: cached ? "cache" : "default" }); + const commons = snapshot.docs.map((doc) => doc.data()); + + if (cached && commons.length === 0) { + return this.getCommonsByDirectParentId(parentCommonId); + } + + return commons; + }; + public getCommonsByDirectParentIds = async ( ids: string[], ): Promise => {