Skip to content

Commit

Permalink
Merge pull request #689 from UKHSA-Internal/COVP-169_add_headline_dat…
Browse files Browse the repository at this point in the history
…a_region_dependent

COVP-169 Add ability to show/hide headline numers for different areas.
  • Loading branch information
adebayoolabintan committed Oct 3, 2023
2 parents 38cf553 + 2cf0c23 commit 81e7d64
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
35 changes: 35 additions & 0 deletions src/common/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ParsedParams } from "./utils.types";
import type { RGB } from "components/MapTable/MapTable.types";
import { generateUrl } from "hooks/useApi";
import Path from "assets/paths.json";
import type IsIncludedTypeProps from 'components/Card/Card.types';


export const sort = (a, b) => {
Expand Down Expand Up @@ -192,6 +193,40 @@ export const getParamValueFor = (params: Array<ParsedParams>, keyName: string, d
}; // getParamValueFor


export const isAreaIncluded = ({ params, locationAware={} }: IsIncludedTypeProps): boolean => {
if ( !Object.keys(locationAware).length )
return true;

const
areaType = getParamValueFor(
params,
"areaType",
"overview"
).toLowerCase(),
areaName = getParamValueFor(
params,
"areaName",
"United Kingdom"
).toLowerCase(),
includedAreaType = locationAware?.included?.areaType ?? [],
includedAreaName = locationAware?.included?.areaName ?? [],
excludedAreaType = locationAware?.excluded?.areaType ?? [],
excludedAreaName = locationAware?.excluded?.areaName ?? [],
hasExclusion = excludedAreaType.length > 0 && !(excludedAreaName.length > 0),
isExcluded = (
excludedAreaType.map(value => value.toLowerCase()).indexOf(areaType) > -1 ||
excludedAreaName.map(value => value.toLowerCase()).indexOf(areaName) > -1
),
isIncluded = (
includedAreaType.map(value => value.toLowerCase()).indexOf(areaType) > -1 ||
includedAreaName.map(value => value.toLowerCase()).indexOf(areaName) > -1
);

return !hasExclusion ? isIncluded : !isExcluded

}; // isAreaIncluded


export const firstObjWithMax = ( arr: Array<{[string|number]: [string|number|null]}>, key: ([string|number]) => [string|number|null] ) => {

const maxValue = max(arr, key);
Expand Down
40 changes: 2 additions & 38 deletions src/components/Card/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React, { useState, useEffect, lazy, Suspense } from 'react';
import { Link } from "react-router-dom";

import { fieldToStructure, getParamValueFor, heading2id } from "common/utils";
import { fieldToStructure, heading2id, isAreaIncluded } from "common/utils";
import usePrevious from "hooks/usePrevious";
import TabLinkContainer from "components/TabLink";
import { Radio } from "components/GovUk";
Expand All @@ -27,7 +27,6 @@ import {
ShareRow
} from './Card.styles';

import type { IsIncludedTypeProps, Props } from './Card.types';
import type { ComponentType } from 'react';

import DownloadIcon from "assets/icon-download.svg";
Expand Down Expand Up @@ -152,41 +151,6 @@ const NoTabCard: ComponentType<*> = ({ cardType, ...props }) => {
}; // NoTabCards


const isIncluded = ({ params, locationAware={} }: IsIncludedTypeProps): boolean => {

if ( !Object.keys(locationAware).length )
return true;

const
areaType = getParamValueFor(
params,
"areaType",
"overview"
).toLowerCase(),
areaName = getParamValueFor(
params,
"areaName",
"United Kingdom"
).toLowerCase(),
includedAreaType = locationAware?.included?.areaType ?? [],
includedAreaName = locationAware?.included?.areaName ?? [],
excludedAreaType = locationAware?.excluded?.areaType ?? [],
excludedAreaName = locationAware?.excluded?.areaName ?? [],
hasExclusion = excludedAreaType.length > 0 && !(excludedAreaName.length > 0),
isExcluded = (
excludedAreaType.map(value => value.toLowerCase()).indexOf(areaType) > -1 ||
excludedAreaName.map(value => value.toLowerCase()).indexOf(areaName) > -1
),
isIncluded = (
includedAreaType.map(value => value.toLowerCase()).indexOf(areaType) > -1 ||
includedAreaName.map(value => value.toLowerCase()).indexOf(areaName) > -1
);

return !hasExclusion ? isIncluded : !isExcluded

}; // isIncluded


const CardContent = ({ tabs: singleOptionTabs=null, cardType, download=[], params, options=null,
heading, fullWidth, abstract=null, ...props }) => {

Expand Down Expand Up @@ -284,7 +248,7 @@ const CardContent = ({ tabs: singleOptionTabs=null, cardType, download=[], param
...customProps?.[cardType] ?? {}
};

if ( !isIncluded(cardProps) )
if ( !isAreaIncluded(cardProps) )
return null;

return <BrowserHistory>
Expand Down
24 changes: 18 additions & 6 deletions src/components/HeadlineNumbers/HeadlineNumbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import React from "react";

import { getParamValueFor, isAreaIncluded } from "common/utils";

import type { Props } from "./HeadlineNumbers.types";
import ValueBox from "components/ValueBox";

Expand All @@ -12,18 +14,28 @@ import type { ComponentType } from "react";

const HeadlineNumbers: ComponentType<Props> = ({ params, headlineNumbers=[] }) => {

const getValueBox = (item, index) => {
return <>
{
isAreaIncluded({params, ...item})
? <ValueBox params={ params }
key={ `headline-number-${ index }` }
heading={ item.caption }
{ ...item }/>
: null
}
</>
}

console.log("headline numbers:", headlineNumbers)

return <Container aria-label={ "Headline numbers" }
aria-describedby={ 'headline-nums-desc' }>
<span id={ 'headline-nums-desc' } className={ "govuk-visually-hidden" }>
Latest available data
</span>
{
headlineNumbers?.map((item, index) =>
<ValueBox params={ params }
key={ `headline-number-${ index }` }
heading={ item.caption }
{ ...item }/>
) ?? null
headlineNumbers?.map((item, index) => getValueBox(item, index)) ?? null
}
</Container>

Expand Down

0 comments on commit 81e7d64

Please sign in to comment.