-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply Custom Fields page components for record
- Loading branch information
1 parent
3106950
commit 8bda292
Showing
12 changed files
with
327 additions
and
5 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
145 changes: 145 additions & 0 deletions
145
lib/CustomFields/pages/EditCustomFieldsRecord/EditCustomFieldsRecord.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,145 @@ | ||
import React from 'react'; | ||
import { PropTypes } from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { chunk } from 'lodash'; | ||
import { | ||
Accordion, | ||
Callout, | ||
Col, | ||
Row, | ||
InfoPopover, | ||
} from '@folio/stripes-components'; | ||
|
||
import { fieldComponents } from '../../constants'; | ||
import { | ||
selectModuleId, | ||
selectOkapiData, | ||
} from '../../selectors'; | ||
import { | ||
useCustomFieldsFetch, | ||
useSectionTitleFetch, | ||
useLoadingErrorCallout, | ||
valuesValidation, | ||
} from '../../utils'; | ||
|
||
const propTypes = { | ||
accordionId: PropTypes.string.isRequired, | ||
backendModuleId: PropTypes.string, | ||
backendModuleName: PropTypes.string.isRequired, | ||
columnCount: PropTypes.number, | ||
entityType: PropTypes.string.isRequired, | ||
expanded: PropTypes.bool.isRequired, | ||
fieldComponent: PropTypes.node.isRequired, | ||
okapi: PropTypes.shape({ | ||
tenant: PropTypes.string.isRequired, | ||
token: PropTypes.string.isRequired, | ||
url: PropTypes.string.isRequired, | ||
}).isRequired, | ||
onToggle: PropTypes.func.isRequired, | ||
}; | ||
|
||
const defaultProps = { | ||
columnCount: 4, | ||
}; | ||
|
||
const EditCustomFieldsRecord = ({ | ||
accordionId, | ||
onToggle, | ||
expanded, | ||
okapi, | ||
backendModuleId, | ||
backendModuleName, | ||
entityType, | ||
fieldComponent, | ||
columnCount, | ||
}) => { | ||
const { | ||
customFields, | ||
customFieldsLoaded, | ||
customFieldsFetchFailed, | ||
} = useCustomFieldsFetch(okapi, backendModuleId, entityType); | ||
const { | ||
sectionTitle, | ||
sectionTitleLoaded, | ||
sectionTitleFetchFailed, | ||
} = useSectionTitleFetch(okapi, backendModuleName.toUpperCase()); | ||
const { calloutRef } = useLoadingErrorCallout(customFieldsFetchFailed || sectionTitleFetchFailed); | ||
const dataIsLoaded = sectionTitleLoaded && customFieldsLoaded; | ||
const customFieldsIsVisible = customFields?.length > 0 && customFields?.some(customField => customField.visible); | ||
const accordionIsVisible = dataIsLoaded && customFieldsIsVisible; | ||
|
||
if (!accordionIsVisible) { | ||
return null; | ||
} | ||
|
||
const Field = fieldComponent; | ||
const formatedCustomFields = chunk(customFields, columnCount); | ||
const columnWidth = 12 / columnCount; | ||
const defaultLabel = <FormattedMessage id="stripes-smart-components.customFields.recordAccordion.defaultName" />; | ||
|
||
return ( | ||
<> | ||
<Accordion | ||
open={expanded} | ||
id={accordionId} | ||
onToggle={onToggle} | ||
label={sectionTitle.value || defaultLabel} | ||
> | ||
{ | ||
formatedCustomFields.map((row, i) => ( | ||
<Row key={i}> | ||
{ | ||
row.map(customField => ( | ||
customField.visible ? ( | ||
<Col | ||
data-test-record-edit-custom-field | ||
key={customField.refId} | ||
xs={columnWidth} | ||
> | ||
<Field | ||
component={fieldComponents[customField.type]} | ||
name={`customFields.${customField.refId}`} | ||
required={customField.required} | ||
label={ | ||
<> | ||
{customField.name} | ||
{customField.helpText && ( | ||
<InfoPopover | ||
content={customField.helpText} | ||
iconSize="small" | ||
/> | ||
)} | ||
</> | ||
} | ||
validate={value => { | ||
if (valuesValidation[customField.type]) { | ||
return valuesValidation[customField.type](value, customField); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
/> | ||
</Col> | ||
) : null | ||
)) | ||
} | ||
</Row> | ||
)) | ||
} | ||
</Accordion> | ||
<Callout ref={calloutRef} /> | ||
</> | ||
); | ||
}; | ||
|
||
EditCustomFieldsRecord.propTypes = propTypes; | ||
EditCustomFieldsRecord.defaultProps = defaultProps; | ||
|
||
export default connect( | ||
(state, ownProps) => ({ | ||
backendModuleId: selectModuleId(state, ownProps.backendModuleName), | ||
okapi: selectOkapiData(state), | ||
}) | ||
)(EditCustomFieldsRecord); |
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 @@ | ||
export { default } from './EditCustomFieldsRecord'; |
122 changes: 122 additions & 0 deletions
122
lib/CustomFields/pages/ViewCustomFieldsRecord/ViewCustomFieldsRecord.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,122 @@ | ||
import React from 'react'; | ||
import { PropTypes } from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import { chunk } from 'lodash'; | ||
|
||
import { | ||
Accordion, | ||
Callout, | ||
KeyValue, | ||
NoValue, | ||
Row, | ||
Col, | ||
} from '@folio/stripes-components'; | ||
|
||
import { | ||
selectModuleId, | ||
selectOkapiData, | ||
} from '../../selectors'; | ||
|
||
import { | ||
useCustomFieldsFetch, | ||
useSectionTitleFetch, | ||
useLoadingErrorCallout, | ||
} from '../../utils'; | ||
|
||
const propTypes = { | ||
accordionId: PropTypes.string.isRequired, | ||
backendModuleId: PropTypes.string, | ||
backendModuleName: PropTypes.string.isRequired, | ||
columnCount: PropTypes.number, | ||
customFieldsValues: PropTypes.object.isRequired, | ||
entityType: PropTypes.string.isRequired, | ||
expanded: PropTypes.bool.isRequired, | ||
okapi: PropTypes.shape({ | ||
tenant: PropTypes.string.isRequired, | ||
token: PropTypes.string.isRequired, | ||
url: PropTypes.string.isRequired, | ||
}).isRequired, | ||
onToggle: PropTypes.func.isRequired, | ||
}; | ||
|
||
const defaultProps = { | ||
columnCount: 4, | ||
}; | ||
|
||
const ViewCustomFieldsRecord = ({ | ||
accordionId, | ||
onToggle, | ||
expanded, | ||
okapi, | ||
backendModuleId, | ||
backendModuleName, | ||
entityType, | ||
customFieldsValues, | ||
columnCount, | ||
}) => { | ||
const { | ||
customFields, | ||
customFieldsLoaded, | ||
customFieldsFetchFailed, | ||
} = useCustomFieldsFetch(okapi, backendModuleId, entityType); | ||
const { | ||
sectionTitle, | ||
sectionTitleLoaded, | ||
sectionTitleFetchFailed, | ||
} = useSectionTitleFetch(okapi, backendModuleName.toUpperCase()); | ||
const { calloutRef } = useLoadingErrorCallout(customFieldsFetchFailed || sectionTitleFetchFailed); | ||
const accordionIsVisible = sectionTitleLoaded && customFieldsLoaded && customFields?.length > 0; | ||
|
||
if (!accordionIsVisible) { | ||
return null; | ||
} | ||
|
||
const formatedCustomFields = chunk(customFields, columnCount); | ||
const defaultLabel = <FormattedMessage id="stripes-smart-components.customFields.recordAccordion.defaultName" />; | ||
|
||
return ( | ||
<> | ||
<Accordion | ||
open={expanded} | ||
id={accordionId} | ||
onToggle={onToggle} | ||
label={sectionTitle.value || defaultLabel} | ||
> | ||
{ | ||
formatedCustomFields.map((row, i) => ( | ||
<Row key={i}> | ||
{ | ||
row.map(customField => ( | ||
customField.visible ? ( | ||
<Col | ||
key={customField.refId} | ||
xs={12 / columnCount} | ||
> | ||
<KeyValue | ||
label={customField.name} | ||
value={customFieldsValues[customField.refId] || <NoValue />} | ||
/> | ||
</Col> | ||
) : null | ||
)) | ||
} | ||
</Row> | ||
)) | ||
} | ||
</Accordion> | ||
<Callout ref={calloutRef} /> | ||
</> | ||
); | ||
}; | ||
|
||
ViewCustomFieldsRecord.propTypes = propTypes; | ||
ViewCustomFieldsRecord.defaultProps = defaultProps; | ||
|
||
export default connect( | ||
(state, ownProps) => ({ | ||
backendModuleId: selectModuleId(state, ownProps.backendModuleName), | ||
okapi: selectOkapiData(state), | ||
}) | ||
)(ViewCustomFieldsRecord); |
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 @@ | ||
export { default } from './ViewCustomFieldsRecord'; |
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
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
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,28 @@ | ||
import React from 'react'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
const TEXTBOX_SHORT_MAX_LENGTH = 150; | ||
const TEXTBOX_LONG_MAX_LENGTH = 1500; | ||
|
||
const textBoxValidation = length => (value, customField) => { | ||
if (customField.required && !value) { | ||
return <FormattedMessage | ||
id="stripes-smart-components.customFields.fieldValue.required" | ||
values={{ field: customField.name }} | ||
/>; | ||
} | ||
|
||
if (value?.length > length) { | ||
return <FormattedMessage | ||
id="stripes-smart-components.customFields.fieldValue.length" | ||
values={{ field: customField.name }} | ||
/>; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const valuesValidation = { | ||
TEXTBOX_SHORT: textBoxValidation(TEXTBOX_SHORT_MAX_LENGTH), | ||
TEXTBOX_LONG: textBoxValidation(TEXTBOX_LONG_MAX_LENGTH), | ||
}; |
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