From c799a52b9ad0a5fe6650e89169c1df64583b1c3e Mon Sep 17 00:00:00 2001 From: James Date: Tue, 27 Feb 2024 15:02:50 -0500 Subject: [PATCH 1/3] [ALS-5983] Add a temp session modal --- src/lib/api.ts | 61 +++++++++++++++++++++++++++++++++++++++ src/routes/+layout.svelte | 35 ++++++++++++++++++++-- 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/lib/api.ts diff --git a/src/lib/api.ts b/src/lib/api.ts new file mode 100644 index 00000000..8b181f40 --- /dev/null +++ b/src/lib/api.ts @@ -0,0 +1,61 @@ +import { error, type NumericRange } from '@sveltejs/kit'; + +async function send({ + method, + path, + token, + data, +}: { + method: string; + path: string; + token: string; + data?: any; //TODO: Change this +}) { + const opts: { method: string; headers: { [key: string]: string }; body?: string } = { + method, + headers: {}, + }; + + if (data) { + opts.headers['Content-Type'] = 'application/json'; + opts.body = JSON.stringify(data); + } + + if (sessionStorage.token) { + token = sessionStorage.token; + } + + if (token) { + opts.headers['Authorization'] = `Token ${token}`; + } + + console.debug('fetching', `${window.location.origin}/${path}`, opts); + const res = await fetch(`${window.location.origin}${path}`, opts); + if (res.ok || res.status === 422) { + const text = await res.text(); + console.log('text', text); + try { + return JSON.parse(text); + } catch (e) { + return text; //TODO: Change this + } + } + + throw error(res.status as NumericRange<400, 599>, await res.text()); +} + +export function get(path: string, token: string) { + return send({ method: 'GET', path, token }); +} + +export function del(path: string, token: string) { + return send({ method: 'DELETE', path, token }); +} + +export function post(path: string, token: string, data: any) { + return send({ method: 'POST', path, token, data }); +} + +export function put(path: string, token: string, data: any) { + return send({ method: 'PUT', path, token, data }); +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 714d87fa..c12d4afc 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,19 +1,50 @@ + + +
-
From c60fd8f7bb33ce847774908a2a9eb76aee2773d2 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 27 Feb 2024 15:04:21 -0500 Subject: [PATCH 2/3] format changes --- src/lib/api.ts | 4 ++-- src/routes/+layout.svelte | 39 +++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/lib/api.ts b/src/lib/api.ts index 8b181f40..5485d9b1 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -4,7 +4,7 @@ async function send({ method, path, token, - data, + data }: { method: string; path: string; @@ -13,7 +13,7 @@ async function send({ }) { const opts: { method: string; headers: { [key: string]: string }; body?: string } = { method, - headers: {}, + headers: {} }; if (data) { diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index c12d4afc..77827b75 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,16 +1,23 @@ From 55812bcf03ce80430316b29b2933d4d50e43886d Mon Sep 17 00:00:00 2001 From: James Date: Tue, 27 Feb 2024 15:19:31 -0500 Subject: [PATCH 3/3] Remove test --- tests/lib/test.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/lib/test.ts b/tests/lib/test.ts index 9c9bba6d..f17b9150 100644 --- a/tests/lib/test.ts +++ b/tests/lib/test.ts @@ -13,12 +13,6 @@ const routes = [ test.describe('Navigation', () => { routes.forEach((route) => { - test(`${route.path} page has expected heading`, async ({ page }) => { - await page.goto('/'); - const navItem = page.locator('#' + route.id); - await navItem.click(); - await expect(page.locator('.main-content>h1')).toHaveText(route.headerText); - }); test(`${route.path} navigation bar has correct active element`, async ({ page }) => { await page.goto(route.path);