Skip to content

Commit

Permalink
fix: health checks and prevent excess fetching when no server connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Lombardoc4 committed Apr 2, 2024
1 parent c423472 commit 6947ab3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/app/api/fetchEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { atomEffect } from 'jotai-effect';
import {
EventListState,
SeasonState,
serverConnectedState,
serverErrorState,
} from '@/state-mgmt/atoms';

Expand All @@ -18,14 +19,21 @@ export const fetchEventList = atomEffect(
const season = get(SeasonState);
const params = season && `?year=${season}`;

// Prevent fetch if not connected
const connected = get(serverConnectedState);
if (!connected) {
return;
}

// Fetch event list from api
fetchAPI('schedule' + params).then(
(res: DataConfigSchema['schedule'] | ServerErrorResponse) => {
const schedule = res as DataConfigSchema['schedule'];
const error = res as ServerErrorResponse;

// *** If errors specific prop, detail, update serverErrorState
if (error.detail) {
if (!schedule) return;
if (error && error.detail) {
set(serverErrorState, error.detail[0].msg);
return;
}
Expand Down
5 changes: 3 additions & 2 deletions src/app/api/fetchHealth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ export const fetchHealth = atomEffect(

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

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

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

0 comments on commit 6947ab3

Please sign in to comment.