removed library remains, add helper usage in hooks

This commit is contained in:
Alex
2024-10-13 13:41:36 +02:00
parent d3365ae065
commit 58daf7bf30
4 changed files with 29 additions and 99 deletions

View File

@@ -1,5 +1,7 @@
/** @type {import('./$types').LayoutLoad} */
export async function load({ fetch, url, data }) {
const { user } = data;
return { fetch, url: url.pathname, user };
const user = data.user;
const subscriptions = data.subscriptions;
const licence_categories = data.licence_categories;
return { fetch, url: url.pathname, user, subscriptions, licence_categories };
}

View File

@@ -1,58 +1,11 @@
import { BASE_API_URI } from "$lib/utils/constants";
import { refreshCookie } from "$lib/utils/helpers";
import { redirect } from "@sveltejs/kit";
import { library } from "$lib/stores/library";
/** @type {import('./$types').LayoutServerLoad} */
export async function load({ locals, cookies }) {
const jwt = cookies.get("jwt");
try {
// Fetch subscriptions, and licence categories in parallel
const [subscriptionsResponse, licenceCategoriesResponse] =
await Promise.all([
fetch(`${BASE_API_URI}/backend/membership/subscriptions`, {
credentials: "include",
headers: { Cookie: `jwt=${jwt}` },
}),
fetch(`${BASE_API_URI}/backend/licence/categories`, {
credentials: "include",
headers: { Cookie: `jwt=${jwt}` },
}),
]);
// Check if any of the responses are not ok
if (!subscriptionsResponse.ok || !licenceCategoriesResponse.ok) {
cookies.delete("jwt", { path: "/" });
throw redirect(302, "/");
}
// Parse the JSON responses
const [subscriptionsData, licence_categoriesData] = await Promise.all([
subscriptionsResponse.json(),
licenceCategoriesResponse.json(),
]);
// Check if the server sent a new token
const newToken =
subscriptionsResponse.headers.get("Set-Cookie") == null
? licenceCategoriesResponse.headers.get("Set-Cookie")
: subscriptionsResponse.headers.get("Set-Cookie");
refreshCookie(newToken, null);
return {
user: locals.user,
licence_categories: licence_categoriesData.licence_categories,
subscriptions: subscriptionsData.subscriptions,
};
} catch (error) {
console.error("Error fetching data:", error);
// In case of any error, clear the JWT cookie
cookies.delete("jwt", { path: "/" });
//throw redirect(302, "/");
}
return {
user: locals.user,
licence_categories: [],
subscriptions: [],
licence_categories: locals.licence_categories,
subscriptions: locals.subscriptions,
};
}

View File

@@ -4,14 +4,11 @@
import { onMount } from "svelte";
import { page } from "$app/stores";
import { t } from "svelte-i18n";
import { library } from "$lib/stores/library";
/** @type {import('./$types').ActionData} */
export let form;
$: user = $page.data.user;
$: ({ licence_categories, subscriptions } = $library);
$: ({ user, licence_categories, subscriptions } = $page.data);
let showModal = false;