Skip to content

Commit

Permalink
Fix a frontend null reference error (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamlogand authored Mar 26, 2024
1 parent eed0c30 commit e7851e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
6 changes: 3 additions & 3 deletions frontend/components/ActionsArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ const ActionsArea = () => {
const latestTurn = turns.asArray.sort((a, b) => a.index - b.index)[
turns.allIds.length - 1
]
const latestPhases = phases.asArray.filter((p) => p.turn === latestTurn.id)
const latestPhase = latestPhases.sort((a, b) => a.index - b.index)[
const latestPhases = (latestTurn) ? phases.asArray.filter((p) => p.turn === latestTurn.id) : []
const latestPhase = (latestPhases.length > 0) ? latestPhases.sort((a, b) => a.index - b.index)[
latestPhases.length - 1
]
] : null

if (thisFactionsPendingActions) {
const requiredAction = thisFactionsPendingActions.asArray.find(
Expand Down
27 changes: 16 additions & 11 deletions frontend/components/MetaSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ const MetaSection = () => {
]

// Get the latest phase
const latestPhases = phases.asArray.filter((p) => p.turn === latestTurn.id)
const latestPhase = latestPhases.sort((a, b) => a.index - b.index)[
latestPhases.length - 1
]
const latestPhases = latestTurn
? phases.asArray.filter((p) => p.turn === latestTurn.id)
: []
const latestPhase =
latestPhases.length > 0
? latestPhases.sort((a, b) => a.index - b.index)[latestPhases.length - 1]
: null

const getPhaseTerm = (phase: string) => {
switch (phase) {
Expand Down Expand Up @@ -157,13 +160,15 @@ const MetaSection = () => {
height={20}
className="align-middle mt-[-4px] mb-[-2px] mr-1"
/>
{game.end_date ? (
<b>Game over</b>
) : (
<span>
Turn {latestTurn?.index},{" "}
{latestPhase && getPhaseTerm(latestPhase.name)}
</span>
{latestTurn && latestPhase && (
game.end_date ? (
<b>Game over</b>
) : (
<span>
Turn {latestTurn?.index},{" "}
{latestPhase && getPhaseTerm(latestPhase.name)}
</span>
)
)}
</span>
</div>
Expand Down

0 comments on commit e7851e6

Please sign in to comment.