Skip to content

Commit

Permalink
chore: update server health check
Browse files Browse the repository at this point in the history
  • Loading branch information
Lombardoc4 committed Apr 1, 2024
1 parent f00dd68 commit 03a449c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
35 changes: 35 additions & 0 deletions src/app/api/fetchHealth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { atomEffect } from 'jotai-effect';

import { serverConnectedState, serverErrorState } from '@/state-mgmt/atoms';

import { fetchAPI } from './fetch';

// otherwise get the default schedule
export const fetchHealth = atomEffect(
(get, set) => {
const serverError = get(serverErrorState);
if (serverError) {
return;
}

// Fetch event list from api
fetchAPI('health').then((res: { status: string } | ServerErrorResponse) => {
const error = res as ServerErrorResponse;

if ('status' in res) {
set(serverConnectedState, true);
return;
}

// *** If errors specific prop, detail, update serverErrorState
if (error.detail) {
set(serverErrorState, error.detail[0].msg);
return;
}

set(serverErrorState, 'No connection');
});
},
// Dependencies:
// serverErrorState
);
10 changes: 1 addition & 9 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import './globals.css';
import { Footer } from '@/components/Footer';
import { TopNav } from '@/components/TopNav';

import { fetchAPI } from '../lib/helpers';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
Expand All @@ -22,15 +20,9 @@ export default async function RootLayout({
}: {
children: React.ReactNode;
}) {
const server = await fetchAPI('health');

return (
<html lang='en'>
<body
className={clsx('min-h-screen', inter.className, {
server: server,
})}
>
<body className={clsx('min-h-screen', inter.className)}>
<Provider>
<TopNav />
{children}
Expand Down
16 changes: 12 additions & 4 deletions src/components/Footer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { fetchAPI } from '@/lib/helpers';
'use client';

async function Footer() {
const serverStatus = await fetchAPI('health');
import { useAtom } from 'jotai';

import { fetchHealth } from '@/app/api/fetchHealth';
import { serverConnectedState, serverErrorState } from '@/state-mgmt/atoms';

function Footer() {
useAtom(fetchHealth);
const [serverError] = useAtom(serverErrorState);
const [serverStatus] = useAtom(serverConnectedState);

return (
<div className='container min-h-24'>
<p>Footer</p>
<p>
<b>Server Status:</b> {serverStatus.status || 'Offline'}
<b>Server Status:</b>{' '}
{serverError || (serverStatus ? 'Connected' : 'Connecting')}
</p>
</div>
);
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/state-mgmt/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const DriverStandingState = atom<DriverStandingSchema[]>([]);

// Server Error
export const serverErrorState = atom('');
export const serverConnectedState = atom(false);

export {
ConstructorListState,
Expand Down

0 comments on commit 03a449c

Please sign in to comment.