Skip to content

Commit de123fc

Browse files
added redux
1 parent 1855036 commit de123fc

4 files changed

Lines changed: 63 additions & 28 deletions

File tree

src/components/ViewChannelItem.jsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ import { ImageV2 } from './reusables/SharedStylingV2';
3939
import Tooltip from './reusables/tooltip/Tooltip';
4040
import UpdateChannelTooltipContent from './UpdateChannelTooltipContent';
4141
import VerifiedTooltipContent from './VerifiedTooltipContent';
42+
import { setAllowNotifModalVisibility } from 'redux/slices/modalSlice';
4243

4344
// Internal Configs
4445
import APP_PATHS from 'config/AppPaths';
45-
import { addresses, appConfig, CHAIN_DETAILS, ALLOW_NOTIF_MODAL } from 'config/index.js';
46+
import { addresses, appConfig, CHAIN_DETAILS, ALLOW_NOTIF_MODAL_LAST_TIMESTAMP } from 'config/index.js';
4647
import { IPFSGateway } from 'helpers/IpfsHelper';
4748
import { checkPermission } from 'helpers/channel/allowNotification';
4849

@@ -60,6 +61,7 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
6061
(state) => state.contracts
6162
);
6263
const { canVerify, channelDetails, coreChannelAdmin } = useSelector((state) => state.admin);
64+
const { isAllowNotifModalVisible } = useSelector((state) => state.modal);
6365
const {
6466
channelsCache,
6567
CHANNEL_BLACKLIST,
@@ -87,7 +89,7 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
8789
const [channelObjectFromHash, setChannelObjectFromHash] = React.useState({});
8890
const [channelObjectStartBlock, setChannelObjectStartBlock] = React.useState({});
8991
const [showChannelChangedWarning, setShowChannelChangedWarning] = React.useState(false);
90-
const [showAllowNotification, setShowAllowNotification] = useState(false);
92+
// const [isAllowNotifModalVisible, setAllowNotifModalVisibility] = useState(false);
9193

9294

9395
const isVerified = channelObject.verified_status;
@@ -109,11 +111,11 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
109111
}, [subscriptionStatus]);
110112

111113
useEffect(() => {
112-
if (showAllowNotification)
114+
if (isAllowNotifModalVisible)
113115
setTimeout(() => {
114-
setShowAllowNotification(false);
116+
dispatch(setAllowNotifModalVisibility({ flag: false }));
115117
}, 10000);
116-
}, [showAllowNotification]);
118+
}, [isAllowNotifModalVisible]);
117119

118120
useEffect(() => {
119121
setIsPushAdmin(pushAdminAddress == account);
@@ -417,19 +419,19 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
417419
the modal was last opened 24 hours before
418420
*/
419421
if (checkPermission() === 'pending') {
420-
let lastTime = localStorage.getItem(ALLOW_NOTIF_MODAL);
422+
let lastTime = localStorage.getItem(ALLOW_NOTIF_MODAL_LAST_TIMESTAMP);
421423
let today = new Date().getTime() + (1 * 24 * 60 * 60 * 1000);
422424
if (lastTime) {
423425
lastTime = parseInt(lastTime);
424426
if (lastTime >= today) {
425-
setShowAllowNotification(true);
427+
dispatch(setAllowNotifModalVisibility({ flag: true }));
426428
}
427429
}
428430
else {
429-
setShowAllowNotification(true);
431+
dispatch(setAllowNotifModalVisibility({ flag: true }));
430432
}
431433
//sets the recent opened time in localstorage.
432-
localStorage.setItem(ALLOW_NOTIF_MODAL, today);
434+
localStorage.setItem(ALLOW_NOTIF_MODAL_LAST_TIMESTAMP, today);
433435
}
434436

435437
}
@@ -1055,8 +1057,10 @@ function ViewChannelItem({ channelObjectProp, loadTeaser, playTeaser, minimal, p
10551057
/>
10561058
)}
10571059
{/* modal to allow notification */}
1058-
{showAllowNotification &&
1059-
<AllowNotificationModal onModalClose={() => setShowAllowNotification(false)} />
1060+
{isAllowNotifModalVisible &&
1061+
<AllowNotificationModal onModalClose={() =>
1062+
dispatch(setAllowNotifModalVisibility({ flag: false }))}
1063+
/>
10601064
}
10611065
</Container>
10621066
);

src/config/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ export { CHAIN_DETAILS, abis, addresses, appConfig };
4040

4141
export const defaultSnapOrigin = 'npm:@pushprotocol/snap';
4242

43-
export const ALLOW_NOTIF_MODAL = 'ALLOW_NOTIF_MODAL';
43+
export const ALLOW_NOTIF_MODAL_LAST_TIMESTAMP = 'ALLOW_NOTIF_MODAL_LAST_TIMESTAMP';

src/redux/slices/modalSlice.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* This file helps us maintain the modal visiblity state so that the modal state is not changed when there are re renders.
3+
*/
4+
5+
// External Packages
6+
import { createSlice } from '@reduxjs/toolkit';
7+
8+
export interface IModalSliceState {
9+
isAllowNotifModalVisible: boolean;
10+
}
11+
12+
const initialState: IModalSliceState = {
13+
isAllowNotifModalVisible: false, //the current page
14+
};
15+
16+
export const modalSlice = createSlice({
17+
name: 'modal',
18+
initialState,
19+
reducers: {
20+
resetModalSlice: () => initialState,
21+
setAllowNotifModalVisibility: (state, action) => {
22+
state.isAllowNotifModalVisible = action.payload.flag;
23+
},
24+
},
25+
});
26+
27+
export const { resetModalSlice, setAllowNotifModalVisibility } = modalSlice.actions;
28+
29+
export default modalSlice.reducer;

src/redux/store.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@ import channelCreationReducer from './slices/channelCreationSlice';
77
import channelReducer from './slices/channelSlice';
88
import contractReducer from './slices/contractSlice';
99
import notificationReducer from './slices/notificationSlice';
10-
import canSendNotification from "./slices/sendNotificationSlice";
10+
import canSendNotification from './slices/sendNotificationSlice';
1111
import spamReducer from './slices/spamSlice';
1212
import userJourneyReducer from './slices/userJourneySlice';
13-
import userReducer from "./slices/userSlice";
13+
import userReducer from './slices/userSlice';
14+
import modalReducer from './slices/modalSlice';
1415

1516
const rootReducer = combineReducers({
16-
contracts: contractReducer,
17-
channels: channelReducer,
18-
channelCreation: channelCreationReducer,
19-
admin: adminReducer,
20-
notifications: notificationReducer,
21-
spam: spamReducer,
22-
userJourney: userJourneyReducer,
23-
canSend:canSendNotification,
24-
user: userReducer
17+
contracts: contractReducer,
18+
channels: channelReducer,
19+
channelCreation: channelCreationReducer,
20+
admin: adminReducer,
21+
notifications: notificationReducer,
22+
spam: spamReducer,
23+
userJourney: userJourneyReducer,
24+
canSend: canSendNotification,
25+
user: userReducer,
26+
modal: modalReducer,
2527
});
2628

2729
const store = configureStore({
28-
reducer: rootReducer,
29-
middleware: getDefaultMiddleware({
30-
serializableCheck: false,
31-
immutableCheck: false
32-
})
30+
reducer: rootReducer,
31+
middleware: getDefaultMiddleware({
32+
serializableCheck: false,
33+
immutableCheck: false,
34+
}),
3335
});
3436

3537
export default store;

0 commit comments

Comments
 (0)