fix in ui for a refresh

This commit is contained in:
user
2026-03-01 09:26:26 +02:00
parent 17039aa3dd
commit 6c2b917088
4 changed files with 40 additions and 10 deletions

View File

@@ -15,6 +15,7 @@ const mc = getMobileController();
const getDevicesInputSchema = v.object({
search: v.optional(v.string()),
refreshAt: v.optional(v.number()),
pagination: mobilePaginationSchema,
});

View File

@@ -37,12 +37,24 @@ class MobileViewModel {
private devicesPollTimer: ReturnType<typeof setInterval> | null = null;
private smsPollTimer: ReturnType<typeof setInterval> | null = null;
private devicesRequestVersion = 0;
async fetchDevices({
showLoading = true,
forceRefresh = false,
}: {
showLoading?: boolean;
forceRefresh?: boolean;
} = {}) {
const requestVersion = ++this.devicesRequestVersion;
if (showLoading) {
this.devicesLoading = true;
}
async fetchDevices() {
this.devicesLoading = true;
try {
const result = await getDevicesSQ({
search: this.devicesSearch || undefined,
refreshAt: forceRefresh ? Date.now() : undefined,
pagination: {
page: this.devicesPage,
pageSize: this.devicesPageSize,
@@ -51,6 +63,10 @@ class MobileViewModel {
},
});
if (requestVersion !== this.devicesRequestVersion) {
return;
}
if (result?.error || !result?.data) {
toast.error(result?.error?.message || "Failed to load devices", {
description: result?.error?.description || "Please try again later",
@@ -58,18 +74,28 @@ class MobileViewModel {
return;
}
this.devices = result.data.data as MobileDevice[];
this.devices = [...(result.data.data as MobileDevice[])];
this.devicesTotal = result.data.total;
} catch (error) {
if (requestVersion !== this.devicesRequestVersion) {
return;
}
toast.error("Failed to load devices", {
description:
error instanceof Error ? error.message : "Please try again later",
});
} finally {
this.devicesLoading = false;
if (showLoading && requestVersion === this.devicesRequestVersion) {
this.devicesLoading = false;
}
}
}
async refreshDevices() {
await this.fetchDevices({ showLoading: true, forceRefresh: true });
}
async fetchDeviceDetail(deviceId: number) {
this.deviceDetailLoading = true;
try {
@@ -154,7 +180,10 @@ class MobileViewModel {
startDevicesPolling(intervalMs = 5000) {
this.stopDevicesPolling();
this.devicesPollTimer = setInterval(() => {
this.fetchDevices();
void this.fetchDevices({
showLoading: false,
forceRefresh: true,
});
}, intervalMs);
}

View File

@@ -17,7 +17,7 @@
breadcrumbs.set([mainNavTree[0]]);
onMount(async () => {
await mobileVM.fetchDevices();
await mobileVM.refreshDevices();
mobileVM.startDevicesPolling(5000);
});
@@ -40,7 +40,7 @@
<Button
variant="outline"
size="sm"
onclick={() => mobileVM.fetchDevices()}
onclick={() => void mobileVM.refreshDevices()}
disabled={mobileVM.devicesLoading}
>
<Icon
@@ -62,7 +62,7 @@
bind:value={mobileVM.devicesSearch}
oninput={() => {
mobileVM.devicesPage = 1;
mobileVM.fetchDevices();
void mobileVM.refreshDevices();
}}
/>
</div>

View File

@@ -64,8 +64,8 @@
</div>
<div class="grid place-items-center p-8 py-32">
<span class="text-xl font-semibold"
>INFO: Normally would show all the users table here
<span class="text-xl font-semibold">
Not Active At The Moment
</span>
</div>
</Card.Content>