Skip to content

Commit

Permalink
feat: show loading state for back side
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Sep 25, 2022
1 parent 6d01abf commit d2000c9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/pages/Learn/helpers/getBackSide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import { useEffect, useState } from 'react';
import Backend from '../../../lib/backend';

type BackSide = {
data: string;
backSide: string;
loading: boolean;
};

export const getBackSide = (id: string | undefined) => {
const backend = new Backend();
export const getBackSide = (id: string | undefined): BackSide => {
const [backSide, setBackSide] = useState('loading');
const [loading, setLoading] = useState(false);
const backend = new Backend();

useEffect(() => {
setLoading(true);
if (id) {
backend.renderBlock(id).then((response: unknown) => {
const res = response as BackSide;
const res = response as { data: string };
setBackSide(res.data);
setLoading(false);
});
}
}, [id]);
return backSide;
return { loading, backSide };
};
9 changes: 6 additions & 3 deletions src/pages/Learn/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function LearnPage({ setError }: Props) {
const location = useLocation();

const block = children ? children[index - 1] : null;
const backSide = getBackSide(block?.id);
const { loading, backSide } = getBackSide(block?.id);
// Load parent page based on id
useEffect(() => {
setParentId(location.pathname.split('/').at(-1) || null);
Expand All @@ -32,8 +32,6 @@ function LearnPage({ setError }: Props) {
return <LoadingPage />;
}

console.log('block', block);

return (
<PageContainer>
<h1>Learn</h1>
Expand Down Expand Up @@ -69,6 +67,11 @@ function LearnPage({ setError }: Props) {
<span style={{ fontSize: '11px' }}>
{index + 1} /{children.length}
</span>
{loading && (
<button type="button" className="is-loading button">
loading
</button>
)}
<button
type="button"
onClick={() => setIndex(Math.max(index - 1, 0))}
Expand Down

0 comments on commit d2000c9

Please sign in to comment.