191 lines
6.3 KiB
Svelte
191 lines
6.3 KiB
Svelte
<script>
|
|
import { t, _ } from "svelte-i18n";
|
|
import store from "../../store";
|
|
import { ScanStationService, TrackService } from "@odit/lfk-client-js";
|
|
|
|
import PromiseError from "../base/PromiseError.svelte";
|
|
import ConfirmScanStationDeletion from "./ConfirmScanStationDeletion.svelte";
|
|
import Select from "svelte-select";
|
|
let data_loaded = false;
|
|
let modal_open;
|
|
let delete_station;
|
|
export let params;
|
|
$: delete_triggered = false;
|
|
$: original_data = {};
|
|
$: editable = {};
|
|
$: tracks = [];
|
|
$: track = {};
|
|
$: changes_performed = !(
|
|
JSON.stringify(original_data) === JSON.stringify(editable)
|
|
);
|
|
$: save_enabled = changes_performed;
|
|
const promise = ScanStationService.scanStationControllerGetOne(
|
|
params.stationid
|
|
).then((data) => {
|
|
data_loaded = true;
|
|
data.track = data.track.id;
|
|
original_data = Object.assign(original_data, data);
|
|
editable = Object.assign(editable, original_data);
|
|
TrackService.trackControllerGetAll().then((val) => {
|
|
tracks = val.map((t) => {
|
|
return { label: t.name || `#{t.id}`, value: t };
|
|
});
|
|
track = tracks.find((t) => t.value.id == editable.track);
|
|
});
|
|
});
|
|
function submit() {
|
|
if (data_loaded === true && save_enabled) {
|
|
toast($_("station-is-being-updated"));
|
|
ScanStationService.scanStationControllerPut(original_data.id, editable)
|
|
.then((resp) => {
|
|
Object.assign(original_data, editable);
|
|
original_data = original_data;
|
|
toast.success($_("updated-station"));
|
|
})
|
|
.catch((err) => {});
|
|
} else {
|
|
}
|
|
}
|
|
function deleteStation() {
|
|
ScanStationService.scanStationControllerRemove(original_data.id, false)
|
|
.then((resp) => {
|
|
toast.success($_("station-deleted"));
|
|
location.replace("./");
|
|
})
|
|
.catch((err) => {
|
|
modal_open = true;
|
|
delete_station = original_data;
|
|
});
|
|
}
|
|
</script>
|
|
|
|
<ConfirmScanStationDeletion bind:modal_open bind:delete_station />
|
|
{#await promise}
|
|
{$_("loading-station-details")}
|
|
{:then}
|
|
<section class="container p-5 select-none">
|
|
<div class="flex flex-row mb-4">
|
|
<div class="w-full">
|
|
<nav class="w-full flex">
|
|
<ol class="list-none flex flex-row items-center justify-start">
|
|
<li class="flex items-center">
|
|
<a class="mr-2" href="./"
|
|
><svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
width="24"
|
|
height="24"
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
stroke-width="2"
|
|
stroke-linecap="round"
|
|
stroke-linejoin="round"
|
|
class="inline-block"
|
|
><path d="m12 19-7-7 7-7" /><path d="M19 12H5" /></svg
|
|
>
|
|
{$_("scanstations")}</a
|
|
>
|
|
</li>
|
|
</ol>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
<div class="mb-4 text-3xl font-extrabold leading-tight">
|
|
{$_("scanstation")} #{original_data.id}<br>"{original_data.description}"
|
|
<div data-id="stations_actions_${editable.id}">
|
|
{#if store.state.jwtinfo.userdetails.permissions.includes("STATION:DELETE")}
|
|
{#if delete_triggered}
|
|
<button
|
|
on:click={deleteStation}
|
|
class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:w-auto sm:text-sm"
|
|
>{$_("confirm-deletion")}</button
|
|
>
|
|
<button
|
|
on:click={() => {
|
|
delete_triggered = !delete_triggered;
|
|
}}
|
|
class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-400 text-base font-medium text-white sm:w-auto sm:text-sm"
|
|
>{$_("cancel")}</button
|
|
>
|
|
{/if}
|
|
{#if !delete_triggered}
|
|
<button
|
|
on:click={() => {
|
|
delete_triggered = true;
|
|
}}
|
|
type="button"
|
|
class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-600 text-base font-medium text-white hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:w-auto sm:text-sm"
|
|
>{$_("delete-station")}</button
|
|
>
|
|
{/if}
|
|
{/if}
|
|
{#if !delete_triggered}
|
|
<button
|
|
disabled={!save_enabled}
|
|
class:opacity-50={!save_enabled}
|
|
type="button"
|
|
on:click={submit}
|
|
class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:w-auto sm:text-sm mb-1 lg:mb-0"
|
|
>{$_("save-changes")}</button
|
|
>
|
|
{/if}
|
|
</div>
|
|
</div>
|
|
<!-- -->
|
|
<div class="mt-2 text-sm w-full">
|
|
<label for="track" class="block text-sm font-semibold text-gray-700"
|
|
>Track</label
|
|
>
|
|
<Select
|
|
containerClasses="rounded-l-md focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
|
|
itemFilter={(label, filterText, option) =>
|
|
label.toLowerCase().includes(filterText.toLowerCase()) ||
|
|
option.value.id.toString().startsWith(filterText.toLowerCase())}
|
|
items={tracks}
|
|
showChevron={true}
|
|
placeholder="Search for a track (by name or id)."
|
|
noOptionsMessage="No track found"
|
|
bind:selectedValue={track}
|
|
on:select={(selectedValue) =>
|
|
(editable.track = selectedValue.detail.value.id)}
|
|
on:clear={() => (track = null)}
|
|
/>
|
|
</div>
|
|
<div class="mt-2 text-sm w-full">
|
|
<label for="description" class="font-semibold text-gray-700"
|
|
>{$_("description")}</label
|
|
>
|
|
<input
|
|
autocomplete="off"
|
|
placeholder={$_("description")}
|
|
type="text"
|
|
bind:value={editable.description}
|
|
name="description"
|
|
class="focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm rounded-l-md sm:text-sm border-gray-300 border bg-gray-50 text-neutral-800 rounded-md p-2"
|
|
/>
|
|
</div>
|
|
<div class="mt-2 text-sm w-full">
|
|
<label for="enabled" class="font-semibold text-gray-700"
|
|
>{$_("enabled")}</label
|
|
>
|
|
<br />
|
|
<p class="text-gray-500">
|
|
<input
|
|
id="enabled"
|
|
on:change={() => {
|
|
editable.enabled = !editable.enabled;
|
|
}}
|
|
name="enabled"
|
|
type="checkbox"
|
|
checked={editable.enabled}
|
|
class="focus:ring-indigo-500 size-4 text-indigo-600 border-gray-300 rounded"
|
|
/>
|
|
{$_("this-scanstation-is")}
|
|
{#if editable.enabled}{$_("enabled")}{:else}{$_("disabled")}{/if}
|
|
</p>
|
|
</div>
|
|
</section>
|
|
{:catch error}
|
|
<PromiseError {error} />
|
|
{/await}
|