forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsersIndexPage.tsx
217 lines (200 loc) · 7.86 KB
/
UsersIndexPage.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import React from "react"
import { observer } from "mobx-react"
import { observable, action, runInAction } from "mobx"
import { Modal, Timeago } from "./Forms.js"
import { Link } from "./Link.js"
import { AdminLayout } from "./AdminLayout.js"
import { AdminAppContext, AdminAppContextType } from "./AdminAppContext.js"
import { UserIndexMeta } from "./UserMeta.js"
interface UserIndexMetaWithLastSeen extends UserIndexMeta {
lastSeen: Date
}
@observer
class InviteModal extends React.Component<{ onClose: () => void }> {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable email: string = ""
@observable fullName: string = ""
@observable responseSuccess: boolean = false
async submit() {
runInAction(() => (this.responseSuccess = false))
if (this.email) {
const resp = await this.context.admin.requestJSON(
"/api/users/add",
{ email: this.email, fullName: this.fullName },
"POST"
)
console.log(resp)
if (resp.success) {
runInAction(() => (this.responseSuccess = true))
}
}
}
@action.bound onSubmit(event: React.FormEvent) {
event.preventDefault()
void this.submit()
}
render() {
return (
<Modal onClose={this.props.onClose}>
<form onSubmit={this.onSubmit}>
<div className="modal-header">
<h5 className="modal-title">Add a user</h5>
</div>
<div className="modal-body">
<div className="form-group">
<label>Full name</label>
<input
type="string"
className="form-control"
onChange={(e) =>
(this.fullName = e.currentTarget.value)
}
required
/>
</div>
<div className="form-group">
<label>Email</label>
<input
type="email"
className="form-control"
onChange={(e) =>
(this.email = e.currentTarget.value)
}
required
/>
</div>
</div>
<div className="modal-footer">
<input
type="submit"
className="btn btn-primary"
value="Add user"
/>
</div>
{this.responseSuccess && (
<div className="alert alert-success" role="alert">
User added! They can now log in with their G Suite
account.
</div>
)}
</form>
</Modal>
)
}
}
@observer
export class UsersIndexPage extends React.Component {
static contextType = AdminAppContext
context!: AdminAppContextType
@observable users: UserIndexMetaWithLastSeen[] = []
@observable isInviteModal: boolean = false
@action.bound async onDelete(user: UserIndexMetaWithLastSeen) {
if (
!window.confirm(
`Delete the user ${user.fullName}? This action cannot be undone!`
)
)
return
const json = await this.context.admin.requestJSON(
`/api/users/${user.id}`,
{},
"DELETE"
)
if (json.success) {
runInAction(() => this.users.splice(this.users.indexOf(user), 1))
}
}
render() {
const { users } = this
const { isSuperuser } = this.context.admin
return (
<AdminLayout title="Users">
<main className="UsersIndexPage">
{this.isInviteModal && (
<InviteModal
onClose={action(() => (this.isInviteModal = false))}
/>
)}
<div className="topbar">
<h2>Users</h2>
{isSuperuser && (
<button
onClick={action(
() => (this.isInviteModal = true)
)}
className="btn btn-primary"
>
Add a user
</button>
)}
</div>
<table className="table table-bordered">
<tbody>
<tr>
<th>Name</th>
<th>Last Seen</th>
<th>Joined</th>
{isSuperuser && <th>Status</th>}
{isSuperuser && <th></th>}
{isSuperuser && <th></th>}
</tr>
{users.map((user) => (
<tr key={user.id}>
<td>{user.fullName}</td>
<td>
<Timeago time={user.lastSeen} />
</td>
<td>
<Timeago time={user.createdAt} />
</td>
{isSuperuser && (
<td>
{user.isActive
? "active"
: "disabled"}
</td>
)}
{isSuperuser && (
<td>
<Link
to={`/users/${user.id}`}
className="btn btn-primary"
>
Edit
</Link>
</td>
)}
{isSuperuser && (
<td>
<button
className="btn btn-danger"
onClick={() =>
this.onDelete(user)
}
>
Delete
</button>
</td>
)}
</tr>
))}
</tbody>
</table>
</main>
</AdminLayout>
)
}
async getData() {
const { admin } = this.context
const json = (await admin.getJSON("/api/users.json")) as {
users: UserIndexMetaWithLastSeen[]
}
runInAction(() => {
this.users = json.users
})
}
componentDidMount() {
void this.getData()
}
}