Skip to content

Commit

Permalink
Merge pull request #7456 from uktrade/refactor/date-functions
Browse files Browse the repository at this point in the history
Remove `createDateFromObject` and replace with `parseDateWithYearMonth`
  • Loading branch information
paulgain authored Jan 7, 2025
2 parents f079a8f + 520b15d commit fc57eeb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { createDateFromObject } from '../../../../utils/date'
import { isValid } from 'date-fns'

import { parseDateWithYearMonth } from '../../../../utils/date'

export const validateDateWithinTheLastYear = (value, field, { values }) => {
if (values && values.date) {
const date = createDateFromObject(values.date)
if (date) {
const date = parseDateWithYearMonth(
values.date.year,
values.date.month,
values.date.day
)
if (isValid(date)) {
const now = new Date()
const oneYearPrior = new Date()
oneYearPrior.setFullYear(oneYearPrior.getFullYear() - 1)
Expand Down
18 changes: 14 additions & 4 deletions src/client/modules/Events/EventForm/validators.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { createDateFromObject } from '../../../utils/date'
import { isValid } from 'date-fns'

import { parseDateWithYearMonth } from '../../../utils/date'

export const validateStartDateBeforeOrEqualToEndDate = (
value,
field,
{ values }
) => {
if (values && values.start_date && values.end_date) {
const startDate = createDateFromObject(values.start_date)
const endDate = createDateFromObject(values.end_date)
if (startDate && endDate) {
const startDate = parseDateWithYearMonth(
values.start_date.year,
values.start_date.month,
values.start_date.day
)
const endDate = parseDateWithYearMonth(
values.end_date.year,
values.end_date.month,
values.end_date.day
)
if (isValid(startDate) && isValid(endDate)) {
const result =
startDate > endDate
? 'Enter a valid end date. This must be after the start date.'
Expand Down
7 changes: 0 additions & 7 deletions src/client/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ function getDifferenceInWords(date, suffix = true) {
}
}

function createDateFromObject({ day, month, year }) {
const monthIndex = parseInt(month, 10) - 1
const result = new Date(year, monthIndex, day)
return result
}

function formatStartAndEndDate(startDate, endDate) {
if (startDate) {
const startDateParsed = startDate ? parseISO(startDate) : startDate
Expand Down Expand Up @@ -270,7 +264,6 @@ module.exports = {
getFinancialYearStart,
parseDateWithYearMonth,
formatDateWithYearMonth,
createDateFromObject,
formatStartAndEndDate,
convertDateToFieldShortDateObject,
convertDateToFieldDateObject,
Expand Down

0 comments on commit fc57eeb

Please sign in to comment.