2025-03-11 14:12:33 +01:00
|
|
|
<template>
|
|
|
|
<figure>
|
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>id</th>
|
|
|
|
<th>name</th>
|
|
|
|
<th>description</th>
|
|
|
|
<th v-if="edit"></th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<tr
|
2025-06-11 21:05:17 +02:00
|
|
|
v-for="location in locations.filter(l => !l.deleted)"
|
2025-03-11 14:12:33 +01:00
|
|
|
:key="location.id"
|
|
|
|
:class="{ removed: removed.has(location.id) }"
|
|
|
|
>
|
|
|
|
<template v-if='edit'>
|
|
|
|
<td>{{ location.id }}</td>
|
|
|
|
<td>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
:value="location.name"
|
|
|
|
@input="setLocation({ ...location, name: ($event as any).target.value })"
|
|
|
|
>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
:value="location.description"
|
|
|
|
@input="setLocation({ ...location, description: ($event as any).target.value || undefined })"
|
|
|
|
>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<button
|
|
|
|
:disabled="removed.has(location.id)"
|
|
|
|
type="button"
|
|
|
|
@click="delLocation(location.id)"
|
|
|
|
>Remove</button>
|
|
|
|
<button
|
2025-06-11 21:05:17 +02:00
|
|
|
v-if="changes.some(c => c.id === location.id)"
|
2025-03-11 14:12:33 +01:00
|
|
|
type="button"
|
|
|
|
@click="revertLocation(location.id)"
|
|
|
|
>Revert</button>
|
|
|
|
</td>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
|
|
|
<td>{{ location.id }}</td>
|
|
|
|
<td>{{ location.name }}</td>
|
|
|
|
<td>{{ location.description }}</td>
|
|
|
|
</template>
|
|
|
|
</tr>
|
|
|
|
<tr v-if='edit'>
|
|
|
|
<td>
|
2025-06-11 21:05:17 +02:00
|
|
|
{{ newLocationId }}
|
2025-03-11 14:12:33 +01:00
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
v-model="newLocationName"
|
|
|
|
>
|
|
|
|
</td>
|
|
|
|
<td colspan="2">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
@click="newLocation(newLocationName); newLocationName = ''"
|
|
|
|
>Add Location</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
<p v-if="changes.length">
|
|
|
|
Changes are not save yet.
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
@click="saveLocations"
|
|
|
|
>Save Changes</button>
|
|
|
|
</p>
|
|
|
|
<details>
|
|
|
|
<summary>Debug</summary>
|
|
|
|
<ol>
|
|
|
|
<li v-for="change in changes">
|
|
|
|
{{ JSON.stringify(change) }}
|
|
|
|
</li>
|
|
|
|
</ol>
|
|
|
|
</details>
|
|
|
|
</figure>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2025-06-11 21:05:17 +02:00
|
|
|
import type { ApiSchedule, ApiScheduleLocation } from '~/shared/types/api';
|
2025-03-15 13:46:13 +01:00
|
|
|
import { toId } from '~/shared/utils/functions';
|
2025-06-11 21:05:17 +02:00
|
|
|
import { applyUpdatesToArray } from '~/shared/utils/update';
|
2025-03-11 14:12:33 +01:00
|
|
|
|
|
|
|
defineProps<{
|
|
|
|
edit?: boolean
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const schedule = await useSchedule();
|
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
const changes = ref<ApiScheduleLocation[]>([]);
|
|
|
|
const removed = computed(() => new Set(changes.value.filter(c => c.deleted).map(c => c.id)));
|
2025-03-11 14:12:33 +01:00
|
|
|
|
|
|
|
function replaceChange(
|
2025-06-11 21:05:17 +02:00
|
|
|
change: ApiScheduleLocation,
|
|
|
|
changes: ApiScheduleLocation[],
|
2025-03-11 14:12:33 +01:00
|
|
|
) {
|
|
|
|
const index = changes.findIndex(item => (
|
2025-06-11 21:05:17 +02:00
|
|
|
item.deleted === change.deleted && item.id === change.id
|
2025-03-11 14:12:33 +01:00
|
|
|
));
|
|
|
|
const copy = [...changes];
|
|
|
|
if (index !== -1)
|
|
|
|
copy.splice(index, 1, change);
|
|
|
|
else
|
|
|
|
copy.push(change);
|
|
|
|
return copy;
|
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
function revertChange(id: number, changes: ApiScheduleLocation[]) {
|
|
|
|
return changes.filter(change => change.id !== id);
|
2025-03-11 14:12:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const newLocationName = ref("");
|
2025-06-11 21:05:17 +02:00
|
|
|
const newLocationId = computed(() => {
|
|
|
|
if (schedule.value.deleted) {
|
|
|
|
throw new Error("Unexpected deleted schedule");
|
|
|
|
}
|
|
|
|
return Math.max(
|
|
|
|
1,
|
|
|
|
...schedule.value.locations?.map(l => l.id) ?? [],
|
|
|
|
...changes.value.map(c => c.id)
|
|
|
|
) + 1;
|
|
|
|
});
|
|
|
|
function setLocation(location: ApiScheduleLocation) {
|
|
|
|
changes.value = replaceChange(location, changes.value);
|
2025-03-11 14:12:33 +01:00
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
function delLocation(id: number) {
|
|
|
|
const change = { id, updatedAt: "", deleted: true as const };
|
2025-03-11 14:12:33 +01:00
|
|
|
changes.value = replaceChange(change, changes.value);
|
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
function revertLocation(id: number) {
|
2025-03-11 14:12:33 +01:00
|
|
|
changes.value = revertChange(id, changes.value);
|
|
|
|
}
|
|
|
|
function newLocation(name: string) {
|
|
|
|
const change = {
|
2025-06-11 21:05:17 +02:00
|
|
|
id: newLocationId.value,
|
|
|
|
updatedAt: "",
|
|
|
|
name,
|
2025-03-11 14:12:33 +01:00
|
|
|
};
|
|
|
|
changes.value = replaceChange(change, changes.value);
|
|
|
|
}
|
|
|
|
async function saveLocations() {
|
|
|
|
try {
|
|
|
|
await $fetch("/api/schedule", {
|
|
|
|
method: "PATCH",
|
2025-06-11 21:05:17 +02:00
|
|
|
body: {
|
|
|
|
id: 111,
|
|
|
|
updatedAt: "",
|
|
|
|
locations: changes.value
|
|
|
|
} satisfies ApiSchedule,
|
2025-03-11 14:12:33 +01:00
|
|
|
});
|
|
|
|
changes.value = [];
|
|
|
|
} catch (err: any) {
|
|
|
|
console.error(err);
|
|
|
|
alert(err?.data?.message ?? err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const locations = computed(() => {
|
2025-06-11 21:05:17 +02:00
|
|
|
if (schedule.value.deleted) {
|
|
|
|
throw new Error("Unexpected deleted schedule");
|
|
|
|
}
|
|
|
|
const data = [...schedule.value.locations ?? []];
|
|
|
|
applyUpdatesToArray(changes.value.filter(change => !change.deleted), data);
|
2025-03-11 14:12:33 +01:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
table {
|
|
|
|
border-spacing: 0;
|
|
|
|
}
|
|
|
|
table th {
|
|
|
|
text-align: left;
|
|
|
|
border-bottom: 1px solid var(--foreground);
|
|
|
|
}
|
|
|
|
table :is(th, td) + :is(th, td) {
|
|
|
|
padding-inline-start: 0.4em;
|
|
|
|
}
|
|
|
|
.removed :is(td, input) {
|
|
|
|
text-decoration: line-through;
|
|
|
|
}
|
|
|
|
</style>
|