From 39e78a37a245f5460e91dd9acb14b1b53c110f62 Mon Sep 17 00:00:00 2001 From: siiky Date: Thu, 19 Sep 2024 15:23:10 +0100 Subject: [PATCH] fix: don't crash on customer page with bad dates --- .../src/pages/Customers/CustomerData.js | 14 +++--------- .../src/pages/Customers/helper.js | 22 ++++++++++++++----- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/new-lamassu-admin/src/pages/Customers/CustomerData.js b/new-lamassu-admin/src/pages/Customers/CustomerData.js index 049c0227..b605eb07 100644 --- a/new-lamassu-admin/src/pages/Customers/CustomerData.js +++ b/new-lamassu-admin/src/pages/Customers/CustomerData.js @@ -1,6 +1,5 @@ import Grid from '@material-ui/core/Grid' import { makeStyles } from '@material-ui/core/styles' -import { parse, format } from 'date-fns/fp' import * as R from 'ramda' import { useState, React } from 'react' import * as Yup from 'yup' @@ -30,6 +29,7 @@ import { customerDataElements, customerDataSchemas, formatDates, + tryFormatDate, getFormattedPhone } from './helper.js' @@ -113,18 +113,10 @@ const CustomerData = ({ firstName: R.path(['firstName'])(idData) ?? '', lastName: R.path(['lastName'])(idData) ?? '', documentNumber: R.path(['documentNumber'])(idData) ?? '', - dateOfBirth: - (rawDob && - format('yyyy-MM-dd')(parse(new Date(), 'yyyyMMdd', rawDob))) ?? - '', + dateOfBirth: tryFormatDate(rawDob), gender: R.path(['gender'])(idData) ?? '', country: R.path(['country'])(idData) ?? '', - expirationDate: - (rawExpirationDate && - format('yyyy-MM-dd')( - parse(new Date(), 'yyyyMMdd', rawExpirationDate) - )) ?? - '' + expirationDate: tryFormatDate(rawExpirationDate) }, usSsn: { usSsn: customer.usSsn ?? '' diff --git a/new-lamassu-admin/src/pages/Customers/helper.js b/new-lamassu-admin/src/pages/Customers/helper.js index 50a38bc1..42e38317 100644 --- a/new-lamassu-admin/src/pages/Customers/helper.js +++ b/new-lamassu-admin/src/pages/Customers/helper.js @@ -528,13 +528,22 @@ const requirementElements = { } } +const tryFormatDate = rawDate => { + try { + return ( + (rawDate && + format('yyyy-MM-dd')(parse(new Date(), 'yyyyMMdd', rawDate))) ?? + '' + ) + } catch (err) { + return '' + } +} + const formatDates = values => { - R.map( - elem => - (values[elem] = format('yyyyMMdd')( - parse(new Date(), 'yyyy-MM-dd', values[elem]) - )) - )(['dateOfBirth', 'expirationDate']) + R.forEach(elem => { + values[elem] = tryFormatDate(values[elem]) + })(['dateOfBirth', 'expirationDate']) return values } @@ -579,6 +588,7 @@ export { customerDataElements, customerDataSchemas, formatDates, + tryFormatDate, REQUIREMENT, CUSTOM, ID_CARD_DATA