Skip to content

Commit

Permalink
feat(web): Chart number box - Add configuration so we can determine w…
Browse files Browse the repository at this point in the history
…ithin the CMS if the value should be rounded or not (#15078)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
RunarVestmann and kodiakhq[bot] authored Jun 4, 2024
1 parent 4b117e0 commit ee1be2d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export const ChartNumberBox = ({ slice }: ChartNumberBoxRendererProps) => {
return <p>{messages[activeLocale].noDataForChart}</p>
}

const reduceAndRoundValue = slice.reduceAndRoundValue ?? true

return (
<div
role="group"
Expand All @@ -97,7 +99,7 @@ export const ChartNumberBox = ({ slice }: ChartNumberBoxRendererProps) => {
})}
>
{boxData.map((data, index) => {
// We assume that the data that key that is provided is a valid number
// We assume that the data behind the key that is provided is a valid number
const comparisonValue = queryResult.data?.[data.sourceDataIndex]?.[
data.sourceDataKey
] as number
Expand All @@ -109,14 +111,22 @@ export const ChartNumberBox = ({ slice }: ChartNumberBoxRendererProps) => {

const ariaValue =
data.valueType === 'number'
? formatValueForPresentation(activeLocale, mostRecentValue)
? formatValueForPresentation(
activeLocale,
mostRecentValue,
reduceAndRoundValue,
)
: formatNumberBoxPercentageForPresentation(
index === 0 ? mostRecentValue : change - 1,
)

const displayedValue =
data.valueType === 'number'
? formatValueForPresentation(activeLocale, mostRecentValue)
? formatValueForPresentation(
activeLocale,
mostRecentValue,
reduceAndRoundValue,
)
: formatNumberBoxPercentageForPresentation(
index === 0 ? mostRecentValue : Math.abs(change - 1),
)
Expand Down
1 change: 1 addition & 0 deletions apps/web/screens/queries/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ export const slices = gql`
displayChangeMonthOverMonth
displayChangeYearOverYear
numberBoxDate
reduceAndRoundValue
}
fragment GenericListFields on GenericList {
Expand Down
3 changes: 3 additions & 0 deletions libs/cms/src/lib/generated/contentfulTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,9 @@ export interface IChartNumberBoxFields {

/** Date */
numberBoxDate?: string | undefined

/** Reduce and round value */
reduceAndRoundValue?: boolean | undefined
}

/** A standalone component to display a value for a data key and optionally how it has evolved over a period of time. */
Expand Down
4 changes: 4 additions & 0 deletions libs/cms/src/lib/models/chartNumberBox.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export class ChartNumberBox {

@Field({ nullable: true })
numberBoxDate?: string

@Field({ nullable: true })
reduceAndRoundValue?: boolean
}

export const mapChartNumberBox = ({
Expand All @@ -48,5 +51,6 @@ export const mapChartNumberBox = ({
'displayChangeYearOverYear',
]),
numberBoxDate: fields.numberBoxDate ?? undefined,
reduceAndRoundValue: fields.reduceAndRoundValue ?? true,
}
}

0 comments on commit ee1be2d

Please sign in to comment.