@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,395 @@
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 && canCreateBuilding"
12
+ >
13
+ Add Building
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="getBuildings()">
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="getBuildings()"
41
+ />
42
+ </v-row>
43
+ </template>
44
+ </v-toolbar>
45
+
46
+ <v-data-table
47
+ :headers="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.createdAt="{ value }">
58
+ {{ new Date(value).toLocaleDateString() }}
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
+ <BuildingForm
67
+ :site="site"
68
+ @cancel="createDialog = false"
69
+ @success="successCreate()"
70
+ @success:create-more="getBuildings()"
71
+ />
72
+ </v-dialog>
73
+
74
+ <!-- Edit Dialog -->
75
+ <v-dialog v-model="editDialog" width="450" persistent>
76
+ <BuildingForm
77
+ :site="site"
78
+ mode="edit"
79
+ @cancel="editDialog = false"
80
+ @success="successUpdate()"
81
+ :building="selectedBuilding"
82
+ />
83
+ </v-dialog>
84
+
85
+ <!-- Preview Dialog -->
86
+ <v-dialog v-if="canViewBuildingDetails" v-model="previewDialog" width="450" persistent>
87
+ <v-card width="100%">
88
+ <v-card-text style="max-height: 100vh; overflow-y: auto" class="pb-0">
89
+ <v-row no-gutters class="mb-4">
90
+ <v-col cols="12">
91
+ <strong>Name:</strong> {{ selectedBuilding?.name ?? "N/A" }}
92
+ </v-col>
93
+
94
+ <v-col cols="12">
95
+ <strong>Block:</strong>
96
+ {{ selectedBuilding?.block ?? "N/A" }}
97
+ </v-col>
98
+
99
+ <v-col cols="12">
100
+ <strong>Levels:</strong>
101
+ {{ selectedBuilding?.levels.length ?? "N/A" }}
102
+ </v-col>
103
+ </v-row>
104
+ </v-card-text>
105
+
106
+ <v-toolbar class="pa-0" density="compact">
107
+ <v-row no-gutters>
108
+ <v-col cols="6" class="pa-0">
109
+ <v-btn
110
+ block
111
+ variant="text"
112
+ class="text-none"
113
+ size="large"
114
+ @click="previewDialog = false"
115
+ height="48"
116
+ >
117
+ Close
118
+ </v-btn>
119
+ </v-col>
120
+
121
+ <v-col cols="6" class="pa-0" v-if="canUpdate">
122
+ <v-menu>
123
+ <template #activator="{ props }">
124
+ <v-btn
125
+ block
126
+ variant="flat"
127
+ color="black"
128
+ class="text-none"
129
+ height="48"
130
+ v-bind="props"
131
+ tile
132
+ :disabled="!canUpdateBuilding && !canDeleteBuilding"
133
+ >
134
+ More actions
135
+ </v-btn>
136
+ </template>
137
+
138
+ <v-list class="pa-0">
139
+ <v-list-item v-if="canUpdateBuilding" @click="openEditDialog()">
140
+ <v-list-item-title class="text-subtitle-2">
141
+ Edit Building
142
+ </v-list-item-title>
143
+ </v-list-item>
144
+
145
+ <v-list-item v-if="canDeleteBuilding" @click="openDeleteDialog()" class="text-red">
146
+ <v-list-item-title class="text-subtitle-2">
147
+ Delete Building
148
+ </v-list-item-title>
149
+ </v-list-item>
150
+ </v-list>
151
+ </v-menu>
152
+ </v-col>
153
+ </v-row>
154
+ </v-toolbar>
155
+ </v-card>
156
+ </v-dialog>
157
+
158
+ <v-dialog
159
+ v-model="confirmDialog"
160
+ :loading="deleteLoading"
161
+ width="450"
162
+ persistent
163
+ >
164
+ <v-card width="100%">
165
+ <v-toolbar density="compact" class="pl-4">
166
+ <span class="font-weight-medium text-h5">Delete Building</span>
167
+ </v-toolbar>
168
+
169
+ <v-card-text>
170
+ <p class="text-subtitle-2 text-center">
171
+ Are you sure you want to delete this building? This action cannot be
172
+ undone.
173
+ </p>
174
+
175
+ <v-row v-if="message" no-gutters justify="center" class="mt-4">
176
+ <span class="text-caption text-error text-center">
177
+ {{ message }}
178
+ </span>
179
+ </v-row>
180
+ </v-card-text>
181
+
182
+ <v-toolbar density="compact">
183
+ <v-row no-gutters>
184
+ <v-col cols="6">
185
+ <v-btn
186
+ tile
187
+ block
188
+ size="48"
189
+ variant="text"
190
+ class="text-none"
191
+ @click="confirmDialog = false"
192
+ :disabled="deleteLoading"
193
+ >
194
+ Close
195
+ </v-btn>
196
+ </v-col>
197
+ <v-col cols="6">
198
+ <v-btn
199
+ tile
200
+ block
201
+ size="48"
202
+ color="black"
203
+ variant="flat"
204
+ class="text-none"
205
+ @click="handleDeleteBuilding"
206
+ :loading="deleteLoading"
207
+ >
208
+ Delete Building
209
+ </v-btn>
210
+ </v-col>
211
+ </v-row>
212
+ </v-toolbar>
213
+ </v-card>
214
+ </v-dialog>
215
+
216
+ <Snackbar v-model="messageSnackbar" :text="message" :color="messageColor" />
217
+ </v-row>
218
+ </template>
219
+
220
+ <script setup lang="ts">
221
+ definePageMeta({
222
+ middleware: ["01-auth", "02-org"],
223
+ memberOnly: true,
224
+ });
225
+
226
+ const props = defineProps({
227
+ headers: {
228
+ type: Array as PropType<Array<Record<string, any>>>,
229
+ default: () => [
230
+ {
231
+ title: "Name",
232
+ value: "name",
233
+ },
234
+ {
235
+ title: "Director",
236
+ value: "directorName",
237
+ },
238
+ { title: "Action", value: "action-table" },
239
+ ],
240
+ },
241
+ canCreate: {
242
+ type: Boolean,
243
+ default: true,
244
+ },
245
+ canUpdate: {
246
+ type: Boolean,
247
+ default: true,
248
+ },
249
+ canDelete: {
250
+ type: Boolean,
251
+ default: true,
252
+ },
253
+ canCreateBuilding: {
254
+ type: Boolean,
255
+ default: true,
256
+ },
257
+ canUpdateBuilding: {
258
+ type: Boolean,
259
+ default: true,
260
+ },
261
+ canDeleteBuilding: {
262
+ type: Boolean,
263
+ default: true,
264
+ },
265
+ canViewBuildingDetails: {
266
+ type: Boolean,
267
+ default: true,
268
+ },
269
+ });
270
+
271
+ const site = (useRoute().params.site as string) ?? "";
272
+ const status = (useRoute().params.status as string) ?? "active";
273
+
274
+ const { headerSearch } = useLocal();
275
+ const { getAll: _getBuildings, deleteById } = useBuilding();
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: getBuildings,
290
+ status: getBuildingReqStatus,
291
+ } = useLazyAsyncData(
292
+ "buildings-get-all",
293
+ () =>
294
+ _getBuildings({
295
+ page: page.value,
296
+ search: headerSearch.value,
297
+ status,
298
+ 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
+ const createDialog = ref(false);
316
+ const editDialog = ref(false);
317
+ const previewDialog = ref(false);
318
+ const selectedBuilding = ref<TBuilding>({
319
+ _id: "",
320
+ site: "",
321
+ name: "",
322
+ block: null,
323
+ levels: [],
324
+ buildingFloorPlan: [],
325
+ });
326
+
327
+ function tableRowClickHandler(_: any, data: any) {
328
+ selectedBuilding.value = data.item as TBuilding;
329
+ previewDialog.value = true;
330
+ }
331
+
332
+ function successCreate() {
333
+ createDialog.value = false;
334
+ getBuildings();
335
+ showMessage("Building created successfully!", "success");
336
+ }
337
+
338
+ function successUpdate() {
339
+ editDialog.value = false;
340
+ previewDialog.value = false;
341
+ getBuildings();
342
+ showMessage("Building updated successfully!", "success");
343
+ }
344
+
345
+ function openEditDialog() {
346
+ editDialog.value = true;
347
+ }
348
+
349
+ const confirmDialog = ref(false);
350
+ const selectedBuildingId = ref<string | null>(null);
351
+ const deleteLoading = ref(false);
352
+
353
+ function openDeleteDialog() {
354
+ confirmDialog.value = true;
355
+ message.value = "";
356
+ }
357
+
358
+ function showMessage(msg: string, color: string) {
359
+ message.value = msg;
360
+ messageColor.value = color;
361
+ messageSnackbar.value = true;
362
+ }
363
+
364
+ async function handleDeleteBuilding() {
365
+ deleteLoading.value = true;
366
+ try {
367
+ await deleteById(selectedBuilding.value._id ?? "");
368
+ await getBuildings();
369
+ selectedBuildingId.value = null;
370
+ confirmDialog.value = false;
371
+ previewDialog.value = false
372
+ } catch (error: any) {
373
+ message.value =
374
+ error?.response?._data?.message || "Failed to delete region";
375
+ } finally {
376
+ deleteLoading.value = false;
377
+ }
378
+ }
379
+
380
+ function setBuilding({
381
+ mode = "create",
382
+ dialog = true,
383
+ data = {} as TBuilding,
384
+ } = {}) {
385
+ if (mode === "create") {
386
+ createDialog.value = dialog;
387
+ } else if (mode === "edit") {
388
+ editDialog.value = dialog;
389
+ selectedBuilding.value = data;
390
+ } else if (mode === "preview") {
391
+ previewDialog.value = dialog;
392
+ selectedBuilding.value = data;
393
+ }
394
+ }
395
+ </script>