forked from traPtitech/traQ-group-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
394 lines (376 loc) · 15.3 KB
/
App.vue
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<template>
<div class="container">
<div v-if="!me" class="content">
<div uk-grid>
<h1 class="uk-margin-top uk-width-1-1">ex-traQ グループ編集ツール</h1>
<div class="uk-text-lead">
ログインしていません.
</div>
<div class="uk-width-1-5">
<button class="uk-button uk-button-primary" @click="login">ログイン</button>
</div>
</div>
</div>
<div class="content uk-margin-top" v-else>
<div uk-grid>
<h1 class="uk-margin-top uk-width-1-1">ex-traQ グループ編集ツール</h1>
<div class="uk-width-1-1">
<div v-if="groups.length > 0">
<div uk-grid>
<h3 class="uk-width-expand" style="line-height: 40px;">グループ一覧</h3>
<button class="uk-button uk-button-default uk-margin-left" @click="getGroups">グループ更新</button>
</div>
<div>
<ul class="uk-list uk-list-divider group">
<li :key="group.id" v-for="group in groups" @click="selectGroup(group)">
<span class="uk-text-lead">
{{group.name}}
</span>
<span class="uk-text-meta">
{{group.description}}
</span>
<div class="uk-leader"></div>
<div class="uk-position-relative">
<span>メンバー {{group.members.length}}人</span>
<span>adminId: {{group.admins}}</span>
<button @click="deleteGroup(group)"
class="uk-button uk-button-danger uk-button-small right-button">削除
</button>
</div>
</li>
</ul>
</div>
</div>
<div>
<div uk-grid>
<div class="uk-width-1-5">
<input class="uk-input" type="text" placeholder="Group Name" v-model="newGroupName">
</div>
<div class="uk-width-3-5">
<input class="uk-input" type="text" placeholder="Group Description"
v-model="newGroupDescription">
</div>
<div class="uk-width-1-5">
<button class="uk-button uk-button-primary" @click="newGroup">作成</button>
</div>
</div>
</div>
</div>
<div uk-grid>
<div class="uk-width-1-3">
<div>
<div v-if="curGroup">
<h3>{{curGroup.name}}のメンバー一覧</h3>
<div class="uk-text-meta">クリックでメンバーを削除</div>
<div>
<user-list :users="curGroupMembers" height="500" @userClick="removeUser"/>
</div>
</div>
<div v-else>
<h3>グループが選択されていません</h3>
</div>
</div>
</div>
<div class="uk-width-1-3">
<div>
<h3>追加したいユーザーのID(スペース区切り)</h3>
<input class="uk-input" placeholder="@foo @bar..." type="text" v-model="addUserIds">
</div>
<div class="uk-margin">
<h3>メンバーに追加されるユーザー</h3>
<button @click="addUser" class="uk-button uk-button-primary">追加</button>
<user-list :users="willAddUser" height="500"/>
</div>
</div>
<div class="uk-width-1-3">
<div v-if="newGroupInfo">
<h3>グループ情報の変更</h3>
<input class="uk-input" placeholder="Group Name" type="text" v-model="newGroupInfo.name">
<input class="uk-input" placeholder="Group Description" type="text" v-model="newGroupInfo.description">
</div>
<div class="uk-margin">
<div v-if="curGroup">
<h3>変更対象グループ: {{curGroup.name}}</h3>
</div>
<div v-else>
<h3>変更対象グループ: グループが選択されていません</h3>
</div>
<button @click="editGroup" class="uk-button uk-button-primary">変更</button>
</div>
<div v-if="newGroupAdmin">
<h3>adminの追加</h3>
<input class="uk-input" placeholder="Group Admin" type="text" v-model="newGroupAdmin.id"
pattern="[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}">
</div>
<div class="uk-margin">
<button @click="addAdmin" class="uk-button uk-button-primary">追加</button>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import {fetchAuthToken, redirectAuthorizationEndpoint} from './oauth'
import {
Apis,
MyUserDetail,
PatchUserGroupRequest,
User,
UserGroup,
UserAccountState,
PostUserGroupAdminRequest
} from '@traptitech/traq'
import {AxiosResponse} from "axios"
import UserList from './UserList.vue'
export default Vue.extend({
data(): {
api: Apis | null
me: MyUserDetail | null
users: User[]
groups: UserGroup[]
curGroup: UserGroup | null
newGroupName: string
addUserIds: string
newGroupDescription: string
newGroupInfo: PatchUserGroupRequest | null
newGroupAdmin: PostUserGroupAdminRequest | null
} {
return {
api: null,
me: null,
users: [],
groups: [],
curGroup: null,
newGroupName: '',
addUserIds: '',
newGroupDescription: '',
newGroupInfo: null,
newGroupAdmin: null
}
},
components: {
UserList
},
methods: {
login() {
redirectAuthorizationEndpoint()
console.log('po')
},
async getGroups() {
await this.api?.getUserGroups().then((res: AxiosResponse<UserGroup[]>) => {
console.log(res)
this.groups = res.data
})
},
selectGroup(group: UserGroup) {
this.curGroup = group
this.newGroupInfo = {
name: group.name,
description: group.description,
type: group.type
}
this.newGroupAdmin = {
id: ''
}
},
async newGroup() {
if (this.newGroupName.trim() === '') {
return
}
await this.api?.createUserGroup({
name: this.newGroupName.trim(),
description: this.newGroupDescription,
type: ''
})
.then(_ => {
this.newGroupName = ''
this.newGroupDescription = ''
return this.getGroups()
})
.catch(e => {
console.log(e)
alert('作成に失敗しました\n' + e.toString())
})
},
async editGroup() {
if (!this.curGroup) {
alert('編集するグループを選択してください')
return
}
if (this.newGroupInfo !== null) {
if (confirm(`変更内容を確認してください。\n グループ名: ${this.newGroupInfo.name ? this.newGroupInfo.name : this.curGroup.name + "(未変更)"}\n グループの説明: ${this.newGroupInfo.description ? this.newGroupInfo.description : this.curGroup.description + "(未変更)"}`)) {
await this.api?.editUserGroup(this.curGroup!.id, this.newGroupInfo)
.then(_ => {
console.log('updated')
this.newGroupInfo = null
return this.getGroups()
})
.then(_ => {
this.curGroup = this.groups.find(g => g.id === this.curGroup!.id) ?? null
})
.catch(e => {
console.log(e)
alert('更新に失敗しました\n' + e.toString())
})
}
}
},
async addAdmin() {
if (!this.curGroup) {
alert('編集するグループを選択してください')
return
}
if (this.newGroupAdmin !== null) {
if (confirm(`追加するユーザーを確認してください。\n 新規admin: ${this.newGroupAdmin.id}`)) {
await this.api?.addUserGroupAdmin(this.curGroup!.id, this.newGroupAdmin)
.then(_ => {
console.log('added')
this.newGroupAdmin = null
return this.getGroups()
})
.then(_ => {
this.curGroup = this.groups.find(g => g.id === this.curGroup!.id) ?? null
})
.catch(e => {
console.log(e)
alert('追加に失敗しました\n' + e.toString())
})
}
} else {
alert('追加するユーザーを入力してください')
return
}
},
async deleteGroup(group: UserGroup) {
if (confirm(`${group.name}をグループ一覧から削除しますか?`)) {
await this.api?.deleteUserGroup(group.id)
.then(_ => {
console.log('deleted')
return this.getGroups()
})
.then(_ => {
this.curGroup = this.groups.find(g => g.id === this.curGroup?.id) ?? null
})
.catch(e => {
console.log(e)
alert('削除に失敗しました\n' + e.toString())
})
}
},
async addUser() {
if (!this.curGroup) return
await Promise.all(this.willAddUser.map(user => {
return this.api?.addUserGroupMember(this.curGroup!.id, {id: user.id, role: ''})
}))
.then(_ => {
console.log('successfully added')
return this.getGroups()
})
.then(_ => {
this.curGroup = this.groups.find(g => g.id === this.curGroup!.id) ?? null
})
.catch(e => {
console.log(e)
alert('追加に失敗しました\n' + e.toString())
})
},
async removeUser(user: User) {
if (!this.curGroup) return
if (confirm(`${user.name}を${this.curGroup.name}から削除しますか?`)) {
await this.api?.removeUserGroupMember(this.curGroup.id, user.id)
.then(_ => {
console.log('deleted')
return this.getGroups()
})
.then(_ => {
this.curGroup = this.groups.find(g => g.id === this.curGroup!.id) ?? null
})
.catch(e => {
console.log(e)
alert('削除に失敗しました\n' + e.toString())
})
}
}
},
async mounted() {
const queryParams = new URLSearchParams(location.search)
const code = queryParams.get('code')
const state = queryParams.get('state')
if (code && state) {
const verifier = sessionStorage.getItem(`login-code-verifier-${state}`)
await fetchAuthToken(code, verifier)
.then(async (res: { data: { access_token: string } }) => {
console.log(res)
this.api = new Apis({
basePath: "https://ex-traq.emoine.tech/api/v3",
accessToken: res.data.access_token
})
await this.api.getMe().then((res: AxiosResponse<MyUserDetail>) => {
this.me = res.data
})
console.log(this.me)
sessionStorage.setItem('access_token', res.data.access_token)
location.href = '/'
})
} else {
const accessToken = sessionStorage.getItem('access_token')!
this.api = new Apis({
basePath: "https://ex-traq.emoine.tech/api/v3",
accessToken: accessToken
})
await this.api.getMe().then((res: AxiosResponse<MyUserDetail>) => {
this.me = res.data
})
}
if (!this.me) {
await redirectAuthorizationEndpoint()
}
await this.api?.getUsers().then((res: AxiosResponse<User[]>) => {
this.users = res.data
})
await this.getGroups()
},
computed: {
curGroupMembers(): User[] {
if (!this.curGroup) {
return []
}
return this.curGroup.members.map(member => {
return this.users.find(user => user.id === member.id)
}).filter(<T>(user: T): user is Exclude<T, undefined> => !!user)
},
realUsers(): User[] {
return this.users.filter((user: User) => !user.bot && user.state === UserAccountState.active)
},
wantSetUser(): string[] {
return this.addUserIds.split('@').map(id => id.trim())
},
willAddUser(): User[] {
return this.wantSetUser.map(id => this.users.find(user => user.name === id)).filter(<T>(user: T): user is Exclude<T, undefined> => !!user)
.filter(user => {
return !this.curGroup?.members.find(member => user.id === member.id)
})
}
}
})
</script>
<style>
.container {
display: flex;
justify-content: center;
}
.content {
max-width: 1200px;
}
.group {
cursor: pointer;
}
.right-button {
position: absolute;
right: 0;
bottom: 0;
}
</style>