|
| 1 | +<script lang="ts"> |
| 2 | + import { onMount } from "svelte"; |
| 3 | + import { goto } from "$app/navigation"; |
| 4 | + import { get } from "svelte/store"; |
| 5 | + import { api } from "$lib/api"; |
| 6 | + import { sessionToken } from "$lib/session"; |
| 7 | +
|
| 8 | + interface Application { |
| 9 | + id: string; |
| 10 | + status: string; |
| 11 | + justification: string | null; |
| 12 | + requestedOntologies: string[]; |
| 13 | + consumer?: { |
| 14 | + ename: string; |
| 15 | + name: string | null; |
| 16 | + contactEmail: string | null; |
| 17 | + webhookBaseUrl: string | null; |
| 18 | + }; |
| 19 | + } |
| 20 | +
|
| 21 | + let applications = $state<Application[]>([]); |
| 22 | + let error = $state<string | null>(null); |
| 23 | + let loading = $state(true); |
| 24 | + let notAdmin = $state(false); |
| 25 | +
|
| 26 | + const token = () => get(sessionToken); |
| 27 | +
|
| 28 | + async function load() { |
| 29 | + loading = true; |
| 30 | + error = null; |
| 31 | + try { |
| 32 | + const result = await api<{ applications: Application[] }>( |
| 33 | + "/api/admin/applications?status=pending", |
| 34 | + { token: token() }, |
| 35 | + ); |
| 36 | + applications = result.applications; |
| 37 | + } catch (e) { |
| 38 | + const msg = e instanceof Error ? e.message : "failed to load"; |
| 39 | + if (msg.includes("admin")) notAdmin = true; |
| 40 | + else error = msg; |
| 41 | + } finally { |
| 42 | + loading = false; |
| 43 | + } |
| 44 | + } |
| 45 | +
|
| 46 | + async function review(id: string, action: "approve" | "reject") { |
| 47 | + try { |
| 48 | + await api(`/api/admin/applications/${id}/${action}`, { |
| 49 | + method: "POST", |
| 50 | + token: token(), |
| 51 | + }); |
| 52 | + await load(); |
| 53 | + } catch (e) { |
| 54 | + error = e instanceof Error ? e.message : "review failed"; |
| 55 | + } |
| 56 | + } |
| 57 | +
|
| 58 | + onMount(() => { |
| 59 | + if (!token()) { |
| 60 | + goto("/"); |
| 61 | + return; |
| 62 | + } |
| 63 | + void load(); |
| 64 | + }); |
| 65 | +</script> |
| 66 | + |
| 67 | +<section> |
| 68 | + <div class="flex items-center justify-between"> |
| 69 | + <h1 class="text-2xl font-bold text-gray-900">Pending applications</h1> |
| 70 | + <a href="/admin/dead-letters" class="text-sm text-indigo-600 hover:underline"> |
| 71 | + Dead letters → |
| 72 | + </a> |
| 73 | + </div> |
| 74 | + |
| 75 | + {#if notAdmin} |
| 76 | + <p class="mt-6 rounded bg-amber-50 px-4 py-3 text-sm text-amber-700"> |
| 77 | + Your eName is not in the admin allowlist. |
| 78 | + </p> |
| 79 | + {:else if error} |
| 80 | + <p class="mt-4 rounded bg-red-50 px-4 py-2 text-sm text-red-700">{error}</p> |
| 81 | + {:else if loading} |
| 82 | + <p class="mt-4 text-gray-500">Loading…</p> |
| 83 | + {:else} |
| 84 | + <ul class="mt-6 space-y-4"> |
| 85 | + {#each applications as app (app.id)} |
| 86 | + <li class="rounded-lg border border-gray-200 bg-white p-5"> |
| 87 | + <div class="flex items-start justify-between"> |
| 88 | + <div> |
| 89 | + <p class="font-semibold text-gray-900"> |
| 90 | + {app.consumer?.name ?? app.consumer?.ename} |
| 91 | + </p> |
| 92 | + <p class="text-sm text-gray-500">{app.consumer?.ename}</p> |
| 93 | + <p class="text-sm text-gray-500"> |
| 94 | + {app.consumer?.contactEmail ?? "no email"} · |
| 95 | + {app.consumer?.webhookBaseUrl ?? "no webhook URL"} |
| 96 | + </p> |
| 97 | + </div> |
| 98 | + <div class="flex gap-2"> |
| 99 | + <button |
| 100 | + class="rounded bg-green-600 px-3 py-1.5 text-sm text-white" |
| 101 | + onclick={() => review(app.id, "approve")} |
| 102 | + > |
| 103 | + Approve |
| 104 | + </button> |
| 105 | + <button |
| 106 | + class="rounded bg-red-600 px-3 py-1.5 text-sm text-white" |
| 107 | + onclick={() => review(app.id, "reject")} |
| 108 | + > |
| 109 | + Reject |
| 110 | + </button> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + {#if app.justification} |
| 114 | + <p class="mt-3 text-sm text-gray-700">{app.justification}</p> |
| 115 | + {/if} |
| 116 | + {#if app.requestedOntologies.length} |
| 117 | + <p class="mt-2 text-xs text-gray-500"> |
| 118 | + Requested: {app.requestedOntologies.join(", ")} |
| 119 | + </p> |
| 120 | + {/if} |
| 121 | + </li> |
| 122 | + {:else} |
| 123 | + <li class="text-gray-400">No pending applications.</li> |
| 124 | + {/each} |
| 125 | + </ul> |
| 126 | + {/if} |
| 127 | +</section> |
0 commit comments