Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERM-3392 Changes to the field content type are not saved, but a success message is displayed #1368

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import PropTypes from 'prop-types';
import { useEffect, useState, useRef } from 'react';
import PropTypes from 'prop-types';

import isEqual from 'lodash/isEqual';

import { FormattedMessage } from 'react-intl';
import { Field } from 'react-final-form';
import { getRefdataValuesByDesc, requiredValidator, usePrevious } from '@folio/stripes-erm-components';

import {
Button,
Col,
Expand All @@ -13,18 +16,9 @@ import {
} from '@folio/stripes/components';
import { useKiwtFieldArray } from '@k-int/stripes-kint-components';

import { useAgreementsRefdata } from '../../../hooks';

// Utility function to check if two arrays of scalars contain the same items (Order does not count)
const arraysAreEqual = (a, b) => {
if (a.length !== b.length) {
return false;
}
import { getRefdataValuesByDesc, requiredValidator, usePrevious } from '@folio/stripes-erm-components';

return a.every(element => {
return b.includes(element);
});
};
import { useAgreementsRefdata } from '../../../hooks';

const [
AGREEMENT_CONTENT_TYPE
Expand All @@ -40,8 +34,12 @@ const ContentTypesFieldArray = ({
AGREEMENT_CONTENT_TYPE,
]
});
const contentTypeValues = getRefdataValuesByDesc(refdata, AGREEMENT_CONTENT_TYPE);
const agreementContentType = contentTypeValues.map(ct => ({ value: ct.value, label: ct.label }));
const contentTypeSelectOptions = getRefdataValuesByDesc(refdata, AGREEMENT_CONTENT_TYPE)
.map(ct => ({
value: ct.id, // Map to id for submittal purposes
label: ct.label
}));

const { items, onAddField, onDeleteField } = useKiwtFieldArray(name);
const [contentTypeInUse, setContentTypeInUse] = useState([]);
const contentTypeRefs = useRef([]);
Expand All @@ -51,12 +49,13 @@ const ContentTypesFieldArray = ({
const previousCount = usePrevious(itemsLength);

useEffect(() => {
const newContentTypeInUse = items.map(i => i?.contentType?.value).filter(x => !!x);
if (!arraysAreEqual(contentTypeInUse, newContentTypeInUse)) {
const newContentTypeInUse = items.map(i => i?.contentType?.id).filter(x => !!x);
if (!isEqual(contentTypeInUse, newContentTypeInUse)) {
setContentTypeInUse(newContentTypeInUse);
}
}, [items, contentTypeInUse]);


useEffect(() => {
// the second conditional is checking if the current field to be focused is the default content type field.
if (contentTypeRefs.current.length > 1 || (contentTypeRefs.current.length > 0 && previousCount - itemsLength === 1)) {
Expand All @@ -73,21 +72,29 @@ const ContentTypesFieldArray = ({

const renderContentTypes = () => (
items.map((act, index) => {
const dataOptions = agreementContentType.filter(ct => !contentTypeInUse.includes(ct.value) || ct.value === act.contentType?.value);
const dataOptions = contentTypeSelectOptions.filter(ct => !contentTypeInUse.includes(ct.value) || ct.value === act.contentType?.id);

return (
<div
key={`${act?.contentType?.id}`}
data-testid={`contentTypesFieldArray[${index}]`}
>
<Row>
<Col xs={11}>
{/*
* This select will ONLY change the id of the refdata,
* and the bind will happen from that. That will make
* the PUT look a little funky as it will have the old
* label/value information, but prevents us from having
* to do loads of eaxtra tweaking at the field level
*/}
<Field
component={Select}
dataOptions={dataOptions}
id="content-type-field-array"
index={index}
inputRef={addToRefs}
name={`${name}[${index}].contentType.value`}
name={`${name}[${index}].contentType.id`}
placeholder=" "
validate={requiredValidator}
/>
Expand Down