Skip to content

Commit

Permalink
Merge pull request #12869 from guardian/doml/hide-match-tab-no-data
Browse files Browse the repository at this point in the history
Hide Match Tab when no data
  • Loading branch information
domlander authored Nov 21, 2024
2 parents cf24182 + 1b0ef5c commit 42cfef9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
42 changes: 40 additions & 2 deletions dotcom-rendering/src/components/GetMatchNav.importable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { css } from '@emotion/react';
import { isObject } from '@guardian/libs';
import { from } from '@guardian/source/foundations';
import type { SWRConfiguration } from 'swr';
import { ArticleDesign, type ArticleFormat } from '../lib/articleFormat';
Expand Down Expand Up @@ -36,6 +37,36 @@ const fallbackData = {
awayTeam: fallbackTeam,
} satisfies MatchData;

const validateMatchData = (data: unknown): data is MatchData => {
if (!isObject(data)) {
return false;
}

const homeTeam = data.homeTeam;
if (
!isObject(homeTeam) ||
typeof homeTeam.name !== 'string' ||
homeTeam.name.length === 0 ||
typeof homeTeam.score !== 'number' ||
isNaN(homeTeam.score)
) {
return false;
}

const awayTeam = data.awayTeam;
if (
!isObject(awayTeam) ||
typeof awayTeam.name !== 'string' ||
awayTeam.name.length === 0 ||
typeof awayTeam.score !== 'number' ||
isNaN(awayTeam.score)
) {
return false;
}

return true;
};

/**
* Wrapper around `MatchNav` with loading and fallback.
*
Expand Down Expand Up @@ -97,7 +128,14 @@ export const GetMatchNav = ({
);
}
}
if (data) {

if (!data) {
// this should never happen because we pass fallback data to SWR
return null;
}

const isDataValid = validateMatchData(data);
if (isDataValid) {
return (
<MatchNav
homeTeam={data.homeTeam}
Expand All @@ -107,6 +145,6 @@ export const GetMatchNav = ({
);
}

// this should never happen because we pass fallback data to SWR
// Invalid data indicates that we do not have the data for this fixture, likely because the match is old.
return null;
};
4 changes: 2 additions & 2 deletions dotcom-rendering/src/layouts/StandardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const StandardGrid = ({
grid-template-areas:
'title border matchNav . right-column'
'title border matchtabs . right-column'
'. border headline . right-column'
'title border headline . right-column'
'. border standfirst . right-column'
'meta border media . right-column'
'meta border body . right-column'
Expand Down Expand Up @@ -152,7 +152,7 @@ const StandardGrid = ({
grid-template-areas:
'title border matchNav right-column'
'title border matchtabs right-column'
'. border headline right-column'
'title border headline right-column'
'. border standfirst right-column'
'meta border media right-column'
'meta border body right-column'
Expand Down

0 comments on commit 42cfef9

Please sign in to comment.