Skip to content

Commit

Permalink
add locationAware attribute to headlineNumbers
Browse files Browse the repository at this point in the history
Code changes to use the new attribute to show/hide
headline numbers for a region
  • Loading branch information
Jaroslaw Michalski committed Oct 3, 2023
1 parent f09f246 commit 2cf0c23
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 52 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
16 changes: 2 additions & 14 deletions src/components/HeadlineNumbers/HeadlineNumbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from "react";

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

import type { Props } from "./HeadlineNumbers.types";
import ValueBox from "components/ValueBox";
Expand All @@ -14,22 +14,10 @@ import type { ComponentType } from "react";

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

const
areaType = getParamValueFor(
params,
"areaType",
"overview"
).toLowerCase(),
areaName = getParamValueFor(
params,
"areaName",
"United Kingdom"
).toLowerCase();

const getValueBox = (item, index) => {
return <>
{
(item?.areaName == null || (item?.areaName.toLowerCase() === areaName && item?.areaType.toLowerCase() === areaType))
isAreaIncluded({params, ...item})
? <ValueBox params={ params }
key={ `headline-number-${ index }` }
heading={ item.caption }
Expand Down

0 comments on commit 2cf0c23

Please sign in to comment.