-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAPIKeyView.vue
More file actions
344 lines (325 loc) · 8.62 KB
/
Copy pathAPIKeyView.vue
File metadata and controls
344 lines (325 loc) · 8.62 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
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
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
// Unhead
import { useHead } from "@unhead/vue";
useHead({
title: `${t("api_keys.view_title")} | PRUNplanner`,
});
// Composables
import { useAPIKeys } from "@/features/api_keys/useAPIKeys";
// Util
import { formatDate, relativeFromDate } from "@/util/date";
// UI
import PButton from "@/ui/components/PButton.vue";
import PTable from "@/ui/components/PTable.vue";
import PForm from "@/ui/components/PForm.vue";
import PFormItem from "@/ui/components/PFormItem.vue";
import PFormSeperator from "@/ui/components/PFormSeperator.vue";
import PInput from "@/ui/components/PInput.vue";
import { NModal } from "naive-ui";
import { CheckSharp, RestartAltSharp } from "@vicons/material";
const {
loaded,
apiKeyData,
lastCreatedKey,
inDeletionId,
fetchAPIKeys,
createAPIKey,
resetCreation,
deleteAPIKey,
} = useAPIKeys();
const showCreateModal = ref<boolean>(false);
const creationStep = ref<"start" | "key" | "error">("start");
const creationAPIKeyName = ref<string>("");
const creationLoading = ref<boolean>(false);
const creationHasName = computed(
() =>
creationAPIKeyName.value &&
creationAPIKeyName.value.trim().length > 0
);
async function createKey(): Promise<void> {
creationLoading.value = true;
const creationStatus: boolean = await createAPIKey(
creationAPIKeyName.value
).finally(() => {});
if (creationStatus) {
creationStep.value = "key";
} else {
creationStep.value = "error";
}
creationAPIKeyName.value = "";
creationLoading.value = false;
}
onMounted(async () => {
fetchAPIKeys();
});
</script>
<template>
<n-modal
v-model:show="showCreateModal"
preset="dialog"
:title="t('api_keys.create.title')"
:show-icon="false">
<template #header>{{ $t("api_keys.create.title") }}</template>
<div v-if="creationStep === 'start'">
<PForm>
<PFormItem :label="t('api_keys.create.form.key_name')">
<PInput v-model:value="creationAPIKeyName" class="w-full" />
</PFormItem>
<PFormSeperator>
<span class="py-1 text-white/50 text-xs">
{{ $t("api_keys.create.form.key_description") }}
</span>
</PFormSeperator>
</PForm>
</div>
<div
v-else-if="creationStep === 'key'"
class="flex flex-col gap-3 text-center">
<div
class="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-prunplanner">
<CheckSharp class="w-6 h-6" />
</div>
<h3 class="text-center text-lg font-bold">
{{ $t("api_keys.create.form.success_title") }}
</h3>
<div
class="flex flex-col gap-2 items-center bg-white/5 border border-dark-gray border-dashed rounded p-3">
<span
class="uppercase font-bold text-xs text-center text-white/80">
{{ $t("api_keys.create.form.success_key") }}
</span>
<span class="font-mono text-prunplanner text-nowrap">
{{ lastCreatedKey }}
</span>
</div>
<div class="text-red-500">
{{ $t("api_keys.create.form.success_note") }}
</div>
</div>
<div v-else-if="creationStep === 'error'">
<div class="text-center text-red-500">
{{ $t("api_keys.create.form.error") }}
</div>
</div>
<template #action>
<div
v-if="creationStep === 'start'"
class="flex flex-row gap-3 items-end">
<PButton
@click="
{
showCreateModal = false;
}
">
{{ $t("api_keys.create.buttons.cancel") }}
</PButton>
<PButton
:loading="creationLoading"
:disabled="!creationHasName"
@click="createKey">
{{ $t("api_keys.create.buttons.create") }}
</PButton>
</div>
<div v-else-if="creationStep === 'key'" class="w-full">
<PButton
class="w-full"
@click="
() => {
showCreateModal = false;
creationStep = 'start';
resetCreation();
}
">
{{ $t("api_keys.create.buttons.saved") }}
</PButton>
</div>
</template>
</n-modal>
<div class="min-h-screen flex flex-col">
<div
class="px-6 py-3 border-b border-white/10 flex flex-row justify-between">
<h1 class="text-2xl font-bold">{{ $t("api_keys.title") }}</h1>
</div>
<div>
<div
class="grid grid-cols-1 lg:grid-cols-3 divide-x divide-white/10 min-h-screen">
<div class="xl:col-span-2 flex flex-col gap-3 px-6 py-3">
<i18n-t keypath="api_keys.info.description.p1" tag="div">
<template #link>
<a
href="https://api.prunplanner.org/docs"
target="_blank"
class="font-bold underline hover:text-prunplanner"
>{{ $t("api_keys.info.description.link") }}</a
>
</template>
</i18n-t>
<div>
{{ $t("api_keys.info.description.p2") }}
</div>
<div
class="bg-prunplanner/10 border-l-4 border-prunplanner p-3">
<p class="text-white font-mono text-xs">
{{ $t("api_keys.info.description.warning") }}
</p>
</div>
<div>
<div
class="flex flex-row justify-between items-center pt-6 pb-3">
<h2 class="text-lg font-bold">
{{ $t("api_keys.info.manage.title") }}
</h2>
<div class="flex flex-row gap-3">
<PButton
@click="
() => {
showCreateModal = !showCreateModal;
}
">
{{
$t("api_keys.manage.buttons.new_apikey")
}}
</PButton>
<PButton @click="fetchAPIKeys">
<template #icon>
<RestartAltSharp />
</template>
</PButton>
</div>
</div>
<div>
<PTable
v-if="apiKeyData !== null"
class="w-full"
striped>
<thead>
<tr>
<th>
{{
$t(
"api_keys.manage.table.apikey_name"
)
}}
</th>
<th>
{{
$t(
"api_keys.manage.table.apikey_prefix"
)
}}
</th>
<th>
{{
$t(
"api_keys.manage.table.apikey_created"
)
}}
</th>
<th>
{{
$t(
"api_keys.manage.table.apikey_lastused"
)
}}
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr
v-for="apiKey in apiKeyData"
:key="apiKey.id">
<td class="font-bold">
{{ apiKey.name }}
</td>
<td class="font-mono">
{{ apiKey.prefix }}
</td>
<td class="font-mono">
{{ formatDate(apiKey.created) }}
</td>
<td>
<span v-if="apiKey.last_used">
{{
relativeFromDate(
apiKey.last_used
)
}}
</span>
<span v-else>—</span>
</td>
<td class="flex justify-end">
<PButton
type="error"
:loading="
apiKey.id === inDeletionId
"
@click="
deleteAPIKey(apiKey.id)
">
{{
$t(
"api_keys.manage.buttons.revoke_apikey"
)
}}
</PButton>
</td>
</tr>
</tbody>
</PTable>
<div
v-else-if="!loaded"
class="text-center font-mono text-white/80">
Loading API Keys.
</div>
<div
v-else
class="text-center font-mono text-white/80">
No API Keys yet. Create your first API Key.
</div>
</div>
</div>
</div>
<div class="px-6 py-3">
<div
class="bg-white/5 border border-dark-gray border-dashed rounded flex flex-col gap-3 p-3">
<h3 class="uppercase font-bold">
{{ $t("api_keys.implementation.title") }}
</h3>
<div class="space-y-4">
<div>
<span
class="text-xs text-prunplanner block mb-1">
HTTP Header
</span>
<code
class="text-xs text-white/80 bg-gray-dark p-2 rounded block border border-white/10">
Authorization: Api-Key <KEY>
</code>
</div>
<div>
<span
class="text-xs text-prunplanner block mb-1">
URL Parameter
</span>
<code
class="text-xs text-white/80 bg-gray-dark p-2 rounded block border border-white/10">
?api_key=<KEY>
</code>
</div>
<a
href="https://api.prunplanner.org/docs"
target="_blank"
class="font-mono text-xs font-bold text-white hover:text-prunplanner underline mt-2">
{{ $t("api_keys.implementation.link") }}
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</template>