Projekt

Obecné

Profil

Stáhnout (2.32 KB) Statistiky
| Větev: | Tag: | Revize:
1 39d74503 Vaclav Honzik
import { createSlice } from '@reduxjs/toolkit'
2 37f6ff02 Vaclav Honzik
import { persistReducer } from 'redux-persist'
3
import storage from 'redux-persist/lib/storage'
4 2d3a4e2b Schwobik
import {logIn, register} from './userThunks'
5 37f6ff02 Vaclav Honzik
6
export interface UserState {
7
    accessToken?: string
8
    refreshToken?: string
9
    username: string
10
    roles: string[]
11 6129910f Vaclav Honzik
    isLoggingIn: boolean
12 37f6ff02 Vaclav Honzik
    isLoggedIn: boolean
13 2d3a4e2b Schwobik
    lastErr?: string// consumable for errors during thunks
14
    isRegistered: boolean
15 37f6ff02 Vaclav Honzik
}
16
17
const persistConfig = {
18
    key: 'auth',
19
    storage, // localStorage for browsers
20
}
21
22
// Default state when user first starts the application
23
const initialState: UserState = {
24
    roles: [],
25
    isLoggedIn: false,
26 6129910f Vaclav Honzik
    isLoggingIn: false,
27 37f6ff02 Vaclav Honzik
    username: '',
28 2d3a4e2b Schwobik
    isRegistered: false,
29 37f6ff02 Vaclav Honzik
}
30
31
export const userSlice = createSlice({
32
    name: 'user', // name to generate action types
33
    initialState, // default state
34
    // Reducers that update the state
35
    reducers: {
36 8370b6c1 Vaclav Honzik
        logout: () => initialState, // Reset to the inital state
37
        refreshTokens: (state, action) => ({
38
            ...state,
39
            accessToken: action.payload.accessToken,
40
            refreshToken: action.payload.refreshToken,
41
        }),
42
        setErr: (state, action) => ({
43
            ...state,
44
            lastErr: action.payload,
45
        }),
46 6129910f Vaclav Honzik
        setUserState: (state, action) => ({ ...state, ...action.payload }),
47
        resetLoggingIn: (state) => ({ ...state, isLoggingIn: false }),
48 2d3a4e2b Schwobik
        resetIsRegistered: (state) => ({ ...state, isRegistered: false }),
49 37f6ff02 Vaclav Honzik
    },
50
51 8370b6c1 Vaclav Honzik
    // Thunks
52
    extraReducers: (builder) => {
53 fc79a8cb Vaclav Honzik
        builder.addCase(logIn.fulfilled, (state, action) => {
54 6129910f Vaclav Honzik
            return { ...state, ...action.payload }
55 fc79a8cb Vaclav Honzik
        })
56
        builder.addCase(logIn.rejected, (state, action) => {
57 0d90d67b Vaclav Honzik
            if (action.payload && typeof action.error.message === 'string') {
58 6129910f Vaclav Honzik
                return { ...state, lastErr: action.error.message }
59 fc79a8cb Vaclav Honzik
            }
60
        })
61 8c57f958 Vaclav Honzik
        builder.addCase(logIn.pending, (state) => {
62 6129910f Vaclav Honzik
            return { ...state, isLoggingIn: true }
63
        })
64 2d3a4e2b Schwobik
        builder.addCase(register.fulfilled, (state, action) => {
65
            return {...state, isRegistered: true}
66
        })
67 8370b6c1 Vaclav Honzik
    },
68 37f6ff02 Vaclav Honzik
})
69
70
const userReducer = persistReducer(persistConfig, userSlice.reducer)
71 8370b6c1 Vaclav Honzik
72 2d3a4e2b Schwobik
export const { logout, refreshTokens, setErr, setUserState, resetLoggingIn, resetIsRegistered } =
73 8c57f958 Vaclav Honzik
    userSlice.actions
74 fc79a8cb Vaclav Honzik
75 37f6ff02 Vaclav Honzik
export default userReducer