owltide/components/TableScheduleShifts.vue

170 lines
3.6 KiB
Vue
Raw Normal View History

<!--
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
SPDX-License-Identifier: AGPL-3.0-or-later
-->
2025-03-15 17:06:23 +01:00
<template>
<div>
2025-03-15 17:06:23 +01:00
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>role</th>
<th>s</th>
<th>description</th>
<th v-if="edit"></th>
</tr>
</thead>
<tbody>
<template v-if="edit">
<tr
v-for="shift in schedule.shifts.values()"
2025-03-15 17:06:23 +01:00
:key="shift.id"
:class="{ removed: shift.deleted }"
2025-03-15 17:06:23 +01:00
>
<td>{{ shift.id }}</td>
<td>
<input
type="text"
v-model="shift.name"
2025-03-15 17:06:23 +01:00
>
</td>
<td>
<SelectSingleEntity
:entities="schedule.roles"
v-model="shift.roleId"
/>
2025-03-15 17:06:23 +01:00
</td>
<td>{{ shift.slots.size ? shift.slots.size : "" }}</td>
2025-03-15 17:06:23 +01:00
<td>
<input
type="text"
v-model="shift.description"
2025-03-15 17:06:23 +01:00
>
</td>
<td>
<button
type="button"
:disabled="shift.deleted"
@click="shift.deleted = true"
2025-03-15 17:06:23 +01:00
>Delete</button>
<button
v-if="shift.isModified()"
2025-03-15 17:06:23 +01:00
type="button"
@click="schedule.shifts.discardId(shift.id)"
2025-03-15 17:06:23 +01:00
>Revert</button>
</td>
</tr>
<tr>
<td>{{ schedule.nextClientId }}</td>
2025-03-15 17:06:23 +01:00
<td>
<input
type="text"
v-model="newShiftName"
>
</td>
<td>
<SelectSingleEntity
:entities="schedule.roles"
v-model="newShiftRoleId"
/>
2025-03-15 17:06:23 +01:00
</td>
<td></td>
<td>
<input
type="text"
v-model="newShiftDescription"
>
</td>
<td>
<button
v-if="shiftExists(newShiftName)"
disabled
>Shift already exists</button>
<button
v-else
type="button"
@click="newShift"
>Add Shift</button>
</td>
</tr>
</template>
<template v-else>
<tr
v-for="shift in schedule.shifts.values()"
2025-03-15 17:06:23 +01:00
:key="shift.id"
>
<td>{{ shift.id }}</td>
<td>{{ shift.name }}</td>
<td>{{ shift.roleId }}</td>
<td>{{ shift.slots.size ? shift.slots.size : "" }}</td>
2025-03-15 17:06:23 +01:00
<td>{{ shift.description }}</td>
</tr>
</template>
</tbody>
</table>
</div>
</template>
<script lang="ts" setup>
import { Info } from '~/shared/utils/luxon';
2025-03-15 17:06:23 +01:00
import { toId } from '~/shared/utils/functions';
const props = defineProps<{
edit?: boolean,
roleId?: number,
2025-03-15 17:06:23 +01:00
}>();
const schedule = await useSchedule();
const accountStore = useAccountStore();
2025-03-15 17:06:23 +01:00
const newShiftName = ref("");
const newShiftRoleId = ref(props.roleId);
watch(() => props.roleId, () => {
newShiftRoleId.value = props.roleId;
2025-03-15 17:06:23 +01:00
});
const newShiftDescription = ref("");
function shiftExists(name: string) {
name = toId(name);
2025-03-15 17:06:23 +01:00
return (
[...schedule.value.shifts.values()].some(s => !s.deleted && toId(s.name) === name)
2025-03-15 17:06:23 +01:00
);
}
function newShift() {
if (shiftExists(newShiftName.value)) {
alert(`Shift ${newShiftName.value} already exists`);
return;
}
const zone = Info.normalizeZone(accountStore.activeTimezone);
const locale = accountStore.activeLocale;
const shift = ClientScheduleShift.create(
schedule.value,
schedule.value.nextClientId--,
newShiftRoleId.value,
newShiftName.value,
newShiftDescription.value,
new Set(),
{ zone, locale },
);
schedule.value.shifts.add(shift);
2025-03-15 17:06:23 +01:00
newShiftName.value = "";
newShiftDescription.value = "";
}
</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>