collections v2
This commit is contained in:
@@ -362,6 +362,13 @@ export const actions: Actions = {
|
||||
const visited = formData.get('visited');
|
||||
const planned = formData.get('planned');
|
||||
|
||||
let include_collections = formData.get('include_collections') as string;
|
||||
|
||||
if (include_collections) {
|
||||
include_collections = 'true';
|
||||
} else {
|
||||
include_collections = 'false';
|
||||
}
|
||||
const order_direction = formData.get('order_direction') as string;
|
||||
const order_by = formData.get('order_by') as string;
|
||||
|
||||
@@ -397,7 +404,7 @@ export const actions: Actions = {
|
||||
console.log(filterString);
|
||||
|
||||
let visitedFetch = await fetch(
|
||||
`${serverEndpoint}/api/adventures/filtered?types=${filterString}&order_by=${order_by}&order_direction=${order_direction}`,
|
||||
`${serverEndpoint}/api/adventures/filtered?types=${filterString}&order_by=${order_by}&order_direction=${order_direction}&include_collections=${include_collections}`,
|
||||
{
|
||||
headers: {
|
||||
Cookie: `${event.cookies.get('auth')}`
|
||||
@@ -502,5 +509,46 @@ export const actions: Actions = {
|
||||
body: { error: 'Failed to fetch data' }
|
||||
};
|
||||
}
|
||||
},
|
||||
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
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -281,6 +281,16 @@
|
||||
id="rating"
|
||||
class="radio radio-primary"
|
||||
/>
|
||||
<br />
|
||||
<label class="label cursor-pointer">
|
||||
<span class="label-text">Include Collection Adventures</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
name="include_collections"
|
||||
id="include_collections"
|
||||
class="checkbox checkbox-primary"
|
||||
/>
|
||||
</label>
|
||||
<button type="submit" class="btn btn-primary mt-4">Filter</button>
|
||||
</form>
|
||||
<div class="divider"></div>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
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';
|
||||
@@ -89,5 +88,74 @@ export const actions: Actions = {
|
||||
status: 204
|
||||
};
|
||||
}
|
||||
},
|
||||
addToCollection: async (event) => {
|
||||
const id = event.params as { id: string };
|
||||
const adventureId = id.id;
|
||||
|
||||
const formData = await event.request.formData();
|
||||
const trip_id = formData.get('collection_id');
|
||||
|
||||
if (!trip_id) {
|
||||
return {
|
||||
status: 400,
|
||||
error: { message: 'Missing collection id' }
|
||||
};
|
||||
}
|
||||
|
||||
if (!event.locals.user) {
|
||||
const refresh = event.cookies.get('refresh');
|
||||
let auth = event.cookies.get('auth');
|
||||
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 (!adventureId) {
|
||||
return {
|
||||
status: 400,
|
||||
error: new Error('Bad request')
|
||||
};
|
||||
}
|
||||
|
||||
let trip_id_number: number = parseInt(trip_id as string);
|
||||
|
||||
let res = await fetch(`${serverEndpoint}/api/adventures/${event.params.id}/`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
Cookie: `${event.cookies.get('auth')}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ collection: trip_id_number })
|
||||
});
|
||||
let res2 = await res.json();
|
||||
console.log(res2);
|
||||
if (!res.ok) {
|
||||
return {
|
||||
status: res.status,
|
||||
error: new Error('Failed to delete adventure')
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: 204
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user