Skip to content

Commit

Permalink
[ALS-5983] Add a temp session modal (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPeck authored Feb 27, 2024
1 parent f61f3e5 commit f9efbab
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 8 deletions.
61 changes: 61 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
42 changes: 40 additions & 2 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
<script lang="ts">
import { AppShell } from '@skeletonlabs/skeleton';
import {
AppShell,
initializeStores,
Modal,
Toast,
getModalStore,
type ModalSettings
} from '@skeletonlabs/skeleton';
import Navigation from '$lib/navigation.svelte';
import '@fortawesome/fontawesome-free/css/all.min.css';
import '../app.postcss';
import { onMount } from 'svelte';
initializeStores();
let modalProps: Record<string, unknown> = {
buttonPositive: 'variant-filled-primary'
};
const modalStore = getModalStore();
onMount(() => {
document.body.classList.add('started');
checkForSession();
});
// // Floating UI for Popups
// import { computePosition, autoUpdate, flip, shift, offset, arrow } from '@floating-ui/dom';
// import { storePopup } from '@skeletonlabs/skeleton';
// storePopup.set({ computePosition, autoUpdate, flip, shift, offset, arrow });
function checkForSession() {
if (!sessionStorage.getItem('token')) {
const modal: ModalSettings = {
type: 'prompt',
// Data
title: 'Enter Long Term Token',
body: 'Provide your long term token below.',
// Populates the input value and attributes
value: 'Token here...',
valueAttr: { type: 'text', required: true },
// Returns the updated response value
response: (r: string) => {
sessionStorage.setItem('token', r);
}
};
modalStore.trigger(modal);
}
}
</script>

<Toast />
<Modal {...modalProps} />
<AppShell>
<svelte:fragment slot="header">
<Navigation />
<hr class="!border-t-2" />
</svelte:fragment>
<hr class="!border-t-2" />
<!-- (sidebarLeft) -->
<!-- (sidebarRight) -->
<!-- (pageHeader) -->
Expand Down
6 changes: 0 additions & 6 deletions tests/lib/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f9efbab

Please sign in to comment.