1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-21 22:09:36 +02:00
AdventureLog/frontend/src/routes/adventures/+page.server.ts

587 lines
14 KiB
TypeScript
Raw Normal View History

2024-07-08 11:44:39 -04:00
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
import type { Adventure } from '$lib/types';
2024-07-11 20:09:55 -04:00
import type { Actions, RequestEvent } from '@sveltejs/kit';
2024-07-08 11:44:39 -04:00
import { fetchCSRFToken, tryRefreshToken } from '$lib/index.server';
import { checkLink } from '$lib';
const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
2024-07-11 09:33:52 -04:00
export const load = (async (event) => {
if (!event.locals.user) {
return redirect(302, '/login');
} else {
2024-07-11 19:27:03 -04:00
let next = null;
let previous = null;
let count = 0;
2024-07-11 15:37:04 -04:00
let adventures: Adventure[] = [];
2024-07-11 19:27:03 -04:00
let initialFetch = await fetch(
2024-07-15 18:01:49 -04:00
`${serverEndpoint}/api/adventures/filtered?types=visited,planned&include_collections=false`,
2024-07-11 19:27:03 -04:00
{
headers: {
Cookie: `${event.cookies.get('auth')}`
}
2024-07-11 09:33:52 -04:00
}
2024-07-11 19:27:03 -04:00
);
if (!initialFetch.ok) {
2024-07-11 09:33:52 -04:00
console.error('Failed to fetch visited adventures');
return redirect(302, '/login');
} else {
2024-07-11 19:27:03 -04:00
let res = await initialFetch.json();
let visited = res.results as Adventure[];
next = res.next;
previous = res.previous;
count = res.count;
2024-07-11 15:37:04 -04:00
adventures = [...adventures, ...visited];
}
2024-07-11 19:27:03 -04:00
return {
props: {
adventures,
next,
2024-07-11 20:09:55 -04:00
previous,
count
2024-07-11 15:37:04 -04:00
}
2024-07-11 19:27:03 -04:00
};
2024-07-11 09:33:52 -04:00
}
}) satisfies PageServerLoad;
2024-07-08 11:44:39 -04:00
export const actions: Actions = {
create: async (event) => {
const formData = await event.request.formData();
const type = formData.get('type') as string;
const name = formData.get('name') as string;
const location = formData.get('location') as string | null;
let date = (formData.get('date') as string | null) ?? null;
const description = formData.get('description') as string | null;
const activity_types = formData.get('activity_types')
? (formData.get('activity_types') as string).split(',')
: null;
const rating = formData.get('rating') ? Number(formData.get('rating')) : null;
let link = formData.get('link') as string | null;
let latitude = formData.get('latitude') as string | null;
let longitude = formData.get('longitude') as string | null;
2024-07-27 15:41:26 -04:00
let collection = formData.get('collection') as string | null;
2024-07-08 11:44:39 -04:00
// check if latitude and longitude are valid
if (latitude && longitude) {
if (isNaN(Number(latitude)) || isNaN(Number(longitude))) {
return {
status: 400,
body: { error: 'Invalid latitude or longitude' }
};
}
}
// round latitude and longitude to 6 decimal places
if (latitude) {
latitude = Number(latitude).toFixed(6);
}
if (longitude) {
longitude = Number(longitude).toFixed(6);
}
const image = formData.get('image') as File;
if (!type || !name) {
return {
status: 400,
body: { error: 'Missing required fields' }
};
}
if (date == null || date == '') {
date = null;
}
if (link) {
link = checkLink(link);
}
const formDataToSend = new FormData();
formDataToSend.append('type', type);
formDataToSend.append('name', name);
formDataToSend.append('location', location || '');
formDataToSend.append('date', date || '');
formDataToSend.append('description', description || '');
formDataToSend.append('latitude', latitude || '');
formDataToSend.append('longitude', longitude || '');
2024-07-29 22:14:42 -04:00
if (!isNaN(Number(collection))) {
if (collection !== null) {
formDataToSend.append('collection', collection);
}
}
2024-07-08 11:44:39 -04:00
if (activity_types) {
// Filter out empty and duplicate activity types, then trim each activity type
const cleanedActivityTypes = Array.from(
new Set(
activity_types
.map((activity_type) => activity_type.trim())
.filter((activity_type) => activity_type !== '' && activity_type !== ',')
)
);
// Append each cleaned activity type to formDataToSend
cleanedActivityTypes.forEach((activity_type) => {
formDataToSend.append('activity_types', activity_type);
});
}
formDataToSend.append('rating', rating ? rating.toString() : '');
formDataToSend.append('link', link || '');
formDataToSend.append('image', image);
2024-07-29 22:14:42 -04:00
// log each key-value pair in the FormData
for (let pair of formDataToSend.entries()) {
console.log(pair[0] + ', ' + pair[1]);
}
2024-07-08 11:44:39 -04:00
let auth = event.cookies.get('auth');
if (!auth) {
const refresh = event.cookies.get('refresh');
if (!refresh) {
return {
status: 401,
body: { message: 'Unauthorized' }
};
}
let res = await tryRefreshToken(refresh);
if (res) {
auth = res;
event.cookies.set('auth', auth, {
httpOnly: true,
sameSite: 'lax',
expires: new Date(Date.now() + 60 * 60 * 1000), // 60 minutes
path: '/'
});
} else {
return {
status: 401,
body: { message: 'Unauthorized' }
};
}
}
if (!auth) {
return {
status: 401,
body: { message: 'Unauthorized' }
};
}
const csrfToken = await fetchCSRFToken();
if (!csrfToken) {
return {
status: 500,
body: { message: 'Failed to fetch CSRF token' }
};
}
const res = await fetch(`${serverEndpoint}/api/adventures/`, {
method: 'POST',
headers: {
'X-CSRFToken': csrfToken,
Cookie: auth
},
body: formDataToSend
});
let new_id = await res.json();
if (!res.ok) {
const errorBody = await res.json();
return {
status: res.status,
body: { error: errorBody }
};
}
let id = new_id.id;
let user_id = new_id.user_id;
let image_url = new_id.image;
let link_url = new_id.link;
return { id, user_id, image_url, link };
},
edit: async (event) => {
const formData = await event.request.formData();
const adventureId = formData.get('adventureId') as string;
const type = formData.get('type') as string;
const name = formData.get('name') as string;
const location = formData.get('location') as string | null;
let date = (formData.get('date') as string | null) ?? null;
const description = formData.get('description') as string | null;
let activity_types = formData.get('activity_types')
? (formData.get('activity_types') as string).split(',')
: null;
const rating = formData.get('rating') ? Number(formData.get('rating')) : null;
let link = formData.get('link') as string | null;
let latitude = formData.get('latitude') as string | null;
let longitude = formData.get('longitude') as string | null;
2024-07-09 16:48:52 -04:00
let is_public = formData.get('is_public') as string | null | boolean;
if (is_public) {
is_public = true;
} else {
is_public = false;
}
2024-07-08 11:44:39 -04:00
// check if latitude and longitude are valid
if (latitude && longitude) {
if (isNaN(Number(latitude)) || isNaN(Number(longitude))) {
return {
status: 400,
body: { error: 'Invalid latitude or longitude' }
};
}
}
// round latitude and longitude to 6 decimal places
if (latitude) {
latitude = Number(latitude).toFixed(6);
}
if (longitude) {
longitude = Number(longitude).toFixed(6);
}
const image = formData.get('image') as File;
2024-07-11 15:37:04 -04:00
// console.log(activity_types);
2024-07-08 11:44:39 -04:00
if (!type || !name) {
return {
status: 400,
body: { error: 'Missing required fields' }
};
}
if (date == null || date == '') {
date = null;
}
if (link) {
link = checkLink(link);
}
const formDataToSend = new FormData();
formDataToSend.append('type', type);
formDataToSend.append('name', name);
formDataToSend.append('location', location || '');
formDataToSend.append('date', date || '');
formDataToSend.append('description', description || '');
formDataToSend.append('latitude', latitude || '');
formDataToSend.append('longitude', longitude || '');
2024-07-09 16:48:52 -04:00
formDataToSend.append('is_public', is_public.toString());
2024-07-31 18:27:06 -04:00
let csrfToken = await fetchCSRFToken();
2024-07-08 11:44:39 -04:00
if (activity_types) {
// Filter out empty and duplicate activity types, then trim each activity type
const cleanedActivityTypes = Array.from(
new Set(
activity_types
.map((activity_type) => activity_type.trim())
.filter((activity_type) => activity_type !== '' && activity_type !== ',')
)
);
// Append each cleaned activity type to formDataToSend
cleanedActivityTypes.forEach((activity_type) => {
formDataToSend.append('activity_types', activity_type);
});
2024-07-31 18:27:06 -04:00
} else {
let res = await fetch(`${serverEndpoint}/api/adventures/${adventureId}/`, {
method: 'PATCH',
headers: {
Cookie: `${event.cookies.get('auth')}`,
'X-CSRFToken': csrfToken,
'Content-Type': 'application/json'
},
body: JSON.stringify({ activity_types: [] })
});
if (!res.ok) {
const errorBody = await res.json();
return {
status: res.status,
body: { error: errorBody }
};
}
2024-07-08 11:44:39 -04:00
}
formDataToSend.append('rating', rating ? rating.toString() : '');
formDataToSend.append('link', link || '');
if (image && image.size > 0) {
formDataToSend.append('image', image);
}
let auth = event.cookies.get('auth');
if (!auth) {
const refresh = event.cookies.get('refresh');
if (!refresh) {
return {
status: 401,
body: { message: 'Unauthorized' }
};
}
let res = await tryRefreshToken(refresh);
if (res) {
auth = res;
event.cookies.set('auth', auth, {
httpOnly: true,
sameSite: 'lax',
expires: new Date(Date.now() + 60 * 60 * 1000), // 60 minutes
path: '/'
});
} else {
return {
status: 401,
body: { message: 'Unauthorized' }
};
}
}
if (!auth) {
return {
status: 401,
body: { message: 'Unauthorized' }
};
}
if (!csrfToken) {
return {
status: 500,
body: { message: 'Failed to fetch CSRF token' }
};
}
const res = await fetch(`${serverEndpoint}/api/adventures/${adventureId}/`, {
method: 'PATCH',
headers: {
'X-CSRFToken': csrfToken,
Cookie: auth
},
body: formDataToSend
});
if (!res.ok) {
const errorBody = await res.json();
return {
status: res.status,
body: { error: errorBody }
};
}
let adventure = await res.json();
let image_url = adventure.image;
let link_url = adventure.link;
return { image_url, link_url };
2024-07-11 09:33:52 -04:00
},
get: async (event) => {
2024-07-11 15:37:04 -04:00
if (!event.locals.user) {
}
2024-07-11 09:33:52 -04:00
2024-07-11 15:37:04 -04:00
const formData = await event.request.formData();
const visited = formData.get('visited');
const planned = formData.get('planned');
2024-07-11 09:33:52 -04:00
2024-07-15 12:09:20 -04:00
let include_collections = formData.get('include_collections') as string;
if (include_collections) {
include_collections = 'true';
} else {
include_collections = 'false';
}
2024-07-15 18:01:49 -04:00
2024-07-13 10:42:42 -04:00
const order_direction = formData.get('order_direction') as string;
const order_by = formData.get('order_by') as string;
console.log(order_direction, order_by);
2024-07-11 15:37:04 -04:00
let adventures: Adventure[] = [];
if (!event.locals.user) {
2024-07-11 09:33:52 -04:00
return {
2024-07-11 15:37:04 -04:00
status: 401,
body: { message: 'Unauthorized' }
2024-07-11 09:33:52 -04:00
};
}
2024-07-11 19:27:03 -04:00
let filterString = '';
2024-07-11 15:37:04 -04:00
if (visited) {
2024-07-11 19:27:03 -04:00
filterString += 'visited';
2024-07-11 15:37:04 -04:00
}
if (planned) {
2024-07-11 19:27:03 -04:00
if (filterString) {
filterString += ',';
2024-07-11 15:37:04 -04:00
}
2024-07-11 19:27:03 -04:00
filterString += 'planned';
2024-07-11 15:37:04 -04:00
}
2024-07-11 19:27:03 -04:00
if (!filterString) {
filterString = '';
}
let next = null;
let previous = null;
let count = 0;
2024-07-11 20:09:55 -04:00
console.log(filterString);
2024-07-11 19:27:03 -04:00
let visitedFetch = await fetch(
2024-07-15 12:09:20 -04:00
`${serverEndpoint}/api/adventures/filtered?types=${filterString}&order_by=${order_by}&order_direction=${order_direction}&include_collections=${include_collections}`,
2024-07-11 19:27:03 -04:00
{
2024-07-11 15:37:04 -04:00
headers: {
Cookie: `${event.cookies.get('auth')}`
}
}
2024-07-11 19:27:03 -04:00
);
if (!visitedFetch.ok) {
console.error('Failed to fetch visited adventures');
return redirect(302, '/login');
} else {
let res = await visitedFetch.json();
let visited = res.results as Adventure[];
next = res.next;
previous = res.previous;
count = res.count;
adventures = [...adventures, ...visited];
2024-07-11 20:09:55 -04:00
console.log(next, previous, count);
2024-07-11 15:37:04 -04:00
}
2024-07-11 19:27:03 -04:00
return {
adventures,
next,
previous,
count
};
2024-07-11 20:09:55 -04:00
},
changePage: async (event) => {
const formData = await event.request.formData();
2024-07-12 09:11:00 -04:00
const next = formData.get('next') as string;
const previous = formData.get('previous') as string;
const page = formData.get('page') as string;
2024-07-11 20:09:55 -04:00
2024-07-12 09:50:10 -04:00
if (!event.locals.user) {
2024-07-12 09:36:53 -04:00
return {
status: 401,
body: { message: 'Unauthorized' }
};
}
2024-07-12 09:11:00 -04:00
if (!page) {
2024-07-11 20:09:55 -04:00
return {
status: 400,
2024-07-12 09:11:00 -04:00
body: { error: 'Missing required fields' }
2024-07-11 20:09:55 -04:00
};
}
2024-07-12 09:50:10 -04:00
// Start with the provided URL or default to the filtered adventures endpoint
let url: string = next || previous || '/api/adventures/filtered';
2024-07-12 09:50:10 -04:00
2024-07-12 10:07:35 -04:00
// Extract the path starting from '/api/adventures'
const apiIndex = url.indexOf('/api/adventures');
2024-07-12 09:50:10 -04:00
if (apiIndex !== -1) {
url = url.slice(apiIndex);
2024-07-12 09:50:10 -04:00
} else {
url = '/api/adventures/filtered';
2024-07-12 09:50:10 -04:00
}
2024-07-12 09:11:00 -04:00
// Replace or add the page number in the URL
if (url.includes('page=')) {
url = url.replace(/page=\d+/, `page=${page}`);
} else {
// If 'page=' is not in the URL, add it
url += url.includes('?') ? '&' : '?';
url += `page=${page}`;
}
const fullUrl = `${serverEndpoint}${url}`;
console.log(fullUrl);
console.log(serverEndpoint);
2024-07-11 20:09:55 -04:00
try {
const response = await fetch(fullUrl, {
2024-07-11 20:09:55 -04:00
headers: {
'Content-Type': 'application/json',
Cookie: `${event.cookies.get('auth')}`
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
let adventures = data.results as Adventure[];
let next = data.next;
let previous = data.previous;
let count = data.count;
return {
status: 200,
body: {
adventures,
next,
previous,
2024-07-13 10:28:45 -04:00
count,
page
2024-07-11 20:09:55 -04:00
}
};
} catch (error) {
console.error('Error fetching data:', error);
return {
status: 500,
body: { error: 'Failed to fetch data' }
};
}
2024-07-15 12:09:20 -04:00
},
all: async (event) => {
if (!event.locals.user) {
return {
status: 401,
body: { message: 'Unauthorized' }
};
}
const formData = await event.request.formData();
let include_collections = formData.get('include_collections') as string;
if (include_collections !== 'true' && include_collections !== 'false') {
include_collections = 'false';
}
let adventures: Adventure[] = [];
let visitedFetch = await fetch(
`${serverEndpoint}/api/adventures/all/?include_collections=${include_collections}`,
{
headers: {
Cookie: `${event.cookies.get('auth')}`,
'Content-Type': 'application/json'
}
}
);
if (!visitedFetch.ok) {
console.error('Failed to fetch all adventures');
return redirect(302, '/login');
} else {
console.log('Fetched all adventures');
let res = await visitedFetch.json();
console.log(res);
adventures = res as Adventure[];
}
return {
adventures
};
2024-07-08 11:44:39 -04:00
}
};