From 843367c4eb4174bf8df556a0c769f18340b178d8 Mon Sep 17 00:00:00 2001 From: Christopher Bisset Date: Mon, 25 Jul 2022 10:28:48 +1000 Subject: [PATCH] added tag functionality --- src/lib/common/apiFunctions.svelte | 34 ++++++++++++++- src/lib/devices/DeviceCard/DeviceTags.svelte | 40 +++++++++++++++--- .../DeviceCard/DeviceTags/NewDeviceTag.svelte | 42 ++++++++++++++++--- 3 files changed, 105 insertions(+), 11 deletions(-) diff --git a/src/lib/common/apiFunctions.svelte b/src/lib/common/apiFunctions.svelte index 2458cf9..9ff0d56 100644 --- a/src/lib/common/apiFunctions.svelte +++ b/src/lib/common/apiFunctions.svelte @@ -69,6 +69,39 @@ }); } + export async function updateTags(deviceID: string, tags: string[]): Promise { + // variables in local storage + let headscaleURL = localStorage.getItem('headscaleURL') || ''; + let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || ''; + + // endpoint url for editing users + let endpointURL = '/api/v1/machine/' + deviceID + '/tags'; + + await fetch(headscaleURL + endpointURL, { + method: 'POST', + headers: { + Accept: 'application/json', + Authorization: `Bearer ${headscaleAPIKey}` + }, + body: JSON.stringify({ + tags: tags + }) + }) + .then((response) => { + if (response.ok) { + return response; + } else { + return response.text().then((text) => { + throw JSON.parse(text).message; + }); + } + }) + .catch((error) => { + throw error; + }); + } + + export async function removeUser(currentUsername: string): Promise { // variables in local storage let headscaleURL = localStorage.getItem('headscaleURL') || ''; @@ -166,7 +199,6 @@ await headscaleDeviceResponse.json().then((data) => { headscaleDevices = data.machines; }); - // console.log(headscaleDevices); return headscaleDevices; } diff --git a/src/lib/devices/DeviceCard/DeviceTags.svelte b/src/lib/devices/DeviceCard/DeviceTags.svelte index 94f8f5e..4ec7f8f 100644 --- a/src/lib/devices/DeviceCard/DeviceTags.svelte +++ b/src/lib/devices/DeviceCard/DeviceTags.svelte @@ -1,17 +1,47 @@ - - + {#each device.forcedTags as tag} - {tag.replace("tag:","")} + {tag.replace("tag:","")} + + + {/each} {#each device.validTags as tag} - {tag.replace("tag:","")} + {tag.replace("tag:","")} {/each} diff --git a/src/lib/devices/DeviceCard/DeviceTags/NewDeviceTag.svelte b/src/lib/devices/DeviceCard/DeviceTags/NewDeviceTag.svelte index f0966b6..96afd7b 100644 --- a/src/lib/devices/DeviceCard/DeviceTags/NewDeviceTag.svelte +++ b/src/lib/devices/DeviceCard/DeviceTags/NewDeviceTag.svelte @@ -1,20 +1,51 @@ -
{ editingTag = true; }} class="btn btn-xs border-dotted border-2 btn-primary opacity-60 normal-case" > {#if !editingTag} - + tag + + tag {:else} -
{console.log("hi");}}> - + +
+