Updated client to support new message format

This commit is contained in:
Donkie
2023-10-15 21:04:34 +02:00
parent 5f7a063125
commit aa28bd1f19
2 changed files with 21 additions and 3 deletions

1
client/.gitignore vendored
View File

@@ -21,3 +21,4 @@
npm-debug.log* npm-debug.log*
yarn-debug.log* yarn-debug.log*
yarn-error.log* yarn-error.log*
.vite

View File

@@ -1,5 +1,18 @@
import { BaseKey, LiveEvent, LiveProvider } from "@refinedev/core"; 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. * Converts an API URL to a WebSocket URL.
* E.g. "https://example.com/api/v1/..." -> "ws://example.com/api/v1/..." * 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); const ws = new WebSocket(websocketURL);
ws.onmessage = (message) => { 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 = { const liveEvent: LiveEvent = {
channel: channel, channel: channel,
type: "updated", type: type,
payload: { payload: {
data: JSON.parse(message.data),
ids: [id], ids: [id],
data: data.payload,
}, },
date: new Date(), date: date,
} }
callback(liveEvent); callback(liveEvent);