diff --git a/src/Controllers/NodeController.cs b/src/Controllers/NodeController.cs index 8a946d68..9be8f93c 100644 --- a/src/Controllers/NodeController.cs +++ b/src/Controllers/NodeController.cs @@ -9,13 +9,19 @@ namespace ESPresense.Controllers; public class NodeController(NodeSettingsStore nodeSettingsStore, State state) : ControllerBase { [HttpGet("{id}/settings")] - public NodeSettingsDetails Get(string id) + public NodeSettings Get(string id) { var nodeSettings = nodeSettingsStore.Get(id); + return nodeSettings ?? new Models.NodeSettings(id); + } + + [HttpGet("{id}/details")] + public IList> Details(string id) + { var details = new List>(); - if (nodeSettings?.Id != null && state.Nodes.TryGetValue(id, out var node)) + if (state.Nodes.TryGetValue(id, out var node)) details.AddRange(node.GetDetails()); - return new NodeSettingsDetails(nodeSettings ?? new Models.NodeSettings(id), details); + return details; } [HttpPut("{id}/settings")] diff --git a/src/ui/src/lib/GlobalSettings.svelte b/src/ui/src/lib/GlobalSettings.svelte deleted file mode 100644 index 8e3aecb1..00000000 --- a/src/ui/src/lib/GlobalSettings.svelte +++ /dev/null @@ -1,155 +0,0 @@ - - -{#if loading} -
-
- -

Loading settings...

-
-
-{:else if error} -
-

Error: {error}

-
-{:else if $settings} -
-
-

Updating

-
-
- - -
-
- - -
-
-
- -
-

Scanning

-
- -
-
- -
-

Counting

-
- - - - - - - -
-
- -
-

Filtering

-
- - - - - - - - - -
-
- -
-

Calibration

-
- - - - - - - -
-
- -
- -
-
-{:else} -
-

No settings available.

-
-{/if} \ No newline at end of file diff --git a/src/ui/src/lib/NodeActions.svelte b/src/ui/src/lib/NodeActions.svelte index c3ab6ee7..24c56fa0 100644 --- a/src/ui/src/lib/NodeActions.svelte +++ b/src/ui/src/lib/NodeActions.svelte @@ -1,11 +1,11 @@ -{#if settings} - - - -

Settings

-
- -
- - - - - - - -
-
-
-
+{#if loading} +
+
+ +

Loading settings...

+
+
+{:else if error} +
+

Error: {error}

+
+{:else if settings} +
+
+

Updating

+
+
+ + +
+
+ + +
+
+
+ +
+

Scanning

+
+ +
+
+ +
+

Counting

+
+ + + + + + + +
+
+ +
+

Filtering

+
+ + + + + + + + + +
+
+ +
+

Calibration

+
+ + + + + + + +
+
+ +
+ +
+
{:else} -
Loading...
-{/if} +
+

No settings available.

+
+{/if} \ No newline at end of file diff --git a/src/ui/src/lib/node.ts b/src/ui/src/lib/node.ts index 7bc8db8e..2526d04c 100644 --- a/src/ui/src/lib/node.ts +++ b/src/ui/src/lib/node.ts @@ -1,22 +1,21 @@ import { base } from '$app/paths'; -import type { Settings, Node, NodeSetting } from './types'; +import type { Node, NodeSetting } from './types'; -export async function loadSettings(id: string): Promise { +export async function loadNodeSettings(id: string): Promise { const response = await fetch(`${base}/api/node/${id}/settings`); if (!response.ok) throw new Error("Something went wrong loading settings (error="+response.status+" "+response.statusText+")"); return await response.json(); } -export async function saveSettings(newSettings: Settings): Promise { +export async function saveNodeSettings(newSettings: NodeSetting): Promise { const response = await fetch(`${base}/api/node/${newSettings.id}/settings`, { - method: 'POST', + method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(newSettings), }); - if (!response.ok) throw new Error("Something went wrong loading settings (error="+response.status+" "+response.statusText+")"); - return await response.json(); + if (!response.ok) throw new Error("Something went wrong saving settings (error="+response.status+" "+response.statusText+")"); } export async function restartNode(nodeId: string): Promise { @@ -24,24 +23,13 @@ export async function restartNode(nodeId: string): Promise { if (!response.ok) throw new Error(response.statusText); } -export async function updateNodeSelf(nodeId: string): Promise { +export async function updateNode(nodeId: string): Promise { const response = await fetch(`${base}/api/node/${nodeId}/update`, { method: 'POST' }); if (!response.ok) throw new Error(response.statusText); } -export async function saveNodeSettings(nodeId: string, settings: NodeSetting): Promise { - const response = await fetch(`${base}/api/node/${nodeId}/settings`, { - method: 'PUT', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(settings) - }); - if (!response.ok) throw new Error(response.statusText); -} - -export async function fetchNode(nodeId: string): Promise { - const response = await fetch(`${base}/api/node/${nodeId}/settings`); +export async function fetchNodeDetails(nodeId: string): Promise { + const response = await fetch(`${base}/api/node/${nodeId}/details`); if (!response.ok) throw new Error(response.statusText); return await response.json(); } \ No newline at end of file diff --git a/src/ui/src/lib/stores.ts b/src/ui/src/lib/stores.ts index c8a02844..6251cdea 100644 --- a/src/ui/src/lib/stores.ts +++ b/src/ui/src/lib/stores.ts @@ -1,7 +1,6 @@ import { readable, writable, derived } from 'svelte/store'; import { base } from '$app/paths'; -import type { Device, Config, Node, Settings, CalibrationData } from './types'; -import { loadSettings, saveSettings } from './node'; +import type { Device, Config, Node, CalibrationData } from './types'; import { fetchConfig, fetchCalibrationState, fetchDeviceState, fetchNodeState } from './state'; export const showAll: SvelteStore = writable(false); @@ -128,21 +127,3 @@ export const calibration = readable({ matrix: {} }, function st clearInterval(interval); }; }); - -export const settings = (() => { - const { subscribe, set, update } = writable(null); - - return { - subscribe, - set, - update, - load: async () => { - const data = await loadSettings("*"); - set(data); - }, - save: async (newSettings: Settings) => { - const data = await saveSettings(newSettings); - set(data); - }, - }; -})(); \ No newline at end of file diff --git a/src/ui/src/lib/types.ts b/src/ui/src/lib/types.ts index d7571214..9d5152ab 100644 --- a/src/ui/src/lib/types.ts +++ b/src/ui/src/lib/types.ts @@ -132,6 +132,8 @@ export interface CalibrationData { } } +export type NodeDetail = Array<{ key: string; value: string }>; + export type NodeSetting = { id: string | null; name: string | null; diff --git a/src/ui/src/lib/urls.ts b/src/ui/src/lib/urls.ts index 80f81793..67764f1d 100644 --- a/src/ui/src/lib/urls.ts +++ b/src/ui/src/lib/urls.ts @@ -2,7 +2,11 @@ import { base } from '$app/paths'; import { goto } from '$app/navigation'; import { isNode, type Device, type Node } from '$lib/types'; -export function detail(d: Device | Node | null) { +export function gotoDetail(d: Device | Node | null) { if (isNode(d)) goto(`${base}/nodes/${d?.id}`); else goto(`${base}/devices/${d?.id}`); } + +export function gotoSettings(id: string) { + return goto(`${base}/settings/node/${id}`); +} \ No newline at end of file diff --git a/src/ui/src/routes/+page.svelte b/src/ui/src/routes/+page.svelte index c19514f6..caa8bbe5 100644 --- a/src/ui/src/routes/+page.svelte +++ b/src/ui/src/routes/+page.svelte @@ -1,7 +1,7 @@ @@ -12,5 +12,5 @@
- detail(d.detail)} bind:floorId /> + gotoDetail(d.detail)} bind:floorId />
diff --git a/src/ui/src/routes/devices/+page.svelte b/src/ui/src/routes/devices/+page.svelte index d94d01df..5cc38933 100644 --- a/src/ui/src/routes/devices/+page.svelte +++ b/src/ui/src/routes/devices/+page.svelte @@ -1,6 +1,6 @@ @@ -9,5 +9,5 @@

Devices

- detail(d.detail)} /> + gotoDetail(d.detail)} />
diff --git a/src/ui/src/routes/nodes/[id]/+page.svelte b/src/ui/src/routes/nodes/[id]/+page.svelte index 67520027..a63dfd46 100644 --- a/src/ui/src/routes/nodes/[id]/+page.svelte +++ b/src/ui/src/routes/nodes/[id]/+page.svelte @@ -1,7 +1,7 @@ @@ -7,8 +7,8 @@
-

Settings

+

Global Settings

These settings will be applied to every node, including new nodes.

- +
diff --git a/src/ui/src/routes/settings/node/[id]/+page.svelte b/src/ui/src/routes/settings/node/[id]/+page.svelte new file mode 100644 index 00000000..13eea9ad --- /dev/null +++ b/src/ui/src/routes/settings/node/[id]/+page.svelte @@ -0,0 +1,14 @@ + + + + ESPresense Companion: {data.title} + + +
+

{data.title}

+ +
diff --git a/src/ui/src/routes/settings/node/[id]/+page.ts b/src/ui/src/routes/settings/node/[id]/+page.ts new file mode 100644 index 00000000..9c07b327 --- /dev/null +++ b/src/ui/src/routes/settings/node/[id]/+page.ts @@ -0,0 +1,9 @@ +import { error } from '@sveltejs/kit'; +import type { PageLoad } from './$types'; + +export const load: PageLoad<{ id: string }> = async ({ params }) => { + if (!params.id) { + throw error(400, 'No node id'); + } + return { id: params.id, title: 'Settings for ' + params.id }; +};