Skip to content

Commit

Permalink
get api keys working again again and work on sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
eksno committed Apr 18, 2024
1 parent 435401d commit 0fbe392
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 28 deletions.
1 change: 0 additions & 1 deletion apps/web/src/routes/app/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { error } from '@sveltejs/kit';

export const load = async ({ locals: { supabase, stripe, getSession } }) => {
const userSession = await getSession();
if (!userSession) throw error(401, 'You are not logged in. Please log in and try again.');

// TODO: convert to using api

Expand Down
12 changes: 5 additions & 7 deletions apps/web/src/routes/app/account/api-keys/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import { apiKeySchema } from '$lib/schema';

export const load = async ({ locals: { getSession } }) => {
const userSession = await getSession();
if (!userSession) throw error(401, 'You are not logged in. Please log in and try again.');

const userApiKeys = await api
.GET('/profiles/{profile_id}/api_keys', {
.GET('/api-keys/', {
params: {
path: {
query: {
profile_id: userSession.user.id
}
}
Expand All @@ -31,7 +30,7 @@ export const load = async ({ locals: { getSession } }) => {

const apiKeyTypes = await api.GET('/api_key_types/').then(({ data: d, error: e }) => {
if (e) {
console.error(`Error retrieving api key types: ${e.detail}`);
console.error(`Error retrieving api key types: ${e}`);
return [];
}
if (!d) {
Expand All @@ -48,7 +47,6 @@ export const load = async ({ locals: { getSession } }) => {
export const actions = {
create: async ({ request, locals }) => {
const userSession = await locals.getSession();
if (!userSession) throw error(401, 'You are not logged in. Please log in and try again.');

const form = await superValidate(request, zod(apiKeySchema));

Expand All @@ -57,7 +55,7 @@ export const actions = {
}

await api
.POST('/api_keys/', {
.POST('/api-keys/', {
body: {
profile_id: userSession.user.id,
api_key: form.data.value,
Expand All @@ -82,7 +80,7 @@ export const actions = {
}

await api
.DELETE(`/api_keys/{api_key_id}`, {
.DELETE(`/api-keys/{api_key_id}`, {
params: { path: { api_key_id: id } }
})
.then(({ data: d, error: e }) => {
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/routes/app/crews/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import api from '$lib/api';

export const load = async ({ locals: { getSession } }) => {
const userSession = await getSession();
if (!userSession) throw error(401, 'You are not logged in. Please log in and try again.');

const form = await superValidate(zod(editCrewSchema));

Expand Down
17 changes: 11 additions & 6 deletions apps/web/src/routes/app/session/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { error, redirect } from '@sveltejs/kit';
import api from '$lib/api';
import type { Session } from '@supabase/supabase-js';

export const load = async ({ url, locals: { getSession } }) => {
const userSession = await getSession();

if (!userSession) throw error(401, 'You are not logged in. Please log in and try again.');

const redirectToSessions = async (userSession: Session) => {
const sessions = await api
.GET('/sessions/', {
params: {
Expand All @@ -26,10 +23,18 @@ export const load = async ({ url, locals: { getSession } }) => {
return d;
});

if (sessions[0] && !url.searchParams.has('debug')) {
if (sessions[0]) {
console.log(`Redirecting to session ${sessions[0].id}`);
redirect(303, `/app/session/${sessions[0].id}`);
}
};

export const load = async ({ url, locals: { getSession } }) => {
const userSession = await getSession();

if (!url.searchParams.has('debug')) {
await redirectToSessions(userSession);
}

const crews = await api
.GET('/crews/', {
Expand Down
21 changes: 9 additions & 12 deletions apps/web/src/routes/app/session/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script lang="ts">
import { Button } from '$lib/components/ui/button';
import type { NoSessionLoad } from '$lib/types/loads';
import api from '$lib/api';
import api, { type schemas } from '$lib/api';
export let data: NoSessionLoad;
export let data;
let profileId = data.profileId;
let crews = data.crews;
let crew: schemas['Crew'] | undefined = crews[0];
async function startNewSession(profileId: string, crewId: string, title: string) {
const runResponse = await api
Expand Down Expand Up @@ -37,19 +37,16 @@
}
</script>

{#if crews}
{#if crew}
<div
class="xl:prose-md prose prose-sm prose-main mx-auto flex h-screen max-w-none flex-col items-center justify-center gap-4 px-12 text-center md:prose-base 2xl:prose-lg"
>
<h1>It looks like you haven't started a session yet...</h1>
{#if crews}
<!-- Allow user to choose crew -->
<Button on:click={() => startNewSession(profileId, crews[0].id, 'New Session')}
>Run Your Crew!</Button
>
{:else}
<p>Loading...</p>
{/if}
<!-- TODO: Allow user to choose crew -->

<Button on:click={() => crew && startNewSession(profileId, crew.id, 'New Session')}
>Run Your Crew!</Button
>
</div>
{:else}
<div
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/routes/app/session/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import api from '$lib/api';

export const load = async ({ params, locals: { getSession } }) => {
const userSession = await getSession();
if (!userSession) throw error(401, 'You are not logged in. Please log in and try again.');

const session = await api
.GET('/sessions/{session_id}', {
Expand Down

0 comments on commit 0fbe392

Please sign in to comment.