Skip to content

Commit

Permalink
add rootCommonId to last common from feed and change persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Nov 14, 2023
1 parent 7525283 commit 2979a19
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/pages/commonFeed/CommonFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
useEffect(() => {
return () => {
const common = stateRef.current?.data?.common;
const rootCommon = stateRef.current?.data?.rootCommon;

dispatch(
commonLayoutActions.setLastCommonFromFeed({
Expand All @@ -426,6 +427,19 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
image: common.image,
isProject: checkIsProject(common),
memberCount: common.memberCount,
rootCommon: common.rootCommonId
? {
id: common.rootCommonId,
data: rootCommon
? {
name: rootCommon.name,
image: rootCommon.image,
isProject: false,
memberCount: rootCommon.memberCount,
}
: null,
}
: null,
}
: null,
}),
Expand Down
29 changes: 20 additions & 9 deletions src/store/states/commonLayout/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { ProjectsStateItem } from "../projects";

interface LastCommonFromFeedData {
name: string;
image: string;
isProject: boolean;
memberCount: number;
}

interface LastCommonFromFeed {
id: string;
data:
| (LastCommonFromFeedData & {
rootCommon: {
id: string;
data: LastCommonFromFeedData | null;
} | null;
})
| null;
}

export interface CommonLayoutState {
currentCommonId: string | null;
lastCommonFromFeed: {
id: string;
data: {
name: string;
image: string;
isProject: boolean;
memberCount: number;
} | null;
} | null;
lastCommonFromFeed: LastCommonFromFeed | null;
commons: ProjectsStateItem[];
areCommonsLoading: boolean;
areCommonsFetched: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/store/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import createSagaMiddleware from "redux-saga";
import { AppState } from "@/shared/interfaces";
import rootReducer from "./reducer";
import appSagas from "./saga";
import { inboxTransform } from "./transforms";
import { inboxTransform, lastCommonFromFeedTransform } from "./transforms";

const persistConfig: PersistConfig<AppState> = {
key: "root",
Expand All @@ -32,7 +32,7 @@ const persistConfig: PersistConfig<AppState> = {
"multipleSpacesLayout",
],
stateReconciler: autoMergeLevel2,
transforms: [inboxTransform],
transforms: [inboxTransform, lastCommonFromFeedTransform],
};

const sagaMiddleware = createSagaMiddleware();
Expand Down
22 changes: 22 additions & 0 deletions src/store/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createTransform } from "redux-persist";
import { deserializeFeedLayoutItemWithFollowData } from "@/shared/interfaces";
import { convertObjectDatesToFirestoreTimestamps } from "@/shared/utils";
import { getFeedLayoutItemDateForSorting } from "@/store/states/inbox/utils";
import { CommonLayoutState } from "./states/commonLayout";
import { InboxItems, InboxState } from "./states/inbox";

export const inboxTransform = createTransform(
Expand Down Expand Up @@ -43,3 +44,24 @@ export const inboxTransform = createTransform(
}),
{ whitelist: ["inbox"] },
);

export const lastCommonFromFeedTransform = createTransform(
(inboundState: CommonLayoutState) => {
const rootCommon = inboundState.lastCommonFromFeed?.data?.rootCommon;

return {
...inboundState,
lastCommonFromFeed: rootCommon
? {
id: rootCommon.id,
data: rootCommon.data && {
...rootCommon.data,
rootCommon: null,
},
}
: inboundState.lastCommonFromFeed,
};
},
(outboundState: CommonLayoutState) => outboundState,
{ whitelist: ["commonLayout"] },
);

0 comments on commit 2979a19

Please sign in to comment.