Skip to content

Commit 612d790

Browse files
luizhf42otavio
authored andcommitted
fix(ui): use DeviceActionButton in DevicesDropdown
1 parent 40659ec commit 612d790

File tree

4 files changed

+97
-150
lines changed

4 files changed

+97
-150
lines changed

ui/src/components/AppBar/DevicesDropdown.vue

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
flat
2222
data-test="devices-card"
2323
>
24-
<v-card-title class="text-h6 py-3">
25-
Device Management
26-
</v-card-title>
24+
<v-card-title class="text-h6 py-3">Device Management</v-card-title>
2725
<v-card-text class="pa-4 pt-0">
2826
<v-row
2927
dense
@@ -41,9 +39,7 @@
4139
<div class="text-h4 font-weight-bold">
4240
{{ stats.registered_devices }}
4341
</div>
44-
<div class="text-caption text-medium-emphasis">
45-
Total
46-
</div>
42+
<div class="text-caption text-medium-emphasis">Total</div>
4743
</v-card>
4844
</v-col>
4945
<v-col
@@ -58,9 +54,7 @@
5854
<div class="text-h4 font-weight-bold">
5955
{{ stats.online_devices }}
6056
</div>
61-
<div class="text-caption text-medium-emphasis">
62-
Online
63-
</div>
57+
<div class="text-caption text-medium-emphasis">Online</div>
6458
</v-card>
6559
</v-col>
6660
<v-col
@@ -75,9 +69,7 @@
7569
<div class="text-h4 font-weight-bold">
7670
{{ stats.pending_devices }}
7771
</div>
78-
<div class="text-caption text-medium-emphasis">
79-
Pending
80-
</div>
72+
<div class="text-caption text-medium-emphasis">Pending</div>
8173
</v-card>
8274
</v-col>
8375
<v-col
@@ -92,9 +84,7 @@
9284
<div class="text-h4 font-weight-bold">
9385
{{ offlineDevices }}
9486
</div>
95-
<div class="text-caption text-medium-emphasis">
96-
Offline
97-
</div>
87+
<div class="text-caption text-medium-emphasis">Offline</div>
9888
</v-card>
9989
</v-col>
10090
</v-row>
@@ -188,26 +178,28 @@
188178
</span>
189179
</template>
190180
<div class="d-flex align-center ga-2 mt-1">
191-
<v-btn
181+
<DeviceActionButton
182+
:uid="device.uid"
183+
:name="device.name"
184+
action="accept"
185+
variant="device"
186+
is-in-devices-dropdown
192187
color="success"
193-
variant="flat"
194-
size="small"
195188
prepend-icon="mdi-check-circle"
196189
:data-test="`accept-${device.uid}`"
197-
@click="handleAccept(device.uid)"
198-
>
199-
Accept
200-
</v-btn>
201-
<v-btn
190+
@update="handleUpdate"
191+
/>
192+
<DeviceActionButton
193+
:uid="device.uid"
194+
:name="device.name"
195+
action="reject"
196+
variant="device"
197+
is-in-devices-dropdown
202198
color="error"
203-
variant="tonal"
204-
size="small"
205199
prepend-icon="mdi-cancel"
206200
:data-test="`reject-${device.uid}`"
207-
@click="handleReject(device.uid)"
208-
>
209-
Reject
210-
</v-btn>
201+
@update="handleUpdate"
202+
/>
211203
<v-btn
212204
icon="mdi-dots-vertical"
213205
variant="text"
@@ -273,7 +265,7 @@
273265
</v-list-item-subtitle>
274266
<template #append>
275267
<span class="text-caption text-medium-emphasis">
276-
{{ device.online ? 'Active now' : formatTimeAgo(device.last_seen) }}
268+
{{ device.online ? "Active now" : formatTimeAgo(device.last_seen) }}
277269
</span>
278270
</template>
279271
</v-list-item>
@@ -325,6 +317,7 @@ import handleError from "@/utils/handleError";
325317
import useSnackbar from "@/helpers/snackbar";
326318
import moment from "moment";
327319
import { IDevice } from "@/interfaces/IDevice";
320+
import DeviceActionButton from "@/components/Devices/DeviceActionButton.vue";
328321
329322
const { smAndUp, thresholds } = useDisplay();
330323
const statsStore = useStatsStore();
@@ -336,36 +329,22 @@ const activeTab = ref<"pending" | "recent">("pending");
336329
const pendingDevicesList = ref<IDevice[]>([]);
337330
const recentDevicesList = ref<IDevice[]>([]);
338331
const stats = computed(() => statsStore.stats);
339-
const offlineDevices = computed(() => stats.value.registered_devices - stats.value.online_devices);
340-
const toggleDrawer = () => { isDrawerOpen.value = !isDrawerOpen.value; };
332+
const offlineDevices = computed(
333+
() => stats.value.registered_devices - stats.value.online_devices,
334+
);
335+
const toggleDrawer = () => {
336+
isDrawerOpen.value = !isDrawerOpen.value;
337+
};
341338
342339
const formatTimeAgo = (date: string | Date) => {
343340
if (!date) return "Unknown";
344341
return moment(date).fromNow();
345342
};
346343
347-
const handleAccept = async (uid: string) => {
348-
try {
349-
await devicesStore.acceptDevice(uid);
350-
await fetchStats();
351-
await fetchPendingDevices();
352-
snackbar.showSuccess("Device accepted successfully");
353-
} catch (error: unknown) {
354-
snackbar.showError("Failed to accept device");
355-
handleError(error);
356-
}
357-
};
358-
359-
const handleReject = async (uid: string) => {
360-
try {
361-
await devicesStore.rejectDevice(uid);
362-
await fetchStats();
363-
await fetchPendingDevices();
364-
snackbar.showSuccess("Device rejected successfully");
365-
} catch (error: unknown) {
366-
snackbar.showError("Failed to reject device");
367-
handleError(error);
368-
}
344+
const handleUpdate = async () => {
345+
await fetchStats();
346+
await fetchPendingDevices();
347+
await fetchRecentDevices();
369348
};
370349
371350
const fetchStats = async () => {
@@ -389,8 +368,10 @@ const fetchPendingDevices = async () => {
389368
const fetchRecentDevices = async () => {
390369
try {
391370
await devicesStore.fetchDeviceList({ status: "accepted" });
392-
recentDevicesList.value = [...devicesStore.devices]
393-
.sort((a, b) => new Date(b.last_seen).getTime() - new Date(a.last_seen).getTime());
371+
recentDevicesList.value = [...devicesStore.devices].sort(
372+
(a, b) =>
373+
new Date(b.last_seen).getTime() - new Date(a.last_seen).getTime(),
374+
);
394375
} catch (error: unknown) {
395376
handleError(error);
396377
}
@@ -406,8 +387,7 @@ defineExpose({
406387
toggleDrawer,
407388
formatTimeAgo,
408389
isDrawerOpen,
409-
handleAccept,
410-
handleReject,
390+
handleUpdate,
411391
activeTab,
412392
pendingDevicesList,
413393
recentDevicesList,

ui/src/components/Devices/DeviceActionButton.vue

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<template>
22
<div>
33
<v-btn
4-
v-if="isInNotification"
4+
v-if="isInDevicesDropdown"
55
v-bind="$attrs"
6-
size="x-small"
7-
color="primary"
8-
data-test="notification-action-button"
6+
size="small"
7+
variant="flat"
8+
:text="capitalizeText(action)"
99
@click="showDialog = true"
10-
>
11-
<v-icon>{{ icon }}</v-icon>
12-
Accept
13-
</v-btn>
10+
/>
1411
<v-list-item
1512
v-else
1613
data-test="list-item"
@@ -87,14 +84,14 @@ import useStatsStore from "@/store/modules/stats";
8784
interface DeviceActionButtonProps {
8885
name?: string;
8986
uid: string;
90-
isInNotification?: boolean;
87+
isInDevicesDropdown?: boolean;
9188
action?: "accept" | "reject" | "remove";
9289
variant: string;
9390
}
9491
9592
const props = withDefaults(defineProps<DeviceActionButtonProps>(), {
9693
name: "Device",
97-
isInNotification: false,
94+
isInDevicesDropdown: false,
9895
action: "accept",
9996
});
10097
@@ -192,7 +189,7 @@ const handleClick = async () => {
192189
}
193190
};
194191
195-
defineExpose({ showDialog, canPerformDeviceAction });
192+
defineExpose({ showDialog, canPerformDeviceAction, handleClick });
196193
</script>
197194

198195
<style scoped>

0 commit comments

Comments
 (0)