Skip to content

Commit

Permalink
add logic to fetch sub-commons with cache
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Nov 21, 2023
1 parent c722fdf commit a30f2a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/pages/commonFeed/hooks/useCommonData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions src/services/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ class CommonService {
.reduce((acc, items) => [...acc, ...items], []);
};

public getCommonsByDirectParentId = async (
parentCommonId: string,
cached = false,
): Promise<Common[]> => {
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<Common[]> => {
Expand Down

0 comments on commit a30f2a4

Please sign in to comment.