|
| 1 | +<script lang="ts"> |
| 2 | + import { hubManagementV1Api } from '$lib/api'; |
| 3 | + import CopyInput from '$lib/components/CopyInput.svelte'; |
| 4 | + import { Button } from '$lib/components/ui/button'; |
| 5 | + import { Content, Description, Dialog, Header, Title } from '$lib/components/ui/dialog'; |
| 6 | + import { handleApiError } from '$lib/errorhandling/apiErrorHandling'; |
| 7 | + import type { Hub } from './columns'; |
| 8 | +
|
| 9 | + interface Props { |
| 10 | + open: boolean; |
| 11 | + hub: Hub; |
| 12 | + } |
| 13 | +
|
| 14 | + let { open = $bindable<boolean>(), hub }: Props = $props(); |
| 15 | +
|
| 16 | + let loading = $state<boolean>(false); |
| 17 | + let code = $state<string | null>(null); |
| 18 | +
|
| 19 | + async function generatePairCode() { |
| 20 | + loading = true; |
| 21 | + try { |
| 22 | + const thing = await hubManagementV1Api.devicesGetPairCode(hub.id); |
| 23 | + code = thing.data; |
| 24 | + } catch (error) { |
| 25 | + await handleApiError(error); |
| 26 | + } finally { |
| 27 | + loading = false; |
| 28 | + } |
| 29 | + } |
| 30 | + function setOpen(o: boolean) { |
| 31 | + open = o; |
| 32 | + if (!o) { |
| 33 | + code = null; |
| 34 | + } |
| 35 | + } |
| 36 | +</script> |
| 37 | + |
| 38 | +<Dialog bind:open={() => open, setOpen}> |
| 39 | + <Content> |
| 40 | + <Header> |
| 41 | + <Title>{loading ? 'Generating...' : code ? 'Pair code generated' : 'Generate pair code?'}</Title> |
| 42 | + {#if !loading} |
| 43 | + <Description> |
| 44 | + {#if code} |
| 45 | + Pair code generated for <strong>{hub.name}</strong><br /> |
| 46 | + The code below will not be accessible later, please copy it now and update clients with it |
| 47 | + {:else} |
| 48 | + You are about to generate a pair code for <strong>{hub.name}</strong><br /> |
| 49 | + It will be vaild for 15 minutes after its creation.<br /> |
| 50 | + There is only one active pair code per hub, newly generated ones will override the older active ones.<br /> |
| 51 | + {/if} |
| 52 | + </Description> |
| 53 | + {/if} |
| 54 | + </Header> |
| 55 | + {#if !loading} |
| 56 | + {#if code} |
| 57 | + <div> |
| 58 | + <strong class="text-sm text-muted-foreground font-medium">New token:</strong> |
| 59 | + <CopyInput value={code} /> |
| 60 | + </div> |
| 61 | + <Button onclick={() => setOpen(false)}>Code has been copied, close</Button> |
| 62 | + {:else} |
| 63 | + <Button onclick={generatePairCode}>Get Pair Code</Button> |
| 64 | + {/if} |
| 65 | + {/if} |
| 66 | + </Content> |
| 67 | +</Dialog> |
0 commit comments