homebox/frontend/pages/label/[id].vue

165 lines
4.2 KiB
Vue
Raw Permalink Normal View History

2022-09-02 17:46:20 +00:00
<script setup lang="ts">
import type { DateDetail, Detail } from "~~/components/global/DetailsSection/types";
2022-09-02 17:46:20 +00:00
definePageMeta({
layout: "home",
2022-09-02 17:46:20 +00:00
});
const route = useRoute();
const api = useUserApi();
const toast = useNotifier();
const preferences = useViewPreferences();
const labelId = computed<string>(() => route.params.id as string);
const { data: label } = useAsyncData(labelId.value, async () => {
const { data, error } = await api.labels.get(labelId.value);
if (error) {
toast.error("Failed to load label");
navigateTo("/home");
2022-09-02 17:46:20 +00:00
return;
}
return data;
});
const details = computed<(Detail | DateDetail)[]>(() => {
const details = [
{
name: "Name",
text: label.value?.name,
},
{
name: "Description",
text: label.value?.description,
},
];
2022-09-02 17:46:20 +00:00
if (preferences.value.showDetails) {
return [
...details,
{
name: "Created",
text: label.value?.createdAt,
type: "date",
},
{
name: "Updated",
text: label.value?.updatedAt,
type: "date",
},
{
name: "Database ID",
text: label.value?.id,
},
];
2022-09-02 17:46:20 +00:00
}
return details;
2022-09-02 17:46:20 +00:00
});
const confirm = useConfirm();
2022-09-02 17:46:20 +00:00
async function confirmDelete() {
const { isCanceled } = await confirm.open(
"Are you sure you want to delete this label? This action cannot be undone."
);
2022-09-02 17:46:20 +00:00
if (isCanceled) {
return;
}
const { error } = await api.labels.delete(labelId.value);
if (error) {
toast.error("Failed to delete label");
2022-09-02 17:46:20 +00:00
return;
}
toast.success("Label deleted");
navigateTo("/home");
2022-09-02 17:46:20 +00:00
}
const updateModal = ref(false);
const updating = ref(false);
const updateData = reactive({
name: "",
description: "",
color: "",
2022-09-02 17:46:20 +00:00
});
function openUpdate() {
updateData.name = label.value?.name || "";
updateData.description = label.value?.description || "";
2022-09-02 17:46:20 +00:00
updateModal.value = true;
}
async function update() {
updating.value = true;
const { error, data } = await api.labels.update(labelId.value, updateData);
if (error) {
toast.error("Failed to update label");
2022-09-02 17:46:20 +00:00
return;
}
toast.success("Label updated");
2022-09-02 17:46:20 +00:00
label.value = data;
updateModal.value = false;
updating.value = false;
}
</script>
<template>
<BaseContainer>
<BaseModal v-model="updateModal">
<template #title> Update Label </template>
<form v-if="label" @submit.prevent="update">
<FormTextField v-model="updateData.name" :autofocus="true" label="Label Name" />
<FormTextArea v-model="updateData.description" label="Label Description" />
2022-09-02 17:46:20 +00:00
<div class="modal-action">
<BaseButton type="submit" :loading="updating"> Update </BaseButton>
</div>
</form>
</BaseModal>
<BaseCard class="mb-16">
<template #title>
<BaseSectionHeader>
<Icon name="mdi-tag" class="mr-2 -mt-1 text-base-content" />
<span class="text-base-content">
{{ label ? label.name : "" }}
</span>
</BaseSectionHeader>
</template>
<template #title-actions>
<div class="flex mt-2 gap-2">
<div class="form-control max-w-[160px]">
<label class="label cursor-pointer">
<input v-model="preferences.showDetails" type="checkbox" class="toggle toggle-primary" />
<span class="label-text ml-2"> Detailed View </span>
</label>
</div>
<BaseButton class="ml-auto" size="sm" @click="openUpdate">
<Icon class="mr-1" name="mdi-pencil" />
Edit
</BaseButton>
<BaseButton size="sm" @click="confirmDelete">
<Icon class="mr-1" name="mdi-delete" />
Delete
</BaseButton>
</div>
</template>
<DetailsSection :details="details" />
</BaseCard>
2022-09-02 17:46:20 +00:00
2022-09-06 18:32:13 +00:00
<section v-if="label">
<BaseSectionHeader class="mb-5"> Items </BaseSectionHeader>
<div class="grid gap-2 grid-cols-1 sm:grid-cols-2">
<ItemCard v-for="item in label.items" :key="item.id" :item="item" />
2022-09-06 18:32:13 +00:00
</div>
</section>
2022-09-02 17:46:20 +00:00
</BaseContainer>
</template>