From aa28bd1f198b88041fb444d9a8559d7ea0df4922 Mon Sep 17 00:00:00 2001 From: Donkie Date: Sun, 15 Oct 2023 21:04:34 +0200 Subject: [PATCH] Updated client to support new message format --- client/.gitignore | 1 + client/src/components/liveProvider.ts | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/client/.gitignore b/client/.gitignore index 4d29575..056e811 100644 --- a/client/.gitignore +++ b/client/.gitignore @@ -21,3 +21,4 @@ npm-debug.log* yarn-debug.log* yarn-error.log* +.vite diff --git a/client/src/components/liveProvider.ts b/client/src/components/liveProvider.ts index 3fb8f91..4d97bc2 100644 --- a/client/src/components/liveProvider.ts +++ b/client/src/components/liveProvider.ts @@ -1,5 +1,18 @@ import { BaseKey, LiveEvent, LiveProvider } from "@refinedev/core"; +/** + * A spoolman websocket event. + */ +interface Event { + type: "updated" | "deleted" | "added"; + resource: "filament" | "spool" | "vendor"; + date: string; + payload: { + id: number; + [key: string]: unknown; + }; +} + /** * Converts an API URL to a WebSocket URL. * E.g. "https://example.com/api/v1/..." -> "ws://example.com/api/v1/..." @@ -49,14 +62,18 @@ function subscribeSingle(apiUrl: string, channel: string, resource: string, id: const ws = new WebSocket(websocketURL); ws.onmessage = (message) => { + const data: Event = JSON.parse(message.data); + const type = data.type === "added" ? "created" : data.type; + const date = new Date(data.date); + const liveEvent: LiveEvent = { channel: channel, - type: "updated", + type: type, payload: { - data: JSON.parse(message.data), ids: [id], + data: data.payload, }, - date: new Date(), + date: date, } callback(liveEvent);