Skip to content

Commit adef58f

Browse files
committed
fix device route for new API
1 parent 38e0de2 commit adef58f

File tree

5 files changed

+85
-136
lines changed

5 files changed

+85
-136
lines changed

src/lib/common/classes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export class Device {
77
public forcedTags: string[] = [];
88
public validTags: string[] = [];
99
public invalidTags: string[] = [];
10+
public approvedRoutes: string[] = [];
11+
public availableRoutes: string[] = [];
12+
public subnetRoutes: string[] = [];
1013
public user: { name: string } = { name: '' };
1114
public online?: boolean;
1215

src/lib/devices/DeviceCard/DeviceRoutes.svelte

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,21 @@
11
<script lang="ts">
2-
import { getDeviceRoutes, modifyDeviceRoutes } from './DeviceRoutesAPI.svelte';
3-
import { Device, Route } from '$lib/common/classes';
4-
import { onMount } from 'svelte';
2+
import { modifyDeviceRoutes } from './DeviceRoutes/DeviceRouteAPI.svelte';
3+
import { Device } from '$lib/common/classes';
4+
import DeviceRoute from './DeviceRoutes/DeviceRoute.svelte';
5+
import { getDevices } from '$lib/common/apiFunctions.svelte';
56
import { alertStore } from '$lib/common/stores';
67
78
export let device = new Device();
8-
let routesList: Route[] = [];
9-
let routeID = 0;
109
11-
onMount(async () => {
12-
getDeviceRoutesAction();
13-
});
1410
15-
function getDeviceRoutesAction() {
16-
getDeviceRoutes(device.id)
17-
.then((routes) => {
18-
routesList = routes;
19-
})
20-
.catch((error) => {
21-
$alertStore = error;
22-
});
23-
}
24-
25-
function modifyDeviceRoutesAction() {
26-
modifyDeviceRoutes(device.id, routesList, routeID)
27-
.then((response) => {
28-
getDeviceRoutesAction();
29-
})
30-
.catch((error) => {
31-
$alertStore = error;
32-
});
33-
}
3411
</script>
3512

3613
<th>Device Routes</th>
3714
<td
3815
><ul class="list-disc list-inside">
39-
{#each routesList as route, index}
16+
{#each device.availableRoutes as route}
4017
<li>
41-
{route.prefix}
42-
{#if route.enabled}
43-
<button
44-
on:click={() => {
45-
routesList[index].enabled = false;
46-
routeID = route.id;
47-
modifyDeviceRoutesAction();
48-
}}
49-
type="button"
50-
class="btn btn-xs tooltip capitalize bg-success text-success-content mx-1"
51-
data-tip="press to disable route">active</button
52-
>
53-
{:else}
54-
<button
55-
on:click={() => {
56-
routesList[index].enabled = true;
57-
routeID = route.id
58-
modifyDeviceRoutesAction();
59-
}}
60-
type="button"
61-
class="btn btn-xs tooltip capitalize bg-secondary text-secondary-content mx-1"
62-
data-tip="press to enable route">pending</button
63-
>
64-
{/if}
18+
<DeviceRoute {route} {device}></DeviceRoute>
6519
</li>
6620
{/each}
6721
</ul></td
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<script>
2+
import { getDevices } from '$lib/common/apiFunctions.svelte';
3+
import { Device } from '$lib/common/classes';
4+
import { alertStore } from '$lib/common/stores';
5+
import { approveDeviceRoute } from './DeviceRouteAPI.svelte';
6+
7+
export let route = ""
8+
export let device = new Device();
9+
10+
let routeDisabled = false;
11+
function approveRouteAction() {
12+
approveDeviceRoute(device.id, [route])
13+
.then(() => {
14+
// refresh users after editing
15+
getDevices();
16+
})
17+
.catch((error) => {
18+
$alertStore = error;
19+
});
20+
}
21+
22+
</script>
23+
24+
{route}
25+
{#if device.approvedRoutes.includes(route)}
26+
<button type="button" class="btn btn-xs tooltip capitalize bg-success text-success-content mx-1">active</button>
27+
{:else}
28+
<button
29+
on:click={() => {
30+
routeDisabled = true;
31+
approveRouteAction();
32+
routeDisabled = false;
33+
}}
34+
type="button"
35+
class="btn btn-xs tooltip capitalize bg-secondary text-secondary-content mx-1" class:disabled={routeDisabled}
36+
data-tip="click to enable route">pending</button
37+
>
38+
{/if}
39+
{#if device.subnetRoutes.includes(route)}
40+
<button type="button" class="btn btn-xs tooltip capitalize bg-secondary text-secondary-content mx-1">subnet</button>
41+
{/if}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<script context="module" lang="ts">
2+
import type { Route } from '$lib/common/classes';
3+
4+
export async function approveDeviceRoute(deviceID: string, routes: [string]): Promise<any> {
5+
// variables in local storage
6+
let headscaleURL = localStorage.getItem('headscaleURL') || '';
7+
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
8+
9+
let endpointURL = `/api/v1/node/${deviceID}/approve_routes`;
10+
11+
await fetch(headscaleURL + endpointURL, {
12+
method: 'POST',
13+
headers: {
14+
Accept: 'application/json',
15+
Authorization: `Bearer ${headscaleAPIKey}`
16+
},
17+
body: JSON.stringify({
18+
routes: routes
19+
})
20+
})
21+
.then((response) => {
22+
if (response.ok) {
23+
// return the api data
24+
return response;
25+
} else {
26+
return response.text().then((text) => {
27+
throw JSON.parse(text).message;
28+
});
29+
}
30+
})
31+
.catch((error) => {
32+
throw error;
33+
});
34+
}
35+
</script>

src/lib/devices/DeviceCard/DeviceRoutesAPI.svelte

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)