Skip to content

Commit

Permalink
CDPS-92: adjudications update (#64)
Browse files Browse the repository at this point in the history
* CDPS-92: adjudications update

* Add pluralise function
Add hyperlink
Change awards to punishments
Update tests
  • Loading branch information
davidatmoj authored Feb 28, 2023
1 parent eca85e4 commit c37046d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 10 deletions.
2 changes: 1 addition & 1 deletion integration_tests/e2e/overviewPage.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ context('Overview Page', () => {
overviewPage.adjudicationsCard().contains('p', 'Proven in last 3 months')
overviewPage.adjudicationsCard().contains('p', '4')
overviewPage.adjudicationsCard().contains('p', 'Active')
overviewPage.adjudicationsCard().contains('p', 'No active awards')
overviewPage.adjudicationsCard().contains('p', 'No active punishments')
overviewPage.adjudicationsCard().contains('a', 'Adjudications history')
})

Expand Down
2 changes: 1 addition & 1 deletion server/data/localMockData/miniSummaryMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const adjudicationsSummaryDataMock: MiniSummaryData = {
topContent: 4,
topClass: 'big',
bottomLabel: 'Active',
bottomContentLine1: 'No active awards',
bottomContentLine1: 'No active punishments',
bottomClass: 'small',
linkLabel: 'Adjudications history',
linkHref: '#',
Expand Down
3 changes: 2 additions & 1 deletion server/interfaces/miniSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export interface MiniSummaryData {
topClass?: string // 'big' | 'small'
bottomLabel: string
bottomContentLine1?: string | number
bottomContentLine2?: string | number // TODO need to implement a link for this as well (updated adjudications mini card - need new ticket)
bottomContentLine1Href?: string
bottomContentLine2?: string | number
bottomClass: string // 'big' | 'small'
linkLabel: string
linkHref: string
Expand Down
8 changes: 5 additions & 3 deletions server/services/overviewPageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ProblemType } from '../data/enums/problemType'
import { ProblemStatus } from '../data/enums/problemStatus'
import { pregnantProblemCodes } from '../data/constants'
import { BooleanString } from '../data/enums/booleanString'
import { pluralise } from '../utils/pluralise'

export default class OverviewPageService {
private prisonApiClient: PrisonApiClient
Expand Down Expand Up @@ -229,9 +230,10 @@ export default class OverviewPageService {
topContent: adjudicationSummary.adjudicationCount,
topClass: 'big',
bottomLabel: 'Active',
bottomContentLine1: adjudicationSummary.awards?.length
? `${adjudicationSummary.awards.length} active punishments`
: 'No active awards',
bottomContentLine1: pluralise(adjudicationSummary.awards.length, 'active punishment', {
emptyMessage: 'No active punishments',
}),
bottomContentLine1Href: adjudicationSummary.awards?.length ? '#' : undefined,
bottomClass: 'small',
linkLabel: 'Adjudications history',
linkHref: '#',
Expand Down
2 changes: 2 additions & 0 deletions server/utils/nunjucksSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import nunjucks from 'nunjucks'
import express from 'express'
import * as pathModule from 'path'
import { formatDate, formatScheduleItem, initialiseName, summaryListOneHalfWidth } from './utils'
import { pluralise } from './pluralise'

const production = process.env.NODE_ENV === 'production'

Expand Down Expand Up @@ -42,4 +43,5 @@ export default function nunjucksSetup(app: express.Express, path: pathModule.Pla
njkEnv.addFilter('formatDate', formatDate)
njkEnv.addFilter('formatScheduleItem', formatScheduleItem)
njkEnv.addFilter('summaryListOneHalfWidth', summaryListOneHalfWidth)
njkEnv.addFilter('pluralise', pluralise)
}
33 changes: 33 additions & 0 deletions server/utils/pluralise.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { pluralise } from './pluralise'

describe('pluralise a word', () => {
it.each([
['Standard word, no options - singular count', 1, 'cat', undefined, '1 cat'],
['Standard word, no options - plural count', 2, 'cat', undefined, '2 cats'],
['Standard word, no options - zero count', 0, 'cat', undefined, '0 cats'],
['Standard word, no options - negative singular count', -1, 'cat', undefined, '-1 cat'],
['Standard word, no options - negative plural count', -7, 'cat', undefined, '-7 cats'],
['Standard word, forced plural - plural count', 3, 'cat', { plural: 'kittens' }, '3 kittens'],
['Standard word, no includeCount - plural count', 4, 'cat', { includeCount: false }, 'cats'],
['Standard word, empty message - zero count', 0, 'cat', { emptyMessage: 'No cats' }, 'No cats'],
['Standard word, empty message - non-zero count', 2, 'cat', { emptyMessage: 'No cats' }, '2 cats'],
['Non-standard word, no options - plural count', 2, 'person', undefined, '2 people'],
['Non-standard word, forced plural - plural count', 4, 'person', { plural: 'humans' }, '4 humans'],
['Non-standard word, no includeCount - plural count', 4, 'person', { includeCount: false }, 'people'],
[
'Non-standard word, forced plural, no includeCount - plural count',
4,
'person',
{ plural: 'humans', includeCount: false },
'humans',
],
['Non-standard word, empty message - plural count', 0, 'person', { emptyMessage: 'Nobody here' }, 'Nobody here'],
['Standard word, multiple - singular count', 1, 'active punishment', undefined, '1 active punishment'],
['Standard word, multiple - plural count', 2, 'active punishment', undefined, '2 active punishments'],
])(
'%s pluralise(%s, %s, %s)',
(_: string, count: number, word: string, options: { [key: string]: string | boolean }, expected: string) => {
expect(pluralise(count, word, options)).toEqual(expected)
},
)
})
47 changes: 47 additions & 0 deletions server/utils/pluralise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const notStandardPlurals = {
person: 'people',
man: 'men',
woman: 'women',
child: 'children',
knife: 'knives',
life: 'lives',
wife: 'wives',
foot: 'feet',
tooth: 'teeth',
}

/**
* Pluralises a given word based on a count provided.
*
* Uses `nonStandardPlurals` if the word is defined, else just adds an 's'
*
* Both can be overridden by using the `plural` option
*
* Added as Nunjucks filter for use in templates, e.g.
* - {{ prisonersArray.length | pluralise('person') }} // 'people'
* - {{ countOfChildren | pluralise('child') }} // 'children'
* - {{ countOfChildren | pluralise('child', 'kids') }} // 'kids'
*
* Limitations: entries in `nonStandardPlurals` are case-sensitive, so 'Person' will return 'Persons'
*
* @param count - denotes if `word` should be pluralised or not
* @param word - the singular form of the word to be pluralised
* @param options - plural, includeCount, emptyMessage
* @param options.plural - return this specific plural if count !== [1,-1]
* @param options.includeCount - prefix the return string with `count` (e.g. '3 people') - defaults to true
* @param options.emptyMessage - return this string if `count` is Falsy
* @return pluralised form of word if count !== [1,-1] else just word
*/
// eslint-disable-next-line import/prefer-default-export
export const pluralise = (
count: number,
word: string,
options?: { plural?: string; includeCount?: boolean; emptyMessage?: string },
): string => {
const includeCount = options?.includeCount ?? true
if (!count && options?.emptyMessage) {
return options.emptyMessage
}
const pluralised = [1, -1].includes(count) ? word : options?.plural || notStandardPlurals[word] || `${word}s`
return includeCount ? `${count} ${pluralised}` : pluralised
}
12 changes: 8 additions & 4 deletions server/views/partials/overviewPage/miniSummaryBoxes/macro.njk
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
{{ miniSummary.data.topLabel }}
</p>
{% endif %}
{% if miniSummary.data.topContent !== undefined %}
<p
class="govuk-body hmpps-mini-card__{{ miniSummary.data.topClass }} {{ 'hmpps-mini-card__filler' if miniSummary.data.topClass === 'small' }}"
>
{{ miniSummary.data.topContent }}
{{ miniSummary.data.topContent if miniSummary.data.topContent else '&nbsp;' | safe }}
</p>
{%- endif -%}
<p class="govuk-body card-label">
{{ miniSummary.data.bottomLabel }}
</p>
<p
class="govuk-body hmpps-mini-card__{{ miniSummary.data.bottomClass }} {{ 'hmpps-mini-card__filler' if not miniSummary.data.bottomContentLine2 and miniSummary.data.bottomClass === 'small' }}"
>
{{ miniSummary.data.bottomContentLine1 }}
{% if miniSummary.data.bottomContentLine1Href %}
<a href="{{ miniSummary.data.bottomContentLine1Href }}">
{{ miniSummary.data.bottomContentLine1 }}
</a>
{% else %}
{{ miniSummary.data.bottomContentLine1 }}
{% endif %}
</p>
{% if miniSummary.data.bottomContentLine2 !== undefined %}
<p class="govuk-body hmpps-mini-card__sub-text">
Expand Down

0 comments on commit c37046d

Please sign in to comment.