-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUserContext.js
More file actions
109 lines (99 loc) · 3.4 KB
/
UserContext.js
File metadata and controls
109 lines (99 loc) · 3.4 KB
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
import React, {createContext, useReducer} from 'react';
const UserContext = createContext();
const initialState = {
authenticated: false,
isAdmin: false,
user: {},
token: "",
registeredEvents: [],
groups: []
}
const actions = {
SET_USER: 'SET_USER',
SET_ADMIN: 'SET_ADMIN',
SET_GROUPS: 'SET_GROUPS',
SET_TOKEN: 'SET_TOKEN',
SET_USER_ROLES: 'SET_USER_ROLES',
REMOVE_USER_ROLES: 'REMOVE_USER_ROLES',
REMOVE_GROUP: 'REMOVE_GROUP',
SET_REGISTERED_EVENTS: 'SET_REGISTERED_EVENTS',
REMOVE_REGISTERED_EVENT: 'REMOVE_REGISTERED_EVENT',
}
function reducer(state, action) {
switch(action.type) {
case actions.SET_USER:
return {...state, user: action.value};
case actions.SET_EVENTS:
return { ...state, userEvents: action.value };
case actions.SET_REGISTERED_EVENTS:
return { ...state, registeredEvents: action.value };
case actions.SET_TOKEN:
return { ...state, token: action.value};
case actions.SET_ADMIN:
return { ...state, isAdmin: action.value};
case actions.SET_USER_ROLES:
return { ...state, roles: action.value};
case actions.REMOVE_USER_ROLES:
let filteredRoles = state.registeredEvents.filter(role => role.organization_id != action.value);
return { ...state, roles: filteredRoles}
case actions.REMOVE_REGISTERED_EVENT:
let filteredEvent = state.registeredEvents.filter(event => event.event_id != action.value);
return {...state, registeredEvents: filteredEvent};
case actions.SET_GROUPS:
return { ...state, clubs: action.value};
case actions.REMOVE_GROUP:
let filteredGroup = state.registeredEvents.filter(group => group.organization_id != action.value);
return {...state, clubs: filteredGroup};
default:
return state;
}
}
function UserProvider({children}) {
const [state, dispatch] = useReducer(reducer, initialState);
const data = {
authenticated: state.authenticated,
name: state.user.name,
roles: state.user.roles,
token: state.token,
isAdmin: state.isAdmin,
registeredEvents: state.registeredEvents,
groups: state.groups,
setToken: value => {
dispatch({ type: actions.SET_TOKEN, value});
},
setUser: value => {
dispatch({ type: actions.SET_USER, value});
},
setRegisteredEvents: value => {
dispatch({ type: actions.SET_REGISTERED_EVENTS, value});
},
setGroups: value => {
dispatch({ type: actions.SET_GROUPS, value});
},
setIsAdmin: value => {
dispatch({ type: actions.SET_ADMIN, value});
},
removeRegisteredEvent: value => {
dispatch({ type: actions.REMOVE_REGISTERED_EVENT, value });
},
removeGroup: value => {
dispatch({ type: actions.REMOVE_GROUP, value });
},
setRoles: value => {
console.log("ROLE: ", value);
dispatch({ type: actions.SET_USER_ROLES, value});
},
removeRole: value => {
dispatch({ type: actions.REMOVE_USER_ROLES, value});
}
}
return (
<UserContext.Provider value={data}>
{children}
</UserContext.Provider>
)
}
export {
UserContext,
UserProvider
}