notification store and update in local storage
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||
|
||||
// Define the shape of a notification object
|
||||
export interface Notification {
|
||||
id: string;
|
||||
title: string;
|
||||
message: string;
|
||||
time: string;
|
||||
read: boolean;
|
||||
}
|
||||
|
||||
// Define the slice state type
|
||||
interface NotificationState {
|
||||
notifications: Notification[];
|
||||
}
|
||||
|
||||
// Initial state
|
||||
const initialState: NotificationState = {
|
||||
notifications: [],
|
||||
};
|
||||
|
||||
const notificationSlice = createSlice({
|
||||
name: 'notifications',
|
||||
initialState,
|
||||
reducers: {
|
||||
addNotification: (state, action: PayloadAction<Notification>) => {
|
||||
// Adds the new notification to the top
|
||||
state.notifications.unshift(action.payload);
|
||||
},
|
||||
removeNotification: (state, action: PayloadAction<string>) => {
|
||||
// Remove notification by id
|
||||
state.notifications = state.notifications.filter(
|
||||
(item) => item.id !== action.payload
|
||||
);
|
||||
},
|
||||
clearAllNotifications: (state) => {
|
||||
state.notifications = [];
|
||||
},
|
||||
markAsRead: (state, action: PayloadAction<string>) => {
|
||||
// Mark one notification as read
|
||||
const notification = state.notifications.find(
|
||||
(item) => item.id === action.payload
|
||||
);
|
||||
if (notification) {
|
||||
notification.read = true;
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const {
|
||||
addNotification,
|
||||
removeNotification,
|
||||
clearAllNotifications,
|
||||
markAsRead,
|
||||
} = notificationSlice.actions;
|
||||
|
||||
export default notificationSlice.reducer;
|
||||
+47
-10
@@ -1,13 +1,50 @@
|
||||
// store.ts
|
||||
import { configureStore } from "@reduxjs/toolkit";
|
||||
import userReducer from "./userSlice"; // adjust the path
|
||||
// // store.ts
|
||||
// import { configureStore } from "@reduxjs/toolkit";
|
||||
// import userReducer from "./userSlice"; // adjust the path
|
||||
|
||||
export const store = configureStore({
|
||||
reducer: {
|
||||
user: userReducer, // register user slice here
|
||||
},
|
||||
// export const store = configureStore({
|
||||
// reducer: {
|
||||
// user: userReducer, // register user slice here
|
||||
// },
|
||||
// });
|
||||
|
||||
// // ✅ Types for TypeScript
|
||||
// export type RootState = ReturnType<typeof store.getState>;
|
||||
// export type AppDispatch = typeof store.dispatch;
|
||||
|
||||
|
||||
|
||||
import { configureStore, combineReducers } from '@reduxjs/toolkit';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { persistReducer, persistStore } from 'redux-persist';
|
||||
|
||||
// import slices
|
||||
import userReducer from './userSlice';
|
||||
import notificationReducer from './notificationSlice';
|
||||
|
||||
// combine reducers
|
||||
const rootReducer = combineReducers({
|
||||
user: userReducer,
|
||||
notifications: notificationReducer,
|
||||
});
|
||||
|
||||
// ✅ Types for TypeScript
|
||||
export type RootState = ReturnType<typeof store.getState>;
|
||||
export type AppDispatch = typeof store.dispatch;
|
||||
// persist configuration
|
||||
const persistConfig = {
|
||||
key: 'root',
|
||||
storage: AsyncStorage,
|
||||
whitelist: ['user', 'notifications'], // persist both slices
|
||||
};
|
||||
|
||||
// wrap root reducer with persistReducer
|
||||
const persistedReducer = persistReducer(persistConfig, rootReducer);
|
||||
|
||||
// configure store
|
||||
export const store = configureStore({
|
||||
reducer: persistedReducer,
|
||||
middleware: (getDefaultMiddleware) =>
|
||||
getDefaultMiddleware({
|
||||
serializableCheck: false,
|
||||
}),
|
||||
});
|
||||
|
||||
export const persistor = persistStore(store);
|
||||
|
||||
Reference in New Issue
Block a user