forked from Jacenty00/tagify-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_admin.tsx
64 lines (59 loc) · 1.96 KB
/
index_admin.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
import React, { useEffect, useState } from 'react';
import { Admin, ListGuesser, Resource } from 'react-admin';
import ReactDOM from 'react-dom';
import AlbumIcon from '@material-ui/icons/Book';
import UserIcon from '@material-ui/icons/Group';
import AdminDataProvider from './components/admin/AdminDataProvider';
import { AlbumsEdit, AlbumsList } from './components/admin/Albums';
import { MyAuthProvider, MyLogoutButton } from './components/admin/MyLogoutButton';
import { UserCreate, UserEdit, UserList } from './components/admin/Users';
import BackendToken, { Default, Status, UserRole } from './utils/BackendAPI';
export function App() {
const [render, setRender] = useState<boolean>(false);
useEffect(() => {
(async () => {
let response = await Default.getUser();
let code = response.status;
let role: UserRole | undefined = response.data?.role;
BackendToken.authenticated = code == Status.Ok;
BackendToken.userRole = role;
BackendToken.nickname =
response.data == undefined ? "" : response.data.nickname;
if (BackendToken.userRole != UserRole.Admin) {
window.location.replace("/welcome");
}
setRender(true);
})();
}, []);
if (BackendToken.isAuthenticated && BackendToken.userRole == UserRole.Admin) {
return (
<>
<Admin
dataProvider={AdminDataProvider}
loginPage={false}
logoutButton={MyLogoutButton}
authProvider={MyAuthProvider}
>
<Resource
name="users"
list={UserList}
edit={UserEdit}
create={UserCreate}
icon={UserIcon}
exporter={false}
/>
<Resource
name="albums"
list={AlbumsList}
edit={AlbumsEdit}
icon={AlbumIcon}
exporter={false}
/>
</Admin>
</>
);
} else {
return null;
}
}
ReactDOM.render(<App />, document.getElementById("app"));