initial commit
This commit is contained in:
19
src/store/index.js
Normal file
19
src/store/index.js
Normal file
@@ -0,0 +1,19 @@
|
||||
// third-party
|
||||
import { configureStore } from '@reduxjs/toolkit';
|
||||
import { useDispatch as useAppDispatch, useSelector as useAppSelector } from 'react-redux';
|
||||
|
||||
// project import
|
||||
import reducers from './reducers';
|
||||
|
||||
// ==============================|| REDUX TOOLKIT - MAIN STORE ||============================== //
|
||||
|
||||
const store = configureStore({
|
||||
reducer: reducers
|
||||
});
|
||||
|
||||
const { dispatch } = store;
|
||||
|
||||
const useDispatch = () => useAppDispatch();
|
||||
const useSelector = useAppSelector;
|
||||
|
||||
export { store, dispatch, useSelector, useDispatch };
|
||||
4
src/store/reducers/actions.js
Normal file
4
src/store/reducers/actions.js
Normal file
@@ -0,0 +1,4 @@
|
||||
// action - account reducer
|
||||
export const LOGIN = '@auth/LOGIN';
|
||||
export const LOGOUT = '@auth/LOGOUT';
|
||||
export const REGISTER = '@auth/REGISTER';
|
||||
48
src/store/reducers/auth.js
Normal file
48
src/store/reducers/auth.js
Normal file
@@ -0,0 +1,48 @@
|
||||
// action - state management
|
||||
import { REGISTER, LOGIN, LOGOUT } from './actions';
|
||||
|
||||
// initial state
|
||||
export const initialState = {
|
||||
// isLoggedIn: false,
|
||||
// isInitialized: false,
|
||||
// user: null
|
||||
isLoggedIn: true,
|
||||
isInitialized: true,
|
||||
user: null
|
||||
};
|
||||
|
||||
// ==============================|| AUTH REDUCER ||============================== //
|
||||
|
||||
const auth = (state = initialState, action) => {
|
||||
switch (action.type) {
|
||||
case REGISTER: {
|
||||
const { user } = action.payload;
|
||||
return {
|
||||
...state,
|
||||
user
|
||||
};
|
||||
}
|
||||
case LOGIN: {
|
||||
const { user } = action.payload;
|
||||
return {
|
||||
...state,
|
||||
isLoggedIn: true,
|
||||
isInitialized: true,
|
||||
user
|
||||
};
|
||||
}
|
||||
case LOGOUT: {
|
||||
return {
|
||||
...state,
|
||||
isInitialized: true,
|
||||
isLoggedIn: false,
|
||||
user: null
|
||||
};
|
||||
}
|
||||
default: {
|
||||
return { ...state };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default auth;
|
||||
24
src/store/reducers/fcmSlice.js
Normal file
24
src/store/reducers/fcmSlice.js
Normal file
@@ -0,0 +1,24 @@
|
||||
// src/store/fcmSlice.js
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
const fcmSlice = createSlice({
|
||||
name: 'fcm',
|
||||
initialState: {
|
||||
permission: null,
|
||||
token: null
|
||||
},
|
||||
reducers: {
|
||||
setFcmToken(state, action) {
|
||||
state.token = action.payload;
|
||||
},
|
||||
clearFcmToken(state) {
|
||||
state.token = null;
|
||||
},
|
||||
setFcmPermission(state, action) {
|
||||
state.permission = action.payload;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const { setFcmToken, clearFcmToken, setFcmPermission } = fcmSlice.actions;
|
||||
export default fcmSlice.reducer;
|
||||
21
src/store/reducers/index.js
Normal file
21
src/store/reducers/index.js
Normal file
@@ -0,0 +1,21 @@
|
||||
// third-party
|
||||
import { combineReducers } from 'redux';
|
||||
|
||||
// project import
|
||||
import menu from './menu';
|
||||
import snackbar from './snackbar';
|
||||
import fcm from './fcmSlice';
|
||||
import toastSlice from './toastSlice';
|
||||
import loginUserSlice from './loginUserSlice';
|
||||
|
||||
// ==============================|| COMBINE REDUCERS ||============================== //
|
||||
|
||||
const reducers = combineReducers({
|
||||
menu,
|
||||
snackbar,
|
||||
fcm,
|
||||
toastSlice,
|
||||
loginUserSlice
|
||||
});
|
||||
|
||||
export default reducers;
|
||||
31
src/store/reducers/loginUserSlice.js
Normal file
31
src/store/reducers/loginUserSlice.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
const initialState = {
|
||||
isAuthenticated: false,
|
||||
user: null
|
||||
};
|
||||
|
||||
const loginUserSlice = createSlice({
|
||||
name: 'loginUser',
|
||||
initialState,
|
||||
reducers: {
|
||||
setLoginUser(state, action) {
|
||||
state.isAuthenticated = true;
|
||||
state.user = action.payload;
|
||||
},
|
||||
|
||||
updateUserFcmToken(state, action) {
|
||||
if (state.user) {
|
||||
state.user.userfcmtoken = action.payload;
|
||||
}
|
||||
},
|
||||
|
||||
logoutUser() {
|
||||
return initialState; // 🔥 clears entire slice
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const { setLoginUser, updateUserFcmToken, logoutUser } = loginUserSlice.actions;
|
||||
|
||||
export default loginUserSlice.reducer;
|
||||
67
src/store/reducers/menu.js
Normal file
67
src/store/reducers/menu.js
Normal file
@@ -0,0 +1,67 @@
|
||||
// third-party
|
||||
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
// project import
|
||||
import axios from 'utils/axios';
|
||||
|
||||
// initial state
|
||||
const initialState = {
|
||||
openItem: ['dashboard'],
|
||||
openComponent: 'buttons',
|
||||
selectedID: null,
|
||||
drawerOpen: false,
|
||||
componentDrawerOpen: true,
|
||||
menu: {},
|
||||
error: null,
|
||||
selectedMenu: null
|
||||
};
|
||||
|
||||
// ==============================|| SLICE - MENU ||============================== //
|
||||
|
||||
export const fetchMenu = createAsyncThunk('', async () => {
|
||||
const response = await axios.get('/api/menu/dashboard');
|
||||
return response.data;
|
||||
});
|
||||
|
||||
const menu = createSlice({
|
||||
name: 'menu',
|
||||
initialState,
|
||||
reducers: {
|
||||
activeItem(state, action) {
|
||||
state.openItem = action.payload.openItem;
|
||||
},
|
||||
|
||||
activeID(state, action) {
|
||||
state.selectedID = action.payload;
|
||||
},
|
||||
|
||||
activeComponent(state, action) {
|
||||
state.openComponent = action.payload.openComponent;
|
||||
},
|
||||
|
||||
openDrawer(state, action) {
|
||||
state.drawerOpen = action.payload;
|
||||
},
|
||||
|
||||
openComponentDrawer(state, action) {
|
||||
state.componentDrawerOpen = action.payload.componentDrawerOpen;
|
||||
},
|
||||
|
||||
hasError(state, action) {
|
||||
state.error = action.payload;
|
||||
},
|
||||
setSelectedMenu(state, action) {
|
||||
state.selectedMenu = action.payload;
|
||||
}
|
||||
},
|
||||
|
||||
extraReducers(builder) {
|
||||
builder.addCase(fetchMenu.fulfilled, (state, action) => {
|
||||
state.menu = action.payload.dashboard;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default menu.reducer;
|
||||
|
||||
export const { activeItem, activeComponent, openDrawer, openComponentDrawer, activeID, setSelectedMenu } = menu.actions;
|
||||
67
src/store/reducers/snackbar.js
Normal file
67
src/store/reducers/snackbar.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
const initialState = {
|
||||
action: false,
|
||||
open: false,
|
||||
message: 'Note archived',
|
||||
anchorOrigin: {
|
||||
vertical: 'bottom',
|
||||
horizontal: 'right'
|
||||
},
|
||||
variant: 'default',
|
||||
alert: {
|
||||
color: 'primary',
|
||||
variant: 'filled'
|
||||
},
|
||||
transition: 'Fade',
|
||||
close: true,
|
||||
actionButton: false,
|
||||
maxStack: 3,
|
||||
dense: false,
|
||||
iconVariant: 'usedefault'
|
||||
};
|
||||
|
||||
// ==============================|| SLICE - SNACKBAR ||============================== //
|
||||
|
||||
const snackbar = createSlice({
|
||||
name: 'snackbar',
|
||||
initialState,
|
||||
reducers: {
|
||||
openSnackbar(state, action) {
|
||||
const { open, message, anchorOrigin, variant, alert, transition, close, actionButton } = action.payload;
|
||||
|
||||
state.action = !state.action;
|
||||
state.open = open || initialState.open;
|
||||
state.message = message || initialState.message;
|
||||
state.anchorOrigin = anchorOrigin || initialState.anchorOrigin;
|
||||
state.variant = variant || initialState.variant;
|
||||
state.alert = {
|
||||
color: alert?.color || initialState.alert.color,
|
||||
variant: alert?.variant || initialState.alert.variant
|
||||
};
|
||||
state.transition = transition || initialState.transition;
|
||||
state.close = close === false ? close : initialState.close;
|
||||
state.actionButton = actionButton || initialState.actionButton;
|
||||
},
|
||||
|
||||
closeSnackbar(state) {
|
||||
state.open = false;
|
||||
},
|
||||
handlerIncrease(state, action) {
|
||||
const { maxStack } = action.payload;
|
||||
state.maxStack = maxStack;
|
||||
},
|
||||
handlerDense(state, action) {
|
||||
const { dense } = action.payload;
|
||||
state.dense = dense;
|
||||
},
|
||||
handlerIconVariants(state, action) {
|
||||
const { iconVariant } = action.payload;
|
||||
state.iconVariant = iconVariant;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export default snackbar.reducer;
|
||||
|
||||
export const { closeSnackbar, openSnackbar, handlerIncrease, handlerDense, handlerIconVariants } = snackbar.actions;
|
||||
19
src/store/reducers/toastSlice.js
Normal file
19
src/store/reducers/toastSlice.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
const toastSlice = createSlice({
|
||||
name: 'toast',
|
||||
initialState: {
|
||||
snackbarId: null
|
||||
},
|
||||
reducers: {
|
||||
setSnackbarId: (state, action) => {
|
||||
state.snackbarId = action.payload;
|
||||
},
|
||||
clearSnackbarId: (state) => {
|
||||
state.snackbarId = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const { setSnackbarId, clearSnackbarId } = toastSlice.actions;
|
||||
export default toastSlice.reducer;
|
||||
Reference in New Issue
Block a user