-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy path[id].vue
176 lines (155 loc) · 4.84 KB
/
[id].vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<script setup lang="ts">
import MdiPackageVariant from "~icons/mdi/package-variant";
import MdiPencil from "~icons/mdi/pencil";
import MdiDelete from "~icons/mdi/delete";
definePageMeta({
middleware: ["auth"],
});
const route = useRoute();
const api = useUserApi();
const toast = useNotifier();
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");
return;
}
return data;
});
const confirm = useConfirm();
async function confirmDelete() {
const { isCanceled } = await confirm.open(
"Are you sure you want to delete this label? This action cannot be undone."
);
if (isCanceled) {
return;
}
const { error } = await api.labels.delete(labelId.value);
if (error) {
toast.error("Failed to delete label");
return;
}
toast.success("Label deleted");
navigateTo("/home");
}
const updateModal = ref(false);
const updating = ref(false);
const updateData = reactive({
name: "",
description: "",
color: "",
});
function openUpdate() {
updateData.name = label.value?.name || "";
updateData.description = label.value?.description || "";
updateModal.value = true;
}
async function update() {
updating.value = true;
const { error, data } = await api.labels.update(labelId.value, updateData);
if (error) {
updating.value = false;
toast.error("Failed to update label");
return;
}
toast.success("Label updated");
label.value = data;
updateModal.value = false;
updating.value = false;
}
const items = computedAsync(async () => {
if (!label.value) {
return {
items: [],
totalPrice: null,
};
}
const resp = await api.items.getAll({
labels: [label.value.id],
});
if (resp.error) {
toast.error("Failed to load items");
return {
items: [],
totalPrice: null,
};
}
return resp.data;
});
</script>
<template>
<BaseContainer>
<BaseModal v-model="updateModal">
<template #title> {{ $t("labels.update_label") }} </template>
<form v-if="label" @submit.prevent="update">
<FormTextField
v-model="updateData.name"
:autofocus="true"
:label="$t('components.label.create_modal.label_name')"
:max-length="255"
:min-length="1"
/>
<FormTextArea
v-model="updateData.description"
:label="$t('components.label.create_modal.label_description')"
:max-length="255"
/>
<div class="modal-action">
<BaseButton type="submit" :loading="updating"> {{ $t("global.update") }} </BaseButton>
</div>
</form>
</BaseModal>
<BaseContainer v-if="label">
<div class="rounded bg-base-100 p-3">
<header class="mb-2">
<div class="flex flex-wrap items-end gap-2">
<div class="avatar placeholder mb-auto">
<div class="w-12 rounded-full bg-neutral-focus text-neutral-content">
<MdiPackageVariant class="size-7" />
</div>
</div>
<div>
<h1 class="flex items-center gap-3 pb-1 text-2xl">
{{ label ? label.name : "" }}
<div
v-if="items && items.totalPrice"
class="rounded-full bg-secondary px-2 py-1 text-xs text-secondary-content"
>
<div>
<Currency :amount="items.totalPrice" />
</div>
</div>
</h1>
<div class="flex flex-wrap gap-1 text-xs">
<div>
Created
<DateTime :date="label?.createdAt" />
</div>
</div>
</div>
<div class="ml-auto mt-2 flex flex-wrap items-center justify-between gap-3">
<div class="btn-group">
<PageQRCode class="dropdown-left" />
<BaseButton size="sm" @click="openUpdate">
<MdiPencil class="mr-1" />
Edit
</BaseButton>
</div>
<BaseButton class="btn btn-sm" @click="confirmDelete()">
<MdiDelete class="mr-2" />
Delete
</BaseButton>
</div>
</div>
</header>
<div class="divider my-0 mb-1"></div>
<Markdown v-if="label && label.description" class="text-base" :source="label.description"> </Markdown>
</div>
<section v-if="label && items">
<ItemViewSelectable :items="items.items" />
</section>
</BaseContainer>
</BaseContainer>
</template>