1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-29 17:59:36 +02:00

refactor: Simplify URL handling in adventures page server code

This commit is contained in:
Sean Morley 2024-07-12 09:41:46 -04:00
parent b9745cd700
commit 22b2cb3679

View file

@ -432,7 +432,7 @@ export const actions: Actions = {
const previous = formData.get('previous') as string; const previous = formData.get('previous') as string;
const page = formData.get('page') as string; const page = formData.get('page') as string;
if (!event.locals.user) { if (!event.locals.user || !event.cookies.get('auth')) {
return { return {
status: 401, status: 401,
body: { message: 'Unauthorized' } body: { message: 'Unauthorized' }
@ -446,14 +446,8 @@ export const actions: Actions = {
}; };
} }
// Start with the provided URL or default to the filtered adventures endpoint // Start with the current URL if next and previous are not provided
let url: string = next || previous || `${serverEndpoint}/api/adventures/filtered`; let url: string = next || previous || event.url.toString();
// Extract the path and query parameters
const urlParts = url.split('/api');
if (urlParts.length > 1) {
url = `${serverEndpoint}/api${urlParts[1]}`;
}
// Replace or add the page number in the URL // Replace or add the page number in the URL
if (url.includes('page=')) { if (url.includes('page=')) {
@ -464,6 +458,8 @@ export const actions: Actions = {
url += `page=${page}`; url += `page=${page}`;
} }
console.log('CHANGE PAGE' + url);
try { try {
const response = await fetch(url, { const response = await fetch(url, {
headers: { headers: {