This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
user-slice.ts
121 lines (111 loc) · 3.14 KB
/
user-slice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* eslint-disable no-underscore-dangle */
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { remoteDb } from '../shared/config/pouchdb'
import Permissions from '../shared/model/Permissions'
import User from '../shared/model/User'
import { AppThunk } from '../shared/store'
interface LoginError {
message?: string
username?: string
password?: string
}
interface UserState {
permissions: (Permissions | null)[]
user?: User
loginError?: LoginError
}
const initialState: UserState = {
permissions: [
Permissions.ReadPatients,
Permissions.WritePatients,
Permissions.ReadAppointments,
Permissions.WriteAppointments,
Permissions.DeleteAppointment,
Permissions.AddAllergy,
Permissions.AddDiagnosis,
Permissions.ViewLabs,
Permissions.ViewLab,
Permissions.RequestLab,
Permissions.CompleteLab,
Permissions.CancelLab,
Permissions.ViewIncident,
Permissions.ViewIncidents,
Permissions.ReportIncident,
Permissions.AddCarePlan,
Permissions.ReadCarePlan,
],
}
const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
fetchPermissions(state, { payload }: PayloadAction<Permissions[]>) {
state.permissions = payload
},
loginSuccess(
state,
{ payload }: PayloadAction<{ user: User; permissions: (Permissions | null)[] }>,
) {
state.user = payload.user
state.permissions = initialState.permissions
},
loginError(state, { payload }: PayloadAction<LoginError>) {
state.loginError = payload
},
logoutSuccess(state) {
state.user = undefined
state.permissions = []
},
},
})
export const { fetchPermissions, loginError, loginSuccess, logoutSuccess } = userSlice.actions
export const getCurrentSession = (username: string): AppThunk => async (dispatch) => {
const user = await remoteDb.getUser(username)
dispatch(
loginSuccess({
user: {
id: user._id,
givenName: (user as any).metadata.givenName,
familyName: (user as any).metadata.familyName,
},
permissions: initialState.permissions,
}),
)
}
export const login = (username: string, password: string): AppThunk => async (dispatch) => {
try {
const response = await remoteDb.logIn(username, password)
const user = await remoteDb.getUser(response.name)
dispatch(
loginSuccess({
user: {
id: user._id,
givenName: (user as any).metadata.givenName,
familyName: (user as any).metadata.familyName,
},
permissions: initialState.permissions,
}),
)
} catch (error) {
if (!username || !password) {
dispatch(
loginError({
message: 'user.login.error.message.required',
username: 'user.login.error.username.required',
password: 'user.login.error.password.required',
}),
)
} else if (error.status === 401) {
dispatch(
loginError({
message: 'user.login.error.message.incorrect',
}),
)
}
}
}
export const logout = (): AppThunk => async (dispatch) => {
await remoteDb.logOut()
dispatch(logoutSuccess())
}
export default userSlice.reducer