-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUserProvider.tsx
107 lines (96 loc) · 3.51 KB
/
UserProvider.tsx
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
import * as React from "react";
import { AuthContext } from "./AuthProvider";
import { Account } from "../../model/account";
import { RouteComponentProps, withRouter } from "react-router";
import { IApiPage } from "../../api/api";
import { ErrorContext } from "../errors/ErrorGuard";
import Permission from "../../model/permission";
import ContactAnAdmin from "../generic/ContactAnAdmin";
import useSWR from "swr";
export interface IAccountWithExtraContext extends Account {
permissions?: Permission[];
showAssays?: boolean;
showAnalyses?: boolean;
showManifests?: boolean;
canDownload?: boolean;
}
export const UserContext = React.createContext<
IAccountWithExtraContext | undefined
>(undefined);
export function useUserContext() {
const user = React.useContext(UserContext)!;
return user;
}
const UserProvider: React.FunctionComponent<RouteComponentProps> = props => {
const authData = React.useContext(AuthContext);
const setError = React.useCallback(React.useContext(ErrorContext), []);
const { data: user, error } = useSWR<Account>(
authData.state === "logged-in"
? ["/users/self", authData.userInfo.idToken]
: null
);
React.useEffect(() => {
if (user) {
if (user?.disabled) {
// user's account is registered and approved, but disabled
setError({
type: "Login Error",
message: "Account Disabled",
description: (
<>
Your CIDC account has been disabled due to
inactivity. <ContactAnAdmin /> to reactivate your
account.
</>
)
});
} else if (!user?.approval_date) {
// user is registered but not yet approved
props.history.replace("/");
}
} else if (
// user is authenticated but not yet registered
error?.response?.data?._error?.message?.includes("not registered")
) {
props.history.replace("/register");
} else if (error) {
setError({
type: "Request Error",
message: "error loading account information"
});
}
}, [user, error, setError, props.history]);
const { data: permissions } = useSWR<IApiPage<Permission>>(
authData.state === "logged-in" && user
? [`/permissions?user_id=${user.id}`, authData.userInfo.idToken]
: null
);
const showAssays =
user?.role &&
["cimac-biofx-user", "cidc-biofx-user", "cidc-admin"].includes(
user.role
);
const showManifests =
user?.role && ["nci-biobank-user", "cidc-admin"].includes(user.role);
const showAnalyses =
user?.role && ["cidc-biofx-user", "cidc-admin"].includes(user.role);
const canDownload = user?.role !== "network-viewer";
const value =
authData.state === "logged-in" && user && permissions
? {
...authData.userInfo.user,
...user,
permissions: permissions._items,
showAssays,
showManifests,
showAnalyses,
canDownload
}
: undefined;
return (
<UserContext.Provider value={value}>
{props.children}
</UserContext.Provider>
);
};
export default withRouter(UserProvider);