Skip to content

Commit

Permalink
Merge branch 'staging' into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
#	yarn.lock
  • Loading branch information
andreymikhadyuk committed Jan 8, 2024
2 parents 4116d41 + 525e03a commit ebbe62c
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 83 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
"redux-persist": "^6.0.0",
"redux-saga": "^1.1.3",
"reselect": "^4.0.0",
"slate": "0.90.0",
"slate-history": "^0.86.0",
"slate": "0.87.0",
"slate-history": "0.86.0",
"slate-react": "0.90.0",
"styled-components": "^5.2.1",
"swiper": "^6.5.9",
Expand Down
61 changes: 39 additions & 22 deletions src/pages/OldCommon/hooks/useCommonMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import {
CommonEvent,
} from "@/events";
import { selectUser } from "@/pages/Auth/store/selectors";
import { ErrorCode } from "@/shared/constants";
import { LoadingState } from "@/shared/interfaces";
import {
Circles,
CirclesPermissions,
CommonMember,
Governance,
} from "@/shared/models";
import { generateCirclesDataForCommonMember } from "@/shared/utils";
import {
generateCirclesDataForCommonMember,
isGeneralError,
} from "@/shared/utils";
import { CommonService, GovernanceService, Logger } from "../../../services";

interface Options {
Expand All @@ -38,6 +42,7 @@ interface Return extends State {
commonMember: (CommonMember & CirclesPermissions) | null,
) => void;
resetCommonMember: () => void;
missingCirclesError: boolean;
}

export const useCommonMember = (options: Options = {}): Return => {
Expand All @@ -52,6 +57,7 @@ export const useCommonMember = (options: Options = {}): Return => {
fetched: false,
data: null,
});
const [missingCirclesError, setMissingCirclesError] = useState(false);
const user = useSelector(selectUser());
const userId = options.userId || user?.uid;
const commonMemberId = state.data?.id;
Expand Down Expand Up @@ -189,29 +195,39 @@ export const useCommonMember = (options: Options = {}): Return => {
commonId,
userId,
(commonMember, { isAdded, isRemoved }) => {
let data: State["data"] = null;

if (isAdded) {
CommonEventEmitter.emit(CommonEvent.ProjectUpdated, {
commonId,
hasMembership: true,
try {
let data: State["data"] = null;

if (isAdded) {
CommonEventEmitter.emit(CommonEvent.ProjectUpdated, {
commonId,
hasMembership: true,
});
}
if (!isRemoved) {
data = {
...commonMember,
...generateCirclesDataForCommonMember(
governanceCircles,
commonMember.circleIds,
),
};
}

setState({
loading: false,
fetched: true,
data,
});
} catch (err) {
if (
isGeneralError(err) &&
(err.code === ErrorCode.CCircleInGovernanceNotFound ||
err.code === ErrorCode.CCircleIndexValidationFailure)
) {
setMissingCirclesError(true);
}
}
if (!isRemoved) {
data = {
...commonMember,
...generateCirclesDataForCommonMember(
governanceCircles,
commonMember.circleIds,
),
};
}

setState({
loading: false,
fetched: true,
data,
});
},
);

Expand All @@ -223,5 +239,6 @@ export const useCommonMember = (options: Options = {}): Return => {
fetchCommonMember,
setCommonMember,
resetCommonMember,
missingCirclesError,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ export default function ChatComponent({
markChatMessageItemAsSeen({
chatChannelId: feedItemId,
});
} else {
} else if (commonId) {
markDiscussionMessageItemAsSeen({
feedObjectId: feedItemId,
commonId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useFeedItemCounters = (
commonId?: string,
): Return => {
const { data: governance, fetchGovernance } = useGovernanceByCommonId();
const { data: commonMember } = useCommonMember({
const { data: commonMember, missingCirclesError } = useCommonMember({
shouldAutoReset: false,
withSubscription: true,
governanceCircles: governance?.circles,
Expand All @@ -27,6 +27,12 @@ export const useFeedItemCounters = (
}
}, [fetchGovernance, commonId]);

useEffect(() => {
if (missingCirclesError && commonId) {
fetchGovernance(commonId, true);
}
}, [missingCirclesError]);

return {
projectUnreadStreamsCount: streamsUnreadCountByProjectStream?.[feedItemId],
projectUnreadMessages: unreadCountByProjectStream?.[feedItemId],
Expand Down
11 changes: 9 additions & 2 deletions src/pages/commonFeed/hooks/useCommonData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CommonService,
GovernanceService,
} from "@/services";
import { FirestoreDataSource } from "@/shared/constants";
import { getRootCommon } from "@/shared/hooks/useCases/useFullCommonData";
import { useCommonSubscription } from "@/shared/hooks/useCases/useFullCommonData/useCommonSubscription";
import { State, CombinedState } from "./types";
Expand Down Expand Up @@ -48,7 +49,10 @@ export const useCommonData = (userId?: string): Return => {
try {
const [common, governance, sharedFeedItem] = await Promise.all([
CommonService.getCommonById(commonId, true),
GovernanceService.getGovernanceByCommonId(commonId, true),
GovernanceService.getGovernanceByCommonId(
commonId,
FirestoreDataSource.Cache,
),
sharedFeedItemId
? CommonFeedService.getCommonFeedItemById(
commonId,
Expand All @@ -73,7 +77,10 @@ export const useCommonData = (userId?: string): Return => {
CommonService.getAllParentCommonsForCommon(common, true),
CommonService.getCommonsByDirectParentId(common.id, true),
rootCommonId
? GovernanceService.getGovernanceByCommonId(rootCommonId, true)
? GovernanceService.getGovernanceByCommonId(
rootCommonId,
FirestoreDataSource.Cache,
)
: null,
]);
const rootCommon = await getRootCommon(
Expand Down
6 changes: 0 additions & 6 deletions src/services/Discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,8 @@ class DiscussionService {
const snapshot = await this.getDiscussionCollection()
.doc(discussionId)
.get({ source });
const fromCache = snapshot.metadata.fromCache ? "local cache" : "server";
const discussion = snapshot?.data() || null;

console.log(
`getDiscussionById [${fromCache}]`,
discussionId,
snapshot?.data() || null,
);
if (!discussion && source === FirestoreDataSource.Cache) {
return this.getDiscussionById(discussionId, FirestoreDataSource.Server);
}
Expand Down
10 changes: 5 additions & 5 deletions src/services/Governance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { governanceCollection } from "@/pages/OldCommon/store/api";
import { DocChange } from "@/shared/constants";
import { DocChange, FirestoreDataSource } from "@/shared/constants";
import { UnsubscribeFunction } from "@/shared/interfaces";
import { Collection, Governance } from "@/shared/models";
import {
Expand All @@ -13,17 +13,17 @@ const converter = firestoreDataConverter<Governance>();
class GovernanceService {
public getGovernanceByCommonId = async (
commonId: string,
cached = false,
source = FirestoreDataSource.Default,
): Promise<Governance | null> => {
const snapshot = await governanceCollection
.where("commonId", "==", commonId)
.withConverter(converter)
.get({ source: cached ? "cache" : "default" });
.get({ source });
const governanceList = snapshot.docs.map((doc) => doc.data());
const governance = governanceList[0] || null;

if (cached && !governance) {
return this.getGovernanceByCommonId(commonId);
if (source === FirestoreDataSource.Cache && !governance) {
return this.getGovernanceByCommonId(commonId, FirestoreDataSource.Server);
}

return governance;
Expand Down
3 changes: 0 additions & 3 deletions src/services/Proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ class ProposalService {
const proposal = snapshot.data();

if (proposal) {
console.log("proposal found!", proposalId);
callback(proposal);
} else {
console.log("proposal was not found", proposalId);
}
});
};
Expand Down
2 changes: 2 additions & 0 deletions src/shared/constants/errorCode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export enum ErrorCode {
// Front-end error codes
CUserDoesNotExist = "CUserDoesNotExist",
CCircleInGovernanceNotFound = "CCircleInGovernanceNotFound",
CCircleIndexValidationFailure = "CCircleIndexValidationFailure",

// Back-end error codes
ArgumentDuplicatedError = "ArgumentDuplicatedError",
Expand Down
6 changes: 3 additions & 3 deletions src/shared/hooks/useCases/useGovernanceByCommonId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { cacheActions, selectGovernanceStateByCommonId } from "@/store/states";
type State = LoadingState<Governance | null>;

interface Return extends State {
fetchGovernance: (commonId: string) => void;
fetchGovernance: (commonId: string, force?: boolean) => void;
setGovernance: (governance: Governance | null) => void;
}

Expand All @@ -28,12 +28,12 @@ export const useGovernanceByCommonId = (): Return => {
const governanceId = state.data?.id || "";

const fetchGovernance = useCallback(
(commonId: string) => {
(commonId: string, force = false) => {
setDefaultState({ ...DEFAULT_STATE });
setCurrentCommonId(commonId);
dispatch(
cacheActions.getGovernanceStateByCommonId.request({
payload: { commonId },
payload: { commonId, force },
}),
);
},
Expand Down
39 changes: 28 additions & 11 deletions src/shared/utils/generateCircleDataForCommonMember.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { GeneralError } from "@/shared/utils/error";
import { generateCirclesBinaryNumber } from "../../pages/OldCommon/components/CommonDetailContainer/CommonWhitepaper/utils";
import { GovernanceActions, ProposalsTypes } from "../constants";
import { ErrorCode, GovernanceActions, ProposalsTypes } from "../constants";
import { CircleIndex, CirclesPermissions, Governance } from "../models";

export const circleIndexGuard = (n: number | string) => {
const castedN = Number(n);

if (castedN < 0 || castedN > 31)
throw new Error("Circle index validation failed");
if (castedN < 0 || castedN > 31) {
throw new GeneralError({
message: "Circle index validation failed",
code: ErrorCode.CCircleIndexValidationFailure,
});
}

return castedN as CircleIndex;
};
Expand All @@ -25,8 +30,12 @@ export const generateCirclesDataForCommonMember = (
const circle = Object.entries(governanceCircles).find(
([_, circle]) => circle.id === id,
);
if (!circle || !circle[1])
throw new Error(`could not find circle in governance ${id}`);
if (!circle || !circle[1]) {
throw new GeneralError({
message: `could not find circle in governance ${id}`,
code: ErrorCode.CCircleInGovernanceNotFound,
});
}
circleIdsByHierarchy.add(circle[1].id);
circlesIndexesByHierarchy.add(circleIndexGuard(circle[0]));

Expand Down Expand Up @@ -61,8 +70,12 @@ export const generateCirclesDataForCommonMember = (
([_, circle]) => circle.id === circleId,
);

if (!circle) throw new Error("could not find cirlce in governance");

if (!circle) {
throw new GeneralError({
message: "could not find cirlce in governance",
code: ErrorCode.CCircleInGovernanceNotFound,
});
}
Object.keys(circle[1].allowedActions).forEach((action) => {
allowedActions.add(action as GovernanceActions);
});
Expand Down Expand Up @@ -95,10 +108,14 @@ export const generateCirclesDataForCommonMember = (
const circlesMap = Array.from(circlesIndexesByHierarchy).reduce(
(prev, next) => {
const circle = governanceCircles[next];
if (!circle)
throw new Error(
`could not find cirlce in governance ${JSON.stringify(circle)}`,
);
if (!circle) {
throw new GeneralError({
message: `could not find cirlce in governance ${JSON.stringify(
circle,
)}`,
code: ErrorCode.CCircleInGovernanceNotFound,
});
}
circleIdsByHierarchy.add(circle[1]?.id);
return { ...prev, [next]: circle.id };
},
Expand Down
19 changes: 12 additions & 7 deletions src/store/states/cache/saga/getGovernanceStateByCommonId.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { call, put, select } from "redux-saga/effects";
import { GovernanceService } from "@/services";
import { FirestoreDataSource } from "@/shared/constants";
import { Awaited, LoadingState } from "@/shared/interfaces";
import { Governance } from "@/shared/models";
import { isError } from "@/shared/utils";
Expand All @@ -16,13 +17,15 @@ type State = LoadingState<Governance | null>;
export function* getGovernanceStateByCommonId({
payload,
}: ReturnType<typeof getStateAction.request>) {
const { commonId } = payload.payload;
const { commonId, force } = payload.payload;

try {
const state = ((yield select(selectState(commonId))) as State) || null;
if (!force) {
const state = ((yield select(selectState(commonId))) as State) || null;

if (state?.fetched || state?.loading) {
return;
if (state?.fetched || state?.loading) {
return;
}
}

yield put(
Expand All @@ -35,9 +38,11 @@ export function* getGovernanceStateByCommonId({
},
}),
);
const data = (yield call(requestFunction, commonId)) as Awaited<
ReturnType<typeof requestFunction>
>;
const data = (yield call(
requestFunction,
commonId,
force ? FirestoreDataSource.Server : FirestoreDataSource.Default,
)) as Awaited<ReturnType<typeof requestFunction>>;

yield put(
updateStateAction({
Expand Down
Loading

0 comments on commit ebbe62c

Please sign in to comment.