1- import { createSlice } from '@reduxjs/toolkit' ;
1+ import { createAction , createSlice } from '@reduxjs/toolkit' ;
22import { login , logout } from './authSlice' ;
33import { jwtDecode } from 'jwt-decode' ;
44
5+ import {
6+ studentSideBarList ,
7+ staffSideBarList ,
8+ studentSideBarIconList ,
9+ staffSideBarIconList ,
10+ getStudentTabContent ,
11+ getStaffTabContent ,
12+ } from '../../utils/sideBarList' ;
13+
14+ import { getStudentPath , getStaffPath } from '../../utils/dashBoardList' ;
15+
16+ export const updateNav = createAction ( 'sideBarItem/updateNav' ) ;
17+ export const updateTab = createAction ( 'sideBarItem/updateTab' ) ;
18+
519const initialState = {
620 nav : '' ,
721 tab : 0 ,
22+ sidebarItemList : [ ] ,
23+ sidebarIconList : [ ] ,
24+ tabContentsList : [ ] ,
25+ role : null ,
26+ path : '/' ,
827} ;
928
1029const sideBarItemSlice = createSlice ( {
1130 name : 'sideBarItem' ,
1231 initialState,
1332 reducers : {
14- updateNav : ( state , action ) => {
15- state . nav = action . payload ;
16- state . tab = 0 ;
17- } ,
18- updateTab : ( state , action ) => {
19- state . tab = action . payload ;
20- } ,
2133 setRole : ( state , action ) => {
2234 state . nav = action . payload ;
35+ state . sidebarItemList = action . payload ;
36+ state . sidebarIconList = action . payload ;
37+ state . tabContentsList = action . payload ;
2338 } ,
2439 } ,
2540 extraReducers : ( builder ) => {
@@ -28,13 +43,50 @@ const sideBarItemSlice = createSlice({
2843 const accessToken = action . payload . accessToken . replace ( 'Bearer ' , '' ) ;
2944 const decodedToken = jwtDecode ( accessToken ) ;
3045 const role = decodedToken . roles [ 0 ] ;
46+
47+ state . role = role ;
3148 state . nav = role === 'STUDENT' ? '출석' : '출결' ;
49+ state . sidebarItemList = role === 'STUDENT' ? studentSideBarList : staffSideBarList ;
50+ state . sidebarIconList = role === 'STUDENT' ? studentSideBarIconList : staffSideBarIconList ;
51+ state . tabContentsList =
52+ role === 'STUDENT' ? getStudentTabContent ( state . nav ) : getStaffTabContent ( state . nav ) ;
53+ state . path =
54+ state . role === 'STUDENT'
55+ ? getStudentPath ( state . nav , state . tab )
56+ : getStaffPath ( state . nav , state . tab ) ;
3257 } )
3358 . addCase ( logout , ( state ) => {
59+ state . role = null ;
3460 state . nav = '' ;
61+ state . sidebarItemList = [ ] ;
62+ state . sidebarIconList = [ ] ;
63+ state . tabContentsList = [ ] ;
64+ } )
65+ . addCase ( updateNav , ( state , action ) => {
66+ if ( state . role ) {
67+ state . nav = action . payload ;
68+ state . tab = 0 ;
69+ state . tabContentsList =
70+ state . role === 'STUDENT'
71+ ? getStudentTabContent ( state . nav )
72+ : getStaffTabContent ( state . nav ) ;
73+ state . path =
74+ state . role === 'STUDENT'
75+ ? getStudentPath ( state . nav , state . tab )
76+ : getStaffPath ( state . nav , state . tab ) ;
77+ }
78+ } )
79+ . addCase ( updateTab , ( state , action ) => {
80+ if ( state . role ) {
81+ state . tab = action . payload ;
82+ state . path =
83+ state . role === 'STUDENT'
84+ ? getStudentPath ( state . nav , state . tab )
85+ : getStaffPath ( state . nav , state . tab ) ;
86+ }
3587 } ) ;
3688 } ,
3789} ) ;
3890
39- export const { updateNav , updateTab , setRole } = sideBarItemSlice . actions ;
91+ export const { setRole } = sideBarItemSlice . actions ;
4092export default sideBarItemSlice . reducer ;
0 commit comments