Skip to content

Commit d70098d

Browse files
MaciejWasglatosinski
authored andcommitted
[#71658] frontend: redirect & show notification when device was not found
1 parent 3a8be14 commit d70098d

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

frontend/src/common/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ setInterval(() => {
296296
}, 200);
297297

298298
export function useNotifications(): ToastPluginApiExtended {
299-
const $toast = useToast() as ToastPluginApiExtended;
299+
const $toast = useToast({ duration: 8_000 }) as ToastPluginApiExtended;
300300
const buildHTML = (prefix: string, msg?: string) => {
301301
let html = `<p>${prefix}</p>`;
302302
if (msg) {

frontend/src/components/devices/Device.vue

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Component wraps functionality for displaying and working with a single rdfm devi
232232
</style>
233233

234234
<script lang="ts">
235-
import { computed, effect, ref } from 'vue';
235+
import { computed, effect, ref, watch } from 'vue';
236236
237237
import {
238238
POLL_INTERVAL,
@@ -248,7 +248,8 @@ import {
248248
execAction,
249249
type Action,
250250
} from './devices';
251-
import { useRoute } from 'vue-router';
251+
import { useRoute, useRouter } from 'vue-router';
252+
import { ActiveTab } from '@/views/HomeView.vue';
252253
253254
const MAC_ADDR_REGEX = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
254255
@@ -263,6 +264,8 @@ export default {
263264
},
264265
setup() {
265266
const route = useRoute();
267+
const router = useRouter();
268+
const notify = useNotifications();
266269
const interval = ref<number | null>(null);
267270
268271
groupResources.fetchResources();
@@ -281,40 +284,52 @@ export default {
281284
const device = ref<RegisteredDevice>();
282285
const groups = ref<Group[]>();
283286
const pattern = ref<string>();
284-
effect(() => {
285-
if (!registeredDevicesResources.resources.value) return;
286-
287-
let foundDevice: RegisteredDevice | undefined;
288-
let foundPattern;
289-
290-
if (routeId.match(MAC_ADDR_REGEX)) {
291-
foundDevice = registeredDevicesResources.resources.value.find(
292-
(d) => d.mac_address == routeId,
293-
);
294-
foundPattern = 'MAC address';
295-
} else {
296-
foundDevice = registeredDevicesResources.resources.value.find(
297-
(d) => d.name == decodeURIComponent(routeId),
298-
);
299-
foundPattern = 'name';
300-
}
287+
const stop = watch(
288+
() => registeredDevicesResources.resources.value,
289+
async () => {
290+
if (!registeredDevicesResources.resources.value) return;
291+
if (device.value) return;
292+
293+
let foundDevice: RegisteredDevice | undefined;
294+
let foundPattern;
295+
296+
if (routeId.match(MAC_ADDR_REGEX)) {
297+
foundDevice = registeredDevicesResources.resources.value.find(
298+
(d) => d.mac_address == routeId,
299+
);
300+
foundPattern = 'MAC address';
301+
} else {
302+
foundDevice = registeredDevicesResources.resources.value.find(
303+
(d) => d.name == decodeURIComponent(routeId),
304+
);
305+
foundPattern = 'name';
306+
}
301307
302-
if (!foundDevice) {
303-
foundDevice = registeredDevicesResources.resources.value.find(
304-
(d) => d.id == Number(routeId),
305-
);
306-
foundPattern = 'id';
307-
}
308+
if (!foundDevice && /^\d+$/g.test(routeId)) {
309+
foundDevice = registeredDevicesResources.resources.value.find(
310+
(d) => d.id == Number(routeId),
311+
);
312+
foundPattern = 'id';
313+
}
308314
309-
device.value = foundDevice;
310-
groups.value = foundDevice?.groups
311-
?.map(
312-
(deviceGroupId) =>
313-
groupResources.resources.value?.find((g) => g.id == deviceGroupId)!,
314-
)
315-
.filter(Boolean);
316-
pattern.value = foundPattern;
317-
});
315+
if (foundDevice) {
316+
device.value = foundDevice;
317+
groups.value = foundDevice?.groups
318+
?.map(
319+
(deviceGroupId) =>
320+
groupResources.resources.value?.find((g) => g.id == deviceGroupId)!,
321+
)
322+
.filter(Boolean);
323+
pattern.value = foundPattern;
324+
} else {
325+
await router.push({ name: ActiveTab.Devices.toString() });
326+
notify.notifyError({
327+
headline: `Device with ${foundPattern} '${routeId}' not found.`,
328+
});
329+
stop();
330+
}
331+
},
332+
);
318333
319334
const devicesLoaded = computed(() => !!registeredDevicesResources.resources.value);
320335

0 commit comments

Comments
 (0)