Skip to content

Commit

Permalink
CDC #157 - Adding null checks to useUserDefinedFields hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dleadbetter committed May 13, 2024
1 parent 37fefa2 commit 57827a3
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions packages/user-defined-fields/src/hooks/UserDefinedFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
*
Expand All @@ -40,7 +58,7 @@ const useUserDefinedColumns = (defineableId, defineableType) => {
...column,
render: (item) => (
<BooleanIcon
value={item.user_defined[field.uuid]}
value={resolveValue(item, field)}
/>
)
});
Expand All @@ -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.
Expand Down

0 comments on commit 57827a3

Please sign in to comment.