Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

format date better #1512

Merged
merged 4 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions f2/src/summary/HowDidItStartSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Trans } from '@lingui/macro'
import { Stack, Flex } from '@chakra-ui/core'
import { useStateValue } from '../utils/state'
import { containsData } from '../utils/containsData'
import { formatDate } from '../utils/formatDate'
import { testdata, EditButton } from '../ConfirmationSummary'
import { H2 } from '../components/header'
import { DescriptionListItem } from '../components/DescriptionListItem'
Expand Down Expand Up @@ -103,13 +104,11 @@ export const HowDidItStartSummary = props => {
/>
<DescriptionListItem
descriptionTitle="confirmationPage.whenDidItStart"
description={
howdiditstart.startDay +
' ' +
howdiditstart.startMonth +
' ' +
howdiditstart.startYear
}
description={formatDate(
howdiditstart.startDay,
howdiditstart.startMonth,
howdiditstart.startYear,
)}
/>
<DescriptionListItem
descriptionTitle="confirmationPage.howManyTimes"
Expand Down
13 changes: 6 additions & 7 deletions f2/src/summary/MoneyLostInfoSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Trans } from '@lingui/macro'
import { Stack, Flex } from '@chakra-ui/core'
import { useStateValue } from '../utils/state'
import { containsData } from '../utils/containsData'
import { formatDate } from '../utils/formatDate'
import { testdata, EditButton } from '../ConfirmationSummary'
import { H2 } from '../components/header'
import { DescriptionListItem } from '../components/DescriptionListItem'
Expand Down Expand Up @@ -81,13 +82,11 @@ export const MoneyLostInfoSummary = props => {
/>
<DescriptionListItem
descriptionTitle="confirmationPage.moneyLost.transactionDate"
description={
moneyLost.transactionDay +
' ' +
moneyLost.transactionMonth +
' ' +
moneyLost.transactionYear
}
description={formatDate(
moneyLost.transactionDay,
moneyLost.transactionMonth,
moneyLost.transactionYear,
)}
/>
<DescriptionListItem
descriptionTitle="confirmationPage.moneyLost.tellUsMore"
Expand Down
22 changes: 22 additions & 0 deletions f2/src/utils/__tests__/formatDate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { formatDate } from '../formatDate'

describe('formatDate', () => {
it('formats a full date correctly', () => {
expect(formatDate(20, 1, 2020)).toEqual('20/1/2020')
})

it('formats a month/year correctly', () => {
expect(formatDate('', 1, 2020)).toEqual('1/2020')
expect(formatDate(undefined, 1, 2020)).toEqual('1/2020')
})

it('formats a year correctly', () => {
expect(formatDate('', '', 2020)).toEqual('2020')
expect(formatDate(undefined, undefined, 2020)).toEqual('2020')
})

it('formats empty data correctly', () => {
expect(formatDate('', '', '')).toEqual('')
expect(formatDate(undefined, undefined, undefined)).toEqual('')
})
})
14 changes: 12 additions & 2 deletions f2/src/utils/formatAnalystEmail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// 'use strict'

const { formatDate } = require('./formatDate')

const formatLine = (label, text) => (text !== '' ? label + text + '\n' : '')

const formatReportInfo = data => {
Expand Down Expand Up @@ -44,7 +46,11 @@ const formatVictimDetails = data => {
}

const formatIncidentInformation = data => {
const occurenceString = `${data.howdiditstart.startDay}-${data.howdiditstart.startMonth}-${data.howdiditstart.startYear}`
const occurenceString = formatDate(
data.howdiditstart.startDay,
data.howdiditstart.startMonth,
data.howdiditstart.startYear,
)
const freqString = data.howdiditstart.howManyTimes.replace(
'howManyTimes.',
'',
Expand Down Expand Up @@ -155,7 +161,11 @@ const formatFinancialTransactions = data => {
const paymentString = data.moneyLost.methodPayment
.map(method => method.replace('methodPayment.', ''))
.join(', ')
const transactionDate = `${data.moneyLost.transactionDay}-${data.moneyLost.transactionMonth}-${data.moneyLost.transactionYear}`
const transactionDate = formatDate(
data.moneyLost.transactionDay,
data.moneyLost.transactionMonth,
data.moneyLost.transactionYear,
)
const returnString =
formatLine('Money requested: ', data.moneyLost.demandedMoney) +
formatLine('Money lost: ', data.moneyLost.moneyTaken) +
Expand Down
7 changes: 7 additions & 0 deletions f2/src/utils/formatDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
formatDate(day, month, year) {
return `${day}/${month}/${year}`
.replace(/undefined/g, '')
.replace(/^\/+/, '')
},
}