From 57827a3cf3b0b3c03f43760da8ec1cb84bdeeff4 Mon Sep 17 00:00:00 2001 From: dleadbetter <> Date: Mon, 13 May 2024 07:26:31 -0400 Subject: [PATCH] CDC #157 - Adding null checks to useUserDefinedFields hook --- .../src/hooks/UserDefinedFields.js | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/user-defined-fields/src/hooks/UserDefinedFields.js b/packages/user-defined-fields/src/hooks/UserDefinedFields.js index 66d6350e..c4520e2e 100644 --- a/packages/user-defined-fields/src/hooks/UserDefinedFields.js +++ b/packages/user-defined-fields/src/hooks/UserDefinedFields.js @@ -2,7 +2,12 @@ import { BooleanIcon } from '@performant-software/semantic-components'; import { Date as DateUtils } from '@performant-software/shared-components'; -import React, { useEffect, useMemo, useState } from 'react'; +import React, { + useCallback, + useEffect, + useMemo, + useState +} from 'react'; import _ from 'underscore'; import { DataTypes } from '../constants/DataTypes'; import UserDefinedFieldsService from '../services/UserDefinedFields'; @@ -19,6 +24,19 @@ const useUserDefinedColumns = (defineableId, defineableType) => { const [fields, setFields] = useState([]); const [loading, setLoading] = useState(false); + /** + * Resolves the user defined field value for the passed item. + * + * @type {(function(*, *): (null|*))|*} + */ + const resolveValue = useCallback((item, field) => { + if (!item.user_defined) { + return null; + } + + return item.user_defined[field.uuid]; + }, []); + /** * Memo-izes the columns. * @@ -40,7 +58,7 @@ const useUserDefinedColumns = (defineableId, defineableType) => { ...column, render: (item) => ( ) }); @@ -49,20 +67,20 @@ const useUserDefinedColumns = (defineableId, defineableType) => { if (field.data_type === DataTypes.date) { columns.push({ ...column, - resolve: (item) => DateUtils.formatDate(item.user_defined[field.uuid]) + resolve: (item) => DateUtils.formatDate(resolveValue(item, field)) }); } if (field.data_type !== DataTypes.richText) { columns.push({ ...column, - resolve: (item) => item.user_defined[field.uuid] + resolve: (item) => resolveValue(item, field) }); } }); return columns; - }, [fields]); + }, [fields, resolveValue]); /** * Fetches the user defined fields for the passed defineable ID and defineable type.