@iservice365/layer-common 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,391 @@
1
+ <template>
2
+ <v-row no-gutters>
3
+ <v-col cols="12" class="mb-2">
4
+ <v-row no-gutters>
5
+ <v-btn
6
+ class="text-none"
7
+ rounded="pill"
8
+ variant="tonal"
9
+ @click="setBuilding()"
10
+ size="large"
11
+ v-if="canCreate"
12
+ >
13
+ Add Unit
14
+ </v-btn>
15
+ </v-row>
16
+ </v-col>
17
+ <v-col cols="12">
18
+ <v-card
19
+ width="100%"
20
+ variant="outlined"
21
+ border="thin"
22
+ rounded="lg"
23
+ :loading="loading"
24
+ >
25
+ <v-toolbar density="compact" color="grey-lighten-4">
26
+ <template #prepend>
27
+ <v-btn fab icon density="comfortable" @click="getBuildingUnit()">
28
+ <v-icon>mdi-refresh</v-icon>
29
+ </v-btn>
30
+ </template>
31
+
32
+ <template #append>
33
+ <v-row no-gutters justify="end" align="center">
34
+ <span class="mr-2 text-caption text-fontgray">
35
+ {{ pageRange }}
36
+ </span>
37
+ <local-pagination
38
+ v-model="page"
39
+ :length="pages"
40
+ @update:value="getBuildingUnit()"
41
+ />
42
+ </v-row>
43
+ </template>
44
+ </v-toolbar>
45
+
46
+ <v-data-table
47
+ :headers="props.headers"
48
+ :items="items"
49
+ item-value="_id"
50
+ items-per-page="20"
51
+ fixed-header
52
+ hide-default-footer
53
+ hide-default-header
54
+ @click:row="tableRowClickHandler"
55
+ style="max-height: calc(100vh - (200px))"
56
+ >
57
+ <template #item.block="{ value }">
58
+ {{ value ? `blk ${value}` : "" }}
59
+ </template>
60
+ </v-data-table>
61
+ </v-card>
62
+ </v-col>
63
+
64
+ <!-- Create Dialog -->
65
+ <v-dialog v-model="createDialog" width="450" persistent>
66
+ <BuildingUnitFormAdd
67
+ :site="site"
68
+ @cancel="createDialog = false"
69
+ @success="successCreate()"
70
+ @success:create-more="getBuildingUnit()"
71
+ />
72
+ </v-dialog>
73
+
74
+ <!-- Edit Dialog -->
75
+ <v-dialog v-model="dialogEdit" width="450" persistent>
76
+ <BuildingUnitFormEdit
77
+ v-if="selectedBuildingUnit"
78
+ @cancel="dialogEdit = false"
79
+ @success="successUpdate()"
80
+ :roomFacility="selectedBuildingUnit"
81
+ />
82
+ </v-dialog>
83
+
84
+ <!-- Preview Dialog -->
85
+ <v-dialog v-model="dialogPreview" width="450" persistent>
86
+ <v-card width="100%">
87
+ <v-card-text style="max-height: 100vh; overflow-y: auto" class="pb-0">
88
+ <v-row no-gutters v-if="selectedBuildingUnit" class="mb-4">
89
+ <v-col cols="12">
90
+ <strong>Name:</strong> {{ selectedBuildingUnit.name }}
91
+ </v-col>
92
+
93
+ <v-col cols="12">
94
+ <strong>Block:</strong>
95
+ {{ selectedBuildingUnit.block }}
96
+ </v-col>
97
+
98
+ <v-col cols="12">
99
+ <strong>Floors:</strong>
100
+ {{ selectedBuildingUnit.level }}
101
+ </v-col>
102
+
103
+ <v-col cols="12">
104
+ <strong>Category:</strong>
105
+ {{ selectedBuildingUnit.category }}
106
+ </v-col>
107
+ </v-row>
108
+ </v-card-text>
109
+
110
+ <v-toolbar class="pa-0" density="compact">
111
+ <v-row no-gutters>
112
+ <v-col cols="6" class="pa-0">
113
+ <v-btn
114
+ block
115
+ variant="text"
116
+ class="text-none"
117
+ size="large"
118
+ @click="dialogPreview = false"
119
+ height="48"
120
+ >
121
+ Close
122
+ </v-btn>
123
+ </v-col>
124
+
125
+ <v-col cols="6" class="pa-0" v-if="canUpdate">
126
+ <v-menu>
127
+ <template #activator="{ props }">
128
+ <v-btn
129
+ block
130
+ variant="flat"
131
+ color="black"
132
+ class="text-none"
133
+ height="48"
134
+ v-bind="props"
135
+ tile
136
+ >
137
+ More actions
138
+ </v-btn>
139
+ </template>
140
+
141
+ <v-list class="pa-0">
142
+ <v-list-item @click="openEditDialog()">
143
+ <v-list-item-title class="text-subtitle-2">
144
+ Edit Unit
145
+ </v-list-item-title>
146
+ </v-list-item>
147
+
148
+ <v-list-item @click="openDeleteDialog()" class="text-red">
149
+ <v-list-item-title class="text-subtitle-2">
150
+ Delete Unit
151
+ </v-list-item-title>
152
+ </v-list-item>
153
+ </v-list>
154
+ </v-menu>
155
+ </v-col>
156
+ </v-row>
157
+ </v-toolbar>
158
+ </v-card>
159
+ </v-dialog>
160
+
161
+ <v-dialog
162
+ v-model="dialogDelete"
163
+ :loading="deleteLoading"
164
+ width="450"
165
+ persistent
166
+ >
167
+ <v-card width="100%">
168
+ <v-toolbar density="compact" class="pl-4">
169
+ <span class="font-weight-medium text-h5">Delete Unit</span>
170
+ </v-toolbar>
171
+
172
+ <v-card-text>
173
+ <p class="text-subtitle-2 text-center">
174
+ Are you sure you want to delete this unit? This action cannot be
175
+ undone.
176
+ </p>
177
+
178
+ <v-row v-if="message" no-gutters justify="center" class="mt-4">
179
+ <span class="text-caption text-error text-center">
180
+ {{ message }}
181
+ </span>
182
+ </v-row>
183
+ </v-card-text>
184
+
185
+ <v-toolbar density="compact">
186
+ <v-row no-gutters>
187
+ <v-col cols="6">
188
+ <v-btn
189
+ tile
190
+ block
191
+ size="48"
192
+ variant="text"
193
+ class="text-none"
194
+ @click="dialogDelete = false"
195
+ :disabled="deleteLoading"
196
+ >
197
+ Close
198
+ </v-btn>
199
+ </v-col>
200
+ <v-col cols="6">
201
+ <v-btn
202
+ tile
203
+ block
204
+ size="48"
205
+ color="black"
206
+ variant="flat"
207
+ class="text-none"
208
+ @click="handleDeleteBuildingUnit"
209
+ :loading="deleteLoading"
210
+ >
211
+ Delete Unit
212
+ </v-btn>
213
+ </v-col>
214
+ </v-row>
215
+ </v-toolbar>
216
+ </v-card>
217
+ </v-dialog>
218
+
219
+ <Snackbar v-model="messageSnackbar" :text="message" :color="messageColor" />
220
+ </v-row>
221
+ </template>
222
+
223
+ <script setup lang="ts">
224
+ const props = defineProps({
225
+ headers: {
226
+ type: Array as PropType<Array<Record<string, any>>>,
227
+ default: () => [
228
+ {
229
+ title: "Name",
230
+ value: "name",
231
+ },
232
+ {
233
+ title: "Building",
234
+ value: "buildingName",
235
+ },
236
+ {
237
+ title: "Block",
238
+ value: "block",
239
+ },
240
+ {
241
+ title: "Floor",
242
+ value: "level",
243
+ },
244
+ {
245
+ title: "Category",
246
+ value: "category",
247
+ },
248
+ {
249
+ title: "Type",
250
+ value: "type",
251
+ },
252
+ { title: "Action", value: "action-table" },
253
+ ],
254
+ },
255
+ canCreate: {
256
+ type: Boolean,
257
+ default: true,
258
+ },
259
+ canUpdate: {
260
+ type: Boolean,
261
+ default: true,
262
+ },
263
+ canDelete: {
264
+ type: Boolean,
265
+ default: true,
266
+ },
267
+ });
268
+
269
+ const site = (useRoute().params.site as string) ?? "";
270
+ const status = (useRoute().params.status as string) ?? "active";
271
+
272
+ const { toOrdinal } = useUtils();
273
+
274
+ const { headerSearch } = useLocal();
275
+ const { getAll, deleteById } = useBuildingUnit();
276
+
277
+ const page = ref(1);
278
+ const pages = ref(0);
279
+ const pageRange = ref("-- - -- of --");
280
+
281
+ const message = ref("");
282
+ const messageSnackbar = ref(false);
283
+ const messageColor = ref("");
284
+
285
+ const items = ref<Array<Record<string, any>>>([]);
286
+
287
+ const {
288
+ data: getBuildingReq,
289
+ refresh: getBuildingUnit,
290
+ status: getBuildingReqStatus,
291
+ } = useLazyAsyncData(
292
+ "get-all-building-units",
293
+ () =>
294
+ getAll({
295
+ page: page.value,
296
+ search: headerSearch.value,
297
+ status,
298
+ site: site,
299
+ }),
300
+ {
301
+ watch: [page, headerSearch],
302
+ }
303
+ );
304
+
305
+ const loading = computed(() => getBuildingReqStatus.value === "pending");
306
+
307
+ watchEffect(() => {
308
+ if (getBuildingReq.value) {
309
+ items.value = getBuildingReq.value.items;
310
+ pages.value = getBuildingReq.value.pages;
311
+ pageRange.value = getBuildingReq.value.pageRange;
312
+ }
313
+ });
314
+
315
+ function tableRowClickHandler(_: any, data: any) {
316
+ selectedBuildingUnit.value = data.item;
317
+ dialogPreview.value = true;
318
+ }
319
+
320
+ const createDialog = ref(false);
321
+ const dialogEdit = ref(false);
322
+ const dialogPreview = ref(false);
323
+ const selectedBuildingUnit = ref<TBuildingUnit | null>(null);
324
+
325
+ function successCreate() {
326
+ createDialog.value = false;
327
+ getBuildingUnit();
328
+ showMessage("Unit created successfully!", "success");
329
+ }
330
+
331
+ function successUpdate() {
332
+ dialogEdit.value = false;
333
+ dialogPreview.value = false;
334
+ getBuildingUnit();
335
+ showMessage("Unit updated successfully!", "success");
336
+ }
337
+
338
+ function openEditDialog() {
339
+ dialogEdit.value = true;
340
+ if (dialogPreview.value) {
341
+ dialogPreview.value = false;
342
+ }
343
+ }
344
+
345
+ const dialogDelete = ref(false);
346
+ const deleteLoading = ref(false);
347
+
348
+ function openDeleteDialog() {
349
+ dialogDelete.value = true;
350
+ message.value = "";
351
+ if (dialogPreview.value) {
352
+ dialogPreview.value = false;
353
+ }
354
+ }
355
+
356
+ function showMessage(msg: string, color: string) {
357
+ message.value = msg;
358
+ messageColor.value = color;
359
+ messageSnackbar.value = true;
360
+ }
361
+
362
+ async function handleDeleteBuildingUnit() {
363
+ deleteLoading.value = true;
364
+ try {
365
+ await deleteById(selectedBuildingUnit.value?._id ?? "");
366
+ await getBuildingUnit();
367
+ dialogDelete.value = false;
368
+ selectedBuildingUnit.value = null;
369
+ } catch (error: any) {
370
+ message.value = error?.response?._data?.message || "Failed to delete unit";
371
+ } finally {
372
+ deleteLoading.value = false;
373
+ }
374
+ }
375
+
376
+ function setBuilding({
377
+ mode = "create",
378
+ dialog = true,
379
+ data = {} as TBuildingUnit,
380
+ } = {}) {
381
+ if (mode === "create") {
382
+ createDialog.value = dialog;
383
+ } else if (mode === "edit") {
384
+ dialogEdit.value = dialog;
385
+ selectedBuildingUnit.value = data;
386
+ } else if (mode === "preview") {
387
+ dialogPreview.value = dialog;
388
+ selectedBuildingUnit.value = data;
389
+ }
390
+ }
391
+ </script>