2025-03-15 14:20:38 +01:00
|
|
|
<template>
|
2025-06-11 21:05:17 +02:00
|
|
|
<div v-if="schedule.deleted">
|
|
|
|
Error: Unexpected deleted schedule.
|
|
|
|
</div>
|
|
|
|
<div v-else>
|
2025-03-15 14:20:38 +01:00
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>id</th>
|
|
|
|
<th>name</th>
|
|
|
|
<th>description</th>
|
|
|
|
<th>p</th>
|
|
|
|
<th>s</th>
|
|
|
|
<th v-if="edit"></th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<template v-if="edit">
|
|
|
|
<tr
|
2025-06-11 21:05:17 +02:00
|
|
|
v-for="event in events.filter(e => !e.deleted)"
|
2025-03-15 14:20:38 +01:00
|
|
|
:key="event.id"
|
|
|
|
:class="{ removed: removed.has(event.id) }"
|
|
|
|
>
|
|
|
|
<td>{{ event.id }}</td>
|
|
|
|
<td>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
:value="event.name"
|
2025-03-15 14:28:27 +01:00
|
|
|
:disabled="!canEdit(event)"
|
2025-03-15 14:20:38 +01:00
|
|
|
@input="editEvent(event, { name: ($event as any).target.value })"
|
|
|
|
>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<input
|
|
|
|
type="text"
|
2025-03-15 14:28:27 +01:00
|
|
|
:disabled="!canEdit(event)"
|
2025-03-15 14:20:38 +01:00
|
|
|
:value="event.description"
|
|
|
|
@input="editEvent(event, { description: ($event as any).target.value })"
|
|
|
|
>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
2025-05-24 20:01:23 +02:00
|
|
|
:disabled="!accountStore.canEditPublic"
|
2025-03-15 14:20:38 +01:00
|
|
|
:value="!event.crew"
|
|
|
|
:checked="!event.crew"
|
|
|
|
@change="editEvent(event, { crew: !($event as any).target.value })"
|
|
|
|
>
|
|
|
|
</td>
|
|
|
|
<td>{{ event.slots.length ? event.slots.length : "" }}</td>
|
|
|
|
<td>
|
|
|
|
<button
|
|
|
|
type="button"
|
2025-03-15 14:28:27 +01:00
|
|
|
:disabled="!canEdit(event) || removed.has(event.id)"
|
2025-03-15 14:20:38 +01:00
|
|
|
@click="delEvent(event.id)"
|
|
|
|
>Delete</button>
|
|
|
|
<button
|
2025-06-11 21:05:17 +02:00
|
|
|
v-if="changes.some(c => c.id === event.id)"
|
2025-03-15 14:20:38 +01:00
|
|
|
type="button"
|
|
|
|
@click="revertEvent(event.id)"
|
|
|
|
>Revert</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{{ toId(newEventName) }}</td>
|
|
|
|
<td>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
v-model="newEventName"
|
|
|
|
>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
v-model="newEventDescription"
|
|
|
|
>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
2025-05-24 20:01:23 +02:00
|
|
|
:disabled="!accountStore.canEditPublic"
|
2025-03-15 14:20:38 +01:00
|
|
|
v-model="newEventPublic"
|
|
|
|
>
|
|
|
|
</td>
|
|
|
|
<td></td>
|
|
|
|
<td>
|
|
|
|
<button
|
|
|
|
v-if="eventExists(newEventName)"
|
|
|
|
disabled
|
|
|
|
>Event already exists</button>
|
|
|
|
<button
|
|
|
|
v-else
|
|
|
|
type="button"
|
|
|
|
@click="newEvent"
|
|
|
|
>Add Event</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
|
|
|
<tr
|
2025-06-11 21:05:17 +02:00
|
|
|
v-for="event in events.filter(e => !e.deleted)"
|
2025-03-15 14:20:38 +01:00
|
|
|
:key="event.id"
|
|
|
|
>
|
|
|
|
<td>{{ event.id }}</td>
|
|
|
|
<td>{{ event.name }}</td>
|
|
|
|
<td>{{ event.description }}</td>
|
|
|
|
<td>{{ event.crew ? "" : "Yes"}}</td>
|
|
|
|
<td>{{ event.slots.length ? event.slots.length : "" }}</td>
|
|
|
|
</tr>
|
|
|
|
</template>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
<p v-if="changes.length">
|
|
|
|
Changes are not saved yet.
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
@click="saveEvents"
|
|
|
|
>Save Changes</button>
|
|
|
|
</p>
|
|
|
|
<details>
|
|
|
|
<summary>Debug</summary>
|
|
|
|
<ol>
|
|
|
|
<li v-for="change in changes">
|
|
|
|
{{ JSON.stringify(change) }}
|
|
|
|
</li>
|
|
|
|
</ol>
|
|
|
|
</details>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2025-06-11 21:05:17 +02:00
|
|
|
import type { ApiSchedule, ApiScheduleEvent } from '~/shared/types/api';
|
2025-03-15 14:20:38 +01:00
|
|
|
import { toId } from '~/shared/utils/functions';
|
2025-06-11 21:05:17 +02:00
|
|
|
import { applyUpdatesToArray } from '~/shared/utils/update';
|
2025-03-15 14:20:38 +01:00
|
|
|
|
|
|
|
defineProps<{
|
|
|
|
edit?: boolean,
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const schedule = await useSchedule();
|
2025-05-24 20:01:23 +02:00
|
|
|
const accountStore = useAccountStore();
|
2025-03-15 14:28:27 +01:00
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
function canEdit(event: ApiScheduleEvent) {
|
|
|
|
return !event.deleted && (event.crew || accountStore.canEditPublic);
|
2025-03-15 14:28:27 +01:00
|
|
|
}
|
2025-03-15 14:20:38 +01:00
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
const changes = ref<ApiScheduleEvent[]>([]);
|
|
|
|
const removed = computed(() => new Set(changes.value.filter(c => c.deleted).map(c => c.id)));
|
2025-03-15 14:20:38 +01:00
|
|
|
function replaceChange(
|
2025-06-11 21:05:17 +02:00
|
|
|
change: ApiScheduleEvent,
|
|
|
|
changes: ApiScheduleEvent[],
|
2025-03-15 14:20:38 +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-15 14:20:38 +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: ApiScheduleEvent[]) {
|
|
|
|
return changes.filter(change => change.id !== id);
|
2025-03-15 14:20:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const newEventName = ref("");
|
|
|
|
const newEventDescription = ref("");
|
|
|
|
const newEventPublic = ref(false);
|
|
|
|
function editEvent(
|
2025-06-11 21:05:17 +02:00
|
|
|
event: Extract<ApiScheduleEvent, { deleted?: false }>,
|
2025-03-15 14:20:38 +01:00
|
|
|
edits: { name?: string, description?: string, crew?: boolean }
|
|
|
|
) {
|
|
|
|
const copy = { ...event };
|
|
|
|
if (edits.name !== undefined) {
|
|
|
|
copy.name = edits.name;
|
|
|
|
}
|
|
|
|
if (edits.description !== undefined) {
|
|
|
|
copy.description = edits.description || undefined;
|
|
|
|
}
|
|
|
|
if (edits.crew !== undefined) {
|
|
|
|
copy.crew = edits.crew || undefined;
|
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
changes.value = replaceChange(copy, changes.value);
|
2025-03-15 14:20:38 +01:00
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
function delEvent(id: number) {
|
|
|
|
const change = { id, updatedAt: "", deleted: true as const };
|
2025-03-15 14:20:38 +01:00
|
|
|
changes.value = replaceChange(change, changes.value);
|
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
function revertEvent(id: number) {
|
2025-03-15 14:20:38 +01:00
|
|
|
changes.value = revertChange(id, changes.value);
|
|
|
|
}
|
|
|
|
function eventExists(name: string) {
|
2025-06-11 21:05:17 +02:00
|
|
|
if (schedule.value.deleted) {
|
|
|
|
throw new Error("Unexpected deleted schedule");
|
|
|
|
}
|
|
|
|
name = toId(name);
|
2025-03-15 14:20:38 +01:00
|
|
|
return (
|
2025-06-11 21:05:17 +02:00
|
|
|
schedule.value.events?.some(e => !e.deleted && toId(e.name) === name)
|
|
|
|
|| changes.value.some(c => !c.deleted && c.name === name)
|
2025-03-15 14:20:38 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
function newEvent() {
|
|
|
|
if (eventExists(newEventName.value)) {
|
|
|
|
alert(`Event ${newEventName.value} already exists`);
|
|
|
|
return;
|
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
if (schedule.value.deleted) {
|
|
|
|
throw new Error("Unexpected deleted schedule");
|
|
|
|
}
|
|
|
|
const id = Math.max(1, ...schedule.value.events?.map(e => e.id) ?? []) + 1;
|
2025-03-15 14:20:38 +01:00
|
|
|
const change = {
|
2025-06-11 21:05:17 +02:00
|
|
|
id,
|
|
|
|
updatedAt: "",
|
|
|
|
name: newEventName.value,
|
|
|
|
description: newEventDescription.value || undefined,
|
|
|
|
crew: !newEventPublic.value || undefined,
|
|
|
|
slots: [],
|
2025-03-15 14:20:38 +01:00
|
|
|
};
|
|
|
|
changes.value = replaceChange(change, changes.value);
|
|
|
|
newEventName.value = "";
|
|
|
|
newEventDescription.value = "";
|
|
|
|
newEventPublic.value = false;
|
|
|
|
}
|
|
|
|
async function saveEvents() {
|
|
|
|
try {
|
|
|
|
await $fetch("/api/schedule", {
|
|
|
|
method: "PATCH",
|
2025-06-11 21:05:17 +02:00
|
|
|
body: {
|
|
|
|
id: 111,
|
|
|
|
updatedAt: "",
|
|
|
|
events: changes.value,
|
|
|
|
} satisfies ApiSchedule,
|
2025-03-15 14:20:38 +01:00
|
|
|
});
|
|
|
|
changes.value = [];
|
|
|
|
} catch (err: any) {
|
|
|
|
console.error(err);
|
|
|
|
alert(err?.data?.message ?? err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const events = computed(() => {
|
2025-06-11 21:05:17 +02:00
|
|
|
if (schedule.value.deleted) {
|
|
|
|
throw new Error("Unexpected deleted schedule");
|
|
|
|
}
|
|
|
|
const data = [...schedule.value.events ?? []];
|
|
|
|
applyUpdatesToArray(changes.value.filter(change => !change.deleted), data);
|
2025-03-15 14:20:38 +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>
|