-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CDC #157 - Adding useUserDefinedColumns hook
- Loading branch information
dleadbetter
committed
May 13, 2024
1 parent
c02fd82
commit 6ed630a
Showing
4 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// @flow | ||
|
||
const DataTypes = { | ||
boolean: 'Boolean', | ||
date: 'Date', | ||
number: 'Number', | ||
richText: 'RichText', | ||
select: 'Select', | ||
string: 'String', | ||
text: 'Text' | ||
}; | ||
|
||
export { | ||
DataTypes | ||
}; |
85 changes: 85 additions & 0 deletions
85
packages/user-defined-fields/src/hooks/UserDefinedFields.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// @flow | ||
|
||
import { BooleanIcon } from '@performant-software/semantic-components'; | ||
import { Date as DateUtils } from '@performant-software/shared-components'; | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import _ from 'underscore'; | ||
import { DataTypes } from '../constants/DataTypes'; | ||
import UserDefinedFieldsService from '../services/UserDefinedFields'; | ||
|
||
/** | ||
* Returns the loading state and list of user defined columns for the passed defineable ID and defineable type. | ||
* | ||
* @param defineableId | ||
* @param defineableType | ||
* | ||
* @returns {{userDefinedColumns: [], loading: boolean}} | ||
*/ | ||
const useUserDefinedColumns = (defineableId, defineableType) => { | ||
const [fields, setFields] = useState([]); | ||
const [loading, setLoading] = useState(false); | ||
|
||
/** | ||
* Memo-izes the columns. | ||
* | ||
* @type {[]} | ||
*/ | ||
const userDefinedColumns = useMemo(() => { | ||
const columns = []; | ||
|
||
_.each(fields, (field) => { | ||
const column = { | ||
name: field.column_name, | ||
label: field.column_name, | ||
hidden: true, | ||
sortable: true | ||
}; | ||
|
||
if (field.data_type === DataTypes.boolean) { | ||
columns.push({ | ||
...column, | ||
render: (item) => ( | ||
<BooleanIcon | ||
value={item.user_defined[field.uuid]} | ||
/> | ||
) | ||
}); | ||
} | ||
|
||
if (field.data_type === DataTypes.date) { | ||
columns.push({ | ||
...column, | ||
resolve: (item) => DateUtils.formatDate(item.user_defined[field.uuid]) | ||
}); | ||
} | ||
|
||
if (field.data_type !== DataTypes.richText) { | ||
columns.push({ | ||
...column, | ||
resolve: (item) => item.user_defined[field.uuid] | ||
}); | ||
} | ||
}); | ||
|
||
return columns; | ||
}, [fields]); | ||
|
||
/** | ||
* Fetches the user defined fields for the passed defineable ID and defineable type. | ||
*/ | ||
useEffect(() => { | ||
setLoading(true); | ||
|
||
UserDefinedFieldsService | ||
.fetchAll({ defineable_id: defineableId, defineable_type: defineableType }) | ||
.then(({ data }) => setFields(data.user_defined_fields)) | ||
.finally(() => setLoading(false)); | ||
}, [defineableId, defineableId]); | ||
|
||
return { | ||
loading, | ||
userDefinedColumns | ||
}; | ||
}; | ||
|
||
export default useUserDefinedColumns; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters