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({ const getDevicesInputSchema = v.object({
search: v.optional(v.string()), search: v.optional(v.string()),
refreshAt: v.optional(v.number()),
pagination: mobilePaginationSchema, pagination: mobilePaginationSchema,
}); });

View File

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

View File

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

View File

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