From 1d7b2842c3bfa5429480d84e3f6bbeef4af6b4b9 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Mon, 30 Oct 2023 12:41:44 +0000 Subject: [PATCH] Bring routes and redirects views closer in implementation --- src/views/RedirectsView.svelte | 29 ++++++++++++++--------------- src/views/RoutesView.svelte | 17 ++++++++--------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/views/RedirectsView.svelte b/src/views/RedirectsView.svelte index b43a5ed..9f0911d 100644 --- a/src/views/RedirectsView.svelte +++ b/src/views/RedirectsView.svelte @@ -9,9 +9,9 @@ const apiViolet = import.meta.env.VITE_API_VIOLET; let tableData: {[key: string]: CSPair} = {}; - let redirectSrcs: string[] = []; + let tableKeys: string[] = []; - $: redirectSrcs = Object.entries(tableData) + $: tableKeys = Object.entries(tableData) .filter(x => x[1].client != null || x[1].server != null) .map(x => x[0]) .filter(x => domainFilter(x, $domainOption)) @@ -26,19 +26,18 @@ return p.endsWith(domain); } - let promiseForRedirects: Promise = reloadRedirects(true); + let promiseForTable: Promise = reloadTable(true); - function reloadRedirects(firstLoad: boolean = false): Promise { + function reloadTable(firstLoad: boolean = false): Promise { return new Promise((res, rej) => { fetch(apiViolet + "/redirect", {headers: {Authorization: getBearer()}}) .then(x => { - if (x.status != 200) throw new Error("Unexpected status code: " + x.status); + if (x.status !== 200) throw new Error("Unexpected status code: " + x.status); return x.json(); }) .then(x => { - let redirects = x as Redirect[]; - let y: {[key: string]: CSPair} = {}; - redirects.forEach(x => { + let rows = x as Redirect[]; + rows.forEach(x => { tableData[x.src] = { client: firstLoad || !tableData[x.src] ? JSON.parse(JSON.stringify(x)) : tableData[x.src]?.client, server: x, @@ -57,7 +56,7 @@ } function saveChanges() { - let redirectPromises = redirectSrcs + let tableProm = tableKeys .map(x => tableData[x]) .filter(x => x.client != null || x.server != null) .filter(x => !redirectEqual(x.client, x.server)) @@ -77,8 +76,8 @@ return x.v.p; }); - Promise.all(redirectPromises) - .then(_ => reloadRedirects()) + Promise.all(tableProm) + .then(_ => reloadTable()) .catch(_ => { alert("Some rows failed to save changes"); }); @@ -91,7 +90,7 @@
- {#await promiseForRedirects} + {#await promiseForTable}
Loading...
@@ -111,13 +110,13 @@ on:make={e => { const x = e.detail; tableData[x.src] = {client: x, server: tableData[x.src]?.server, p: Promise.resolve()}; - redirectSrcs.push(x.src); - redirectSrcs = redirectSrcs; + tableKeys.push(x.src); + tableKeys = tableKeys; }} /> - {#each redirectSrcs as src (src)} + {#each tableKeys as src (src)} {#await tableData[src].p} Loading... {:then _} diff --git a/src/views/RoutesView.svelte b/src/views/RoutesView.svelte index 64f2d5f..e3b2858 100644 --- a/src/views/RoutesView.svelte +++ b/src/views/RoutesView.svelte @@ -26,9 +26,9 @@ return p.endsWith(domain); } - let promiseForRoutes: Promise = reloadRoutes(true); + let promiseForTable: Promise = reloadTable(true); - function reloadRoutes(firstLoad: boolean = false): Promise { + function reloadTable(firstLoad: boolean = false): Promise { return new Promise((res, rej) => { fetch(apiViolet + "/route", {headers: {Authorization: getBearer()}}) .then(x => { @@ -36,9 +36,8 @@ return x.json(); }) .then(x => { - let routes = x as Route[]; - let y: {[key: string]: CSPair} = {}; - routes.forEach(x => { + let rows = x as Route[]; + rows.forEach(x => { tableData[x.src] = { client: firstLoad || !tableData[x.src] ? JSON.parse(JSON.stringify(x)) : tableData[x.src]?.client, server: x, @@ -57,7 +56,7 @@ } function saveChanges() { - let routePromises = tableKeys + let tableProm = tableKeys .map(x => tableData[x]) .filter(x => x.client != null || x.server != null) .filter(x => !routeEqual(x.client, x.server)) @@ -77,8 +76,8 @@ return x.v.p; }); - Promise.all(routePromises) - .then(_ => reloadRoutes()) + Promise.all(tableProm) + .then(_ => reloadTable()) .catch(_ => { alert("Some rows failed to save changes"); }); @@ -91,7 +90,7 @@
- {#await promiseForRoutes} + {#await promiseForTable}
Loading...