Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null check annoucementsMeta #2831

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/providers/queries/announcementsQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -24,19 +24,21 @@ export const useAnnouncementsQuery = () => {
if (announcmentsQuery.data?.length > 0) {
const firstAnnounce = announcmentsQuery.data[0];

const _meta = 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
) {
return;
}

const _markAsSeen = () => {
dispatch(updateAnnoucementsMeta(firstAnnounce.id, false));
dispatch(updateAnnoucementsMeta(_metaId, false));
};

const _onActionPress = () => {
Expand All @@ -50,7 +52,7 @@ export const useAnnouncementsQuery = () => {
}

// mark as processed
dispatch(updateAnnoucementsMeta(firstAnnounce.id, true));
dispatch(updateAnnoucementsMeta(_metaId, true));
};

const _buttons = [
Expand Down
12 changes: 6 additions & 6 deletions src/redux/actions/cacheActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
43 changes: 22 additions & 21 deletions src/redux/reducers/cacheReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -87,8 +87,8 @@ export interface SubscribedCommunity {
}

export interface AnnouncementMeta {
lastSeen:number,
processed:boolean,
lastSeen: number;
processed: boolean;
}

export interface LastUpdateMeta {
Expand All @@ -104,7 +104,7 @@ interface State {
claimsCollection: ClaimsCollection;
subscribedCommunities: Map<string, SubscribedCommunity>;
pointActivities: Map<string, PointActivity>;
announcementsMeta: { [key: number]: AnnouncementMeta }
announcementsMeta: { [key: string]: AnnouncementMeta };
lastUpdate: LastUpdateMeta;
}

Expand All @@ -113,7 +113,7 @@ const initialState: State = {
commentsCollection: {},
draftsCollection: {},
claimsCollection: {},
announcementsMeta:{},
announcementsMeta: {},
subscribedCommunities: new Map(),
pointActivities: new Map(),
lastUpdate: null,
Expand Down Expand Up @@ -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();
Expand Down