Skip to content

Commit

Permalink
implement alert skeleton for loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicarrojado committed Aug 19, 2023
1 parent c5d831e commit 20757e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
13 changes: 13 additions & 0 deletions components/alertSkeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';

export default function AlertSkeleton() {
return (
<div role="status">
<div className="animate-pulse">
<div className="w-11/12 h-5 bg-indigo-200 rounded-md mb-2 sm:h-6 sm:mb-0"></div>
<div className="w-4/5 h-5 bg-indigo-200 rounded-md sm:hidden"></div>
</div>
<span className="sr-only">Loading...</span>
</div>
);
}
23 changes: 12 additions & 11 deletions components/lastAvailableSlotsInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect } from 'react';
import cn from 'classnames';
import Alert from './alert';
import AlertSkeleton from './alertSkeleton';
import { useGetLastAvailableSlotsDate } from '@/lib/api-hooks';
import { FetchState } from '@/lib/types';

Expand All @@ -14,16 +14,17 @@ export default function LastAvailableSlotsInfo() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return fetchState !== FetchState.ERROR ? (
if (fetchState === FetchState.ERROR) {
return null;
}

return (
<Alert>
<div
className={cn(
'transition-opacity duration-300',
fetchState !== FetchState.SUCCESS ? 'opacity-0' : 'opacity-100'
)}
>
Last available slots were spotted on {lastAvailableSlotsDate}.
</div>
{fetchState !== FetchState.SUCCESS ? (
<AlertSkeleton />
) : (
`Last available slots were spotted on ${lastAvailableSlotsDate}.`
)}
</Alert>
) : null;
);
}
4 changes: 1 addition & 3 deletions lib/api-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ export function useUnsubscribe() {

export function useGetLastAvailableSlotsDate() {
const [fetchState, setFetchState] = useState(FetchState.DEFAULT);
const [lastAvailableSlotsDate, setLastAvailableSlotsDate] = useState(
'----------------------'
);
const [lastAvailableSlotsDate, setLastAvailableSlotsDate] = useState('');
const getLastAvailableSlotsDate = async () => {
try {
setFetchState(FetchState.LOADING);
Expand Down

0 comments on commit 20757e9

Please sign in to comment.