Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 8370b6c1

Přidáno uživatelem Václav Honzík před téměř 3 roky(ů)

login simple form impl + slice for user state

Zobrazit rozdíly:

frontend/src/features/Auth/userSlice.ts
1 1
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
2 2
import { persistReducer } from 'redux-persist'
3 3
import storage from 'redux-persist/lib/storage'
4
import { logIn } from './userThunks'
4 5

  
5 6
export interface UserState {
6 7
    accessToken?: string
......
8 9
    username: string
9 10
    roles: string[]
10 11
    isLoggedIn: boolean
12
    lastErr?: string // consumable for errors during thunks
11 13
}
12 14

  
13 15
const persistConfig = {
......
29 31

  
30 32
    // Reducers that update the state
31 33
    reducers: {
32
        logout: () => {
33
            return initialState // Reset to the inital state
34
        },
35
        refreshTokens: (state, action) => {
36
            return {
37
                ...state,
38
                accessToken: action.payload.accessToken,
39
                refreshToken: action.payload.refreshToken,
40
            }
41
        },
34
        logout: () => initialState, // Reset to the inital state
35
        refreshTokens: (state, action) => ({
36
            ...state,
37
            accessToken: action.payload.accessToken,
38
            refreshToken: action.payload.refreshToken,
39
        }),
40
        setErr: (state, action) => ({
41
            ...state,
42
            lastErr: action.payload,
43
        }),
42 44
    },
43 45

  
44
    // For thunks (async operations)
45
    extraReducers: {},
46
    // Thunks
47
    extraReducers: (builder) => {
48
        builder.addCase(logIn.fulfilled, () => {
49
            console.log('Action performed') // TODO remove
50
        }) // TODO funny
51
    },
46 52
})
47 53

  
48 54
const userReducer = persistReducer(persistConfig, userSlice.reducer)
55

  
49 56
export default userReducer

Také k dispozici: Unified diff