Skip to content

Commit

Permalink
feature: improve UI
Browse files Browse the repository at this point in the history
  • Loading branch information
wilcorrea committed Apr 9, 2024
1 parent 646fbee commit 4415d50
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
3 changes: 3 additions & 0 deletions src/view/components/game/GamePlaySession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export function GamePlaySession ({ game, onStart, onFinish }: GamePlaySessionPro
answerQuestion={answerQuestion}
nextQuestion={nextQuestion}
/>
<div className="pt-1">
<small>{game.questions.length - questions.length} / {game.questions.length}</small>
</div>
</Case>
<Case value={GamePlaySessionStatus.FINISHED}>
<Warning />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function GamePlaySessionQuestionUnanswered (props: GameQuestionOptionsPro

return (
<div className="GamePlaySessionQuestionUnanswered">
<div className="card pb-2">
<div className="card mb-2">
<div className="card-header">
<h4>
<Markdown
Expand Down Expand Up @@ -72,6 +72,7 @@ export function GamePlaySessionQuestionUnanswered (props: GameQuestionOptionsPro
</span>
</div>
</div>

<div className="d-grid">
<button
className="btn btn-lg btn-primary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { ReactNode, useEffect, useRef, useState } from 'react'

export type HydrateElementProps = {
children: ReactNode | ReactNode[]
status: HydratedStatus
status: AsyncStatus
}

export type HydrateProps = {
hidrate: () => Promise<any>
using: () => Promise<any>
onResolve: (data: any) => void
onReject?: (data: any) => void
children: ReactNode | ReactNode[]
}

// eslint-disable-next-line react-refresh/only-export-components
export enum HydratedStatus {
export enum AsyncStatus {
Pending = 'Pending',
Resolved = 'Resolved',
Rejected = 'Rejected'
Expand All @@ -24,9 +24,9 @@ export function On ({ children }: HydrateElementProps) {
return children
}

export function Hydrated ({ hidrate, onResolve, onReject, children }: HydrateProps) {
export function Async ({ using, onResolve, onReject, children }: HydrateProps) {
const fetched = useRef(false)
const [status, setStatus] = useState(HydratedStatus.Pending)
const [status, setStatus] = useState(AsyncStatus.Pending)

useEffect(() => {
if (fetched.current) {
Expand All @@ -35,19 +35,19 @@ export function Hydrated ({ hidrate, onResolve, onReject, children }: HydratePro
const fetchData = async () => {
fetched.current = true
try {
const data = await hidrate()
const data = await using()
onResolve(data)
setStatus(HydratedStatus.Resolved)
setStatus(AsyncStatus.Resolved)
} catch (e) {
console.error(e)
setStatus(HydratedStatus.Rejected)
setStatus(AsyncStatus.Rejected)
if (onReject) {
onReject(e)
}
}
}
fetchData()
}, [fetched, hidrate, onReject, onResolve])
}, [fetched, using, onReject, onResolve])

if (!children) {
return null
Expand Down
14 changes: 7 additions & 7 deletions src/view/pages/game/GamePlayPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import GameRepository from '../../../app/Domain/Game/GameRepository.ts'
import Game from '../../../app/Domain/Game/Game.ts'

import { useApp } from '../../hooks'
import { Hydrated, HydratedStatus, On } from '../../components/general/Hydrated.tsx'
import { Async, AsyncStatus, On } from '../../components/general/Async.tsx'
import { Loading } from '../../components/general/Loading.tsx'
import { Warning } from '../../components/general/Alert.tsx'
import { GamePlaySession } from '../../components/game/GamePlaySession.tsx'
Expand Down Expand Up @@ -42,30 +42,30 @@ export function GamePlayPage () {
}

return (
<Hydrated
hidrate={() => gameRepository.findById(gameId)}
<Async
using={() => gameRepository.findById(gameId)}
onResolve={onResolve}
onReject={onReject}
>
<On status={HydratedStatus.Pending}>
<On status={AsyncStatus.Pending}>
<div className="py-3">
<Loading label={t('pending')} />
</div>
</On>
<On status={HydratedStatus.Resolved}>
<On status={AsyncStatus.Resolved}>
<GamePlaySession
game={game}
onFinish={() => navigate(`/game/${gameId}/end`)}
/>
</On>
<On status={HydratedStatus.Rejected}>
<On status={AsyncStatus.Rejected}>
<div className="py-2">
<Warning
strong={t('error')}
message={t('rejected')}
/>
</div>
</On>
</Hydrated>
</Async>
)
}
14 changes: 7 additions & 7 deletions src/view/pages/game/GameWelcomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import GameRepository from '../../../app/Domain/Game/GameRepository.ts'
import Game from '../../../app/Domain/Game/Game.ts'

import { useApp } from '../../hooks'
import { Hydrated, HydratedStatus, On } from '../../components/general/Hydrated.tsx'
import { Async, AsyncStatus, On } from '../../components/general/Async.tsx'
import { Loading } from '../../components/general/Loading.tsx'
import { Warning } from '../../components/general/Alert.tsx'

Expand All @@ -24,27 +24,27 @@ export function GameWelcomePage () {
<div className="py-3 px-4 rounded">
<h1 className="text-center">{t('title')}</h1>
<p className="text-center">{t('description')}</p>
<Hydrated
hidrate={() => gameRepository.paginate(1, 10)}
<Async
using={() => gameRepository.paginate(1, 10)}
onResolve={setGames}
>
<On status={HydratedStatus.Pending}>
<On status={AsyncStatus.Pending}>
<div className="py-3">
<Loading label={t('pending')} />
</div>
</On>
<On status={HydratedStatus.Resolved}>
<On status={AsyncStatus.Resolved}>
<GameList games={games} />
</On>
<On status={HydratedStatus.Rejected}>
<On status={AsyncStatus.Rejected}>
<div className="py-2">
<Warning
strong={t('error')}
message={t('rejected')}
/>
</div>
</On>
</Hydrated>
</Async>
</div>
)
}

0 comments on commit 4415d50

Please sign in to comment.