// src/routes/(app)/groups/[groupId]/+page.ts import { error } from '@sveltejs/kit'; import { apiClient, ApiClientError } from '$lib/apiClient'; import type { GroupPublic } from '$lib/schemas/group'; import type { PageLoad } from './$types'; // SvelteKit's type for load functions // Define the expected shape of the data returned export interface GroupDetailPageLoadData { group: GroupPublic; // The fetched group data } export const load: PageLoad = async ({ params, fetch }) => { const groupId = params.groupId; // Get groupId from the URL parameter console.log(`Group Detail page load: Fetching data for group ID: ${groupId}`); // Basic validation (optional but good) if (!groupId || isNaN(parseInt(groupId, 10))) { throw error(400, 'Invalid Group ID'); // Use SvelteKit's error helper } try { // Fetch the specific group details using the apiClient // The backend endpoint GET /api/v1/groups/{group_id} should include members const groupData = await apiClient.get(`/v1/groups/${groupId}`); if (!groupData) { // Should not happen if API call was successful, but check defensively throw error(404, 'Group not found'); } console.log('Group Detail page load: Data fetched successfully', groupData); return { group: groupData }; } catch (err) { console.error(`Group Detail page load: Failed to fetch group ${groupId}:`, err); if (err instanceof ApiClientError) { if (err.status === 404) { throw error(404, 'Group not found'); } if (err.status === 403) { // User is authenticated (layout guard passed) but not member of this group throw error(403, 'Forbidden: You are not a member of this group'); } // For other API errors (like 500) throw error(err.status || 500, `API Error: ${err.message}`); } else if (err instanceof Error) { // Network or other client errors throw error(500, `Failed to load group data: ${err.message}`); } else { // Unknown error throw error(500, 'An unexpected error occurred while loading group data.'); } } };