From ab06f432fedb80768355f4db27259a07bd79baf8 Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Wed, 7 Feb 2024 22:31:19 +0500 Subject: [PATCH 1/2] null check annoucementsMeta --- src/providers/queries/announcementsQueries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/providers/queries/announcementsQueries.ts b/src/providers/queries/announcementsQueries.ts index b5740518a8..765998c092 100644 --- a/src/providers/queries/announcementsQueries.ts +++ b/src/providers/queries/announcementsQueries.ts @@ -24,7 +24,7 @@ export const useAnnouncementsQuery = () => { if (announcmentsQuery.data?.length > 0) { const firstAnnounce = announcmentsQuery.data[0]; - const _meta = announcementsMeta[firstAnnounce.id]; + const _meta = announcementsMeta && announcementsMeta[firstAnnounce.id]; const curTime = new Date().getTime(); if ( From 37591da2f370abc054c1d4d1b13d15335e400126 Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Wed, 7 Feb 2024 23:42:16 +0500 Subject: [PATCH 2/2] making sure announcements meta is user specific --- src/providers/queries/announcementsQueries.ts | 12 +++--- src/redux/actions/cacheActions.ts | 12 +++--- src/redux/reducers/cacheReducer.ts | 43 ++++++++++--------- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/providers/queries/announcementsQueries.ts b/src/providers/queries/announcementsQueries.ts index 765998c092..61a025cb80 100644 --- a/src/providers/queries/announcementsQueries.ts +++ b/src/providers/queries/announcementsQueries.ts @@ -15,7 +15,7 @@ export const useAnnouncementsQuery = () => { const intl = useIntl(); const dispatch = useDispatch(); - const isLoggedIn = useAppSelector((state) => state.application.isLoggedIn); + const currentAccount = useAppSelector((state) => state.account.currentAccount); const announcementsMeta = useAppSelector((state) => state.cache.announcementsMeta); const announcmentsQuery = useQuery([QUERIES.ANNOUNCEMENTS.GET], getAnnouncements); @@ -24,11 +24,13 @@ export const useAnnouncementsQuery = () => { if (announcmentsQuery.data?.length > 0) { const firstAnnounce = announcmentsQuery.data[0]; - const _meta = announcementsMeta && announcementsMeta[firstAnnounce.id]; + const _metaId = `${firstAnnounce.id}_${currentAccount?.username || 'guest'}`; + + const _meta = announcementsMeta && announcementsMeta[_metaId]; const curTime = new Date().getTime(); if ( - (firstAnnounce.auth && !isLoggedIn) || + (firstAnnounce.auth && !currentAccount?.username) || _meta?.processed || _meta?.lastSeen + PROMPT_AGAIN_INTERVAL > curTime ) { @@ -36,7 +38,7 @@ export const useAnnouncementsQuery = () => { } const _markAsSeen = () => { - dispatch(updateAnnoucementsMeta(firstAnnounce.id, false)); + dispatch(updateAnnoucementsMeta(_metaId, false)); }; const _onActionPress = () => { @@ -50,7 +52,7 @@ export const useAnnouncementsQuery = () => { } // mark as processed - dispatch(updateAnnoucementsMeta(firstAnnounce.id, true)); + dispatch(updateAnnoucementsMeta(_metaId, true)); }; const _buttons = [ diff --git a/src/redux/actions/cacheActions.ts b/src/redux/actions/cacheActions.ts index 29063da2a8..b4e1f9b3f7 100644 --- a/src/redux/actions/cacheActions.ts +++ b/src/redux/actions/cacheActions.ts @@ -16,7 +16,7 @@ import { UPDATE_POINT_ACTIVITY_CACHE, UPDATE_CLAIM_CACHE, DELETE_CLAIM_CACHE_ENTRY, - UPDATE_ANNOUNCEMENTS_META + UPDATE_ANNOUNCEMENTS_META, } from '../constants/constants'; import { Comment, @@ -168,13 +168,13 @@ export const deletePointActivityCache = (id: string) => ({ type: DELETE_POINT_ACTIVITY_CACHE_ENTRY, }); -export const updateAnnoucementsMeta = (id:number, processed:boolean) => ({ - payload:{ +export const updateAnnoucementsMeta = (id: string, processed: boolean) => ({ + payload: { id, - processed + processed, }, - type: UPDATE_ANNOUNCEMENTS_META -}) + type: UPDATE_ANNOUNCEMENTS_META, +}); export const purgeExpiredCache = () => ({ type: PURGE_EXPIRED_CACHE, diff --git a/src/redux/reducers/cacheReducer.ts b/src/redux/reducers/cacheReducer.ts index 99e0173328..f410a269e8 100644 --- a/src/redux/reducers/cacheReducer.ts +++ b/src/redux/reducers/cacheReducer.ts @@ -13,7 +13,7 @@ import { DELETE_POINT_ACTIVITY_CACHE_ENTRY, UPDATE_CLAIM_CACHE, DELETE_CLAIM_CACHE_ENTRY, - UPDATE_ANNOUNCEMENTS_META + UPDATE_ANNOUNCEMENTS_META, } from '../constants/constants'; export enum CacheStatus { @@ -87,8 +87,8 @@ export interface SubscribedCommunity { } export interface AnnouncementMeta { - lastSeen:number, - processed:boolean, + lastSeen: number; + processed: boolean; } export interface LastUpdateMeta { @@ -104,7 +104,7 @@ interface State { claimsCollection: ClaimsCollection; subscribedCommunities: Map; pointActivities: Map; - announcementsMeta: { [key: number]: AnnouncementMeta } + announcementsMeta: { [key: string]: AnnouncementMeta }; lastUpdate: LastUpdateMeta; } @@ -113,7 +113,7 @@ const initialState: State = { commentsCollection: {}, draftsCollection: {}, claimsCollection: {}, - announcementsMeta:{}, + announcementsMeta: {}, subscribedCommunities: new Map(), pointActivities: new Map(), lastUpdate: null, @@ -256,22 +256,23 @@ const cacheReducer = (state = initialState, action) => { } return { ...state }; - case UPDATE_ANNOUNCEMENTS_META: - if (!state.announcementsMeta) { - state.announcementsMeta = {}; - } - - const _alreadyProcessed = state.announcementsMeta[payload.id]?.processed || false - - state.announcementsMeta = { - ...state.announcementsMeta, - [payload.id]: { - processed: _alreadyProcessed || payload.processed, - lastSeen:new Date().getTime(), - } as AnnouncementMeta }; - return { - ...state, // spread operator in requried here, otherwise persist do not register change - }; + case UPDATE_ANNOUNCEMENTS_META: + if (!state.announcementsMeta) { + state.announcementsMeta = {}; + } + + const _alreadyProcessed = state.announcementsMeta[payload.id]?.processed || false; + + state.announcementsMeta = { + ...state.announcementsMeta, + [payload.id]: { + processed: _alreadyProcessed || payload.processed, + lastSeen: new Date().getTime(), + } as AnnouncementMeta, + }; + return { + ...state, // spread operator in requried here, otherwise persist do not register change + }; case PURGE_EXPIRED_CACHE: const currentTime = new Date().getTime();