feat: Add country name to region page

This commit is contained in:
Sean Morley
2024-07-10 17:27:43 -04:00
parent 597e56ea62
commit bb1f2d92cf
8 changed files with 76 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
import type { Region, VisitedRegion } from '$lib/types';
import type { Country, Region, VisitedRegion } from '$lib/types';
import type { PageServerLoad } from './$types';
const endpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
@@ -9,6 +9,7 @@ export const load = (async (event) => {
let regions: Region[] = [];
let visitedRegions: VisitedRegion[] = [];
let country: Country;
let res = await fetch(`${endpoint}/api/${id}/regions/`, {
method: 'GET',
@@ -23,6 +24,11 @@ export const load = (async (event) => {
regions = (await res.json()) as Region[];
}
if (regions.length === 0) {
console.error('No regions found');
return { status: 404 };
}
res = await fetch(`${endpoint}/api/${id}/visits/`, {
method: 'GET',
headers: {
@@ -36,10 +42,24 @@ export const load = (async (event) => {
visitedRegions = (await res.json()) as VisitedRegion[];
}
res = await fetch(`${endpoint}/api/countries/${regions[0].country}/`, {
method: 'GET',
headers: {
Cookie: `${event.cookies.get('auth')}`
}
});
if (!res.ok) {
console.error('Failed to fetch country');
return { status: 500 };
} else {
country = (await res.json()) as Country;
}
return {
props: {
regions,
visitedRegions
visitedRegions,
country
}
};
}) satisfies PageServerLoad;