|
| 1 | +<script lang="ts"> |
| 2 | + import { goto } from '$app/navigation'; |
| 3 | + import { resolve } from '$app/paths'; |
| 4 | + import { page } from '$app/state'; |
| 5 | + import { shockersV1Api } from '$lib/api'; |
| 6 | + import { ShockerModelType, type ShockerWithDevice } from '$lib/api/internal/v1'; |
| 7 | + import Container from '$lib/components/Container.svelte'; |
| 8 | + import { dialog } from '$lib/components/dialog-manager/dialog-store.svelte'; |
| 9 | + import TextInput from '$lib/components/input/TextInput.svelte'; |
| 10 | + import Button from '$lib/components/ui/button/button.svelte'; |
| 11 | + import { Field, FieldLabel } from '$lib/components/ui/field/index.js'; |
| 12 | + import { Input } from '$lib/components/ui/input'; |
| 13 | + import * as Select from '$lib/components/ui/select'; |
| 14 | + import PauseToggle from '$lib/components/utils/PauseToggle.svelte'; |
| 15 | + import { handleApiError } from '$lib/errorhandling/apiErrorHandling'; |
| 16 | + import { refreshOwnHubs } from '$lib/state/hubs-state.svelte'; |
| 17 | + import { LoaderCircle } from '@lucide/svelte'; |
| 18 | + import { onMount } from 'svelte'; |
| 19 | + import { toast } from 'svelte-sonner'; |
| 20 | +
|
| 21 | + const modelOptions = [ |
| 22 | + { value: ShockerModelType.CaiXianlin, label: 'CaiXianlin' }, |
| 23 | + { value: ShockerModelType.PetTrainer, label: 'PetTrainer' }, |
| 24 | + { value: ShockerModelType.Petrainer998Dr, label: 'Petrainer998DR' }, |
| 25 | + ]; |
| 26 | +
|
| 27 | + let shocker = $state<ShockerWithDevice | null>(null); |
| 28 | + let name = $state(''); |
| 29 | + let rfId = $state(0); |
| 30 | + let model = $state<ShockerModelType>(ShockerModelType.CaiXianlin); |
| 31 | + let saving = $state(false); |
| 32 | +
|
| 33 | + onMount(async () => { |
| 34 | + try { |
| 35 | + const shockerId = page.params.shockerId!; |
| 36 | + const response = await shockersV1Api.shockerGetShockerById(shockerId); |
| 37 | + shocker = response.data; |
| 38 | + name = shocker.name; |
| 39 | + rfId = shocker.rfId; |
| 40 | + model = shocker.model; |
| 41 | + } catch (error) { |
| 42 | + handleApiError(error); |
| 43 | + goto(resolve('/shockers/own')); |
| 44 | + } |
| 45 | + }); |
| 46 | +
|
| 47 | + async function save() { |
| 48 | + if (!shocker || !name.trim()) return; |
| 49 | + saving = true; |
| 50 | + try { |
| 51 | + await shockersV1Api.shockerEditShocker(shocker.id, { |
| 52 | + name: name.trim(), |
| 53 | + rfId, |
| 54 | + model, |
| 55 | + device: shocker.device, |
| 56 | + }); |
| 57 | + toast.success('Shocker updated'); |
| 58 | + await refreshOwnHubs(); |
| 59 | + } catch (error) { |
| 60 | + handleApiError(error); |
| 61 | + } finally { |
| 62 | + saving = false; |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + async function deleteShocker() { |
| 67 | + if (!shocker) return; |
| 68 | + const result = await dialog.confirm({ |
| 69 | + title: 'Delete Shocker', |
| 70 | + desc: `Are you sure you want to delete "${shocker.name}"? This action cannot be undone.`, |
| 71 | + confirmButtonText: 'Delete', |
| 72 | + }); |
| 73 | + if (!result.confirmed) return; |
| 74 | + try { |
| 75 | + await shockersV1Api.shockerRemoveShocker(shocker.id); |
| 76 | + toast.success(`Shocker "${shocker.name}" deleted`); |
| 77 | + await refreshOwnHubs(); |
| 78 | + goto(resolve('/shockers/own')); |
| 79 | + } catch (error) { |
| 80 | + handleApiError(error); |
| 81 | + } |
| 82 | + } |
| 83 | +</script> |
| 84 | + |
| 85 | +<Container> |
| 86 | + {#if !shocker} |
| 87 | + <div class="flex items-center gap-2 p-8"> |
| 88 | + <LoaderCircle class="animate-spin" /> |
| 89 | + <span>Loading shocker...</span> |
| 90 | + </div> |
| 91 | + {:else} |
| 92 | + <div class="mx-auto flex w-full max-w-lg flex-col gap-6 py-4"> |
| 93 | + <h1 class="text-2xl font-bold">Edit Shocker</h1> |
| 94 | + |
| 95 | + <TextInput label="Name" placeholder="Shocker name" bind:value={name} /> |
| 96 | + |
| 97 | + <Field class="gap-2"> |
| 98 | + <FieldLabel>RF ID</FieldLabel> |
| 99 | + <Input type="number" bind:value={rfId} min={1} /> |
| 100 | + </Field> |
| 101 | + |
| 102 | + <Field class="gap-2"> |
| 103 | + <FieldLabel>Model</FieldLabel> |
| 104 | + <Select.Root type="single" name="model" bind:value={model}> |
| 105 | + <Select.Trigger> |
| 106 | + {modelOptions.find((o) => o.value === model)?.label ?? 'Select model'} |
| 107 | + </Select.Trigger> |
| 108 | + <Select.Content> |
| 109 | + <Select.Group> |
| 110 | + {#each modelOptions as option (option.value)} |
| 111 | + <Select.Item value={option.value} label={option.label}>{option.label}</Select.Item> |
| 112 | + {/each} |
| 113 | + </Select.Group> |
| 114 | + </Select.Content> |
| 115 | + </Select.Root> |
| 116 | + </Field> |
| 117 | + |
| 118 | + <Field class="gap-2"> |
| 119 | + <FieldLabel>Pause State</FieldLabel> |
| 120 | + <div class="flex items-center gap-2"> |
| 121 | + <PauseToggle |
| 122 | + shockerId={shocker.id} |
| 123 | + bind:paused={shocker.isPaused} |
| 124 | + userShareUserId={undefined} |
| 125 | + onPausedChange={() => {}} |
| 126 | + /> |
| 127 | + <span class="text-muted-foreground text-sm"> |
| 128 | + {shocker.isPaused ? 'Shocker is paused' : 'Shocker is active'} |
| 129 | + </span> |
| 130 | + </div> |
| 131 | + </Field> |
| 132 | + |
| 133 | + <Button disabled={saving || !name.trim()} onclick={save}> |
| 134 | + {#if saving}<LoaderCircle class="animate-spin" />{/if} |
| 135 | + Save Changes |
| 136 | + </Button> |
| 137 | + |
| 138 | + <hr class="border-destructive/30" /> |
| 139 | + |
| 140 | + <div class="flex flex-col gap-2"> |
| 141 | + <h2 class="text-destructive text-lg font-semibold">Danger Zone</h2> |
| 142 | + <p class="text-muted-foreground text-sm"> |
| 143 | + Permanently delete this shocker. This action cannot be undone. |
| 144 | + </p> |
| 145 | + <Button variant="destructive" onclick={deleteShocker}>Delete Shocker</Button> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + {/if} |
| 149 | +</Container> |
0 commit comments