Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ALS-5983] Add a temp session modal #9

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pull the token in here so we don't have to pass it down everywhere?

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