blog/frontend/src/routes/dashboard/+layout.svelte
SquidSpirit 7670996aa1
Some checks failed
Frontend CI / build (push) Failing after 53s
feat: add custom ErrorPage component for improved error handling
2025-10-13 10:51:55 +08:00

47 lines
1.7 KiB
Svelte

<script lang="ts">
import type { AuthApiService } from '$lib/auth/adapter/gateway/authApiService';
import { AuthRepositoryImpl } from '$lib/auth/adapter/gateway/authRepositoryImpl';
import { AuthBloc, AuthEventType } from '$lib/auth/adapter/presenter/authBloc';
import type { AuthRepository } from '$lib/auth/application/gateway/authRepository';
import { GetCurrentUserUseCase } from '$lib/auth/application/useCase/getCurrentUserUseCase';
import { AuthApiServiceImpl } from '$lib/auth/framework/api/authApiServiceImpl';
import { onMount, setContext } from 'svelte';
import type { LayoutProps } from './$types';
import { StatusType } from '$lib/common/adapter/presenter/asyncState';
import ErrorPage from '$lib/common/framework/ui/ErrorPage.svelte';
const { children }: LayoutProps = $props();
const authApiService: AuthApiService = new AuthApiServiceImpl(fetch);
const authRepository: AuthRepository = new AuthRepositoryImpl(authApiService);
const getcurrentUserUseCase = new GetCurrentUserUseCase(authRepository);
const authBloc = new AuthBloc(getcurrentUserUseCase);
setContext(AuthBloc.name, authBloc);
const authState = $derived($authBloc);
onMount(() => authBloc.dispatch({ event: AuthEventType.CurrentUserLoadedEvent }));
const isLoading = $derived.by(
() => authState.status === StatusType.Loading || authState.status === StatusType.Idle
);
const hasError = $derived.by(() => {
if (authState.status === StatusType.Error) {
return true;
}
if (authState.status === StatusType.Success && !authState.data.isAuthenticated) {
return true;
}
return false;
});
</script>
{#if isLoading}
<div></div>
{:else if hasError}
<ErrorPage />
{:else}
{@render children()}
{/if}