META-INF.resources.js.components.modals.EditAPIPropertyModalContent.tsx Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.headless.builder.web
Show all versions of com.liferay.headless.builder.web
Liferay Headless Builder Web
The newest version!
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
import ClayButton from '@clayui/button';
import ClayForm, {ClayInput} from '@clayui/form';
import ClayIcon from '@clayui/icon';
import ClayModal from '@clayui/modal';
import classNames from 'classnames';
import {openToast} from 'frontend-js-web';
import React, {Dispatch, SetStateAction, useEffect, useState} from 'react';
import {Select} from '../fieldComponents/Select';
type DataError = {
dataType: boolean;
mappedProperty: boolean;
name: boolean;
};
interface PropertyUIData {
dataType: ObjectFieldBusinessType;
description: string;
mappedProperty: string;
name: string;
}
interface EditAPIPropertyModalContentProps extends Partial {
closeModal: voidReturn;
setSchemaUIData: Dispatch>;
}
export default function EditAPIPropertyModalContent({
businessType,
closeModal,
description,
name,
objectFieldId,
objectFieldName,
setSchemaUIData,
}: EditAPIPropertyModalContentProps) {
const [localUIData, setLocalUIData] = useState>({
dataType: businessType,
description,
mappedProperty: objectFieldName,
name,
});
const [displayError, setDisplayError] = useState({
dataType: false,
mappedProperty: false,
name: false,
});
useEffect(() => {
for (const key in localUIData) {
if (localUIData[key as keyof PropertyUIData] !== '') {
setDisplayError((previousErrors) => ({
...previousErrors,
[key]: false,
}));
}
}
}, [localUIData]);
function validateData() {
let isDataValid = true;
const mandatoryFields = ['dataType', 'mappedProperty', 'name'];
if (!Object.keys(localUIData).length) {
const errors = mandatoryFields.reduce(
(errors, field) => ({...errors, [field]: true}),
{}
);
setDisplayError(errors as DataError);
isDataValid = false;
}
else {
mandatoryFields.forEach((field) => {
if (localUIData[field as keyof PropertyUIData]) {
setDisplayError((previousErrors) => ({
...previousErrors,
[field]: false,
}));
}
else {
setDisplayError((previousErrors) => ({
...previousErrors,
[field]: true,
}));
isDataValid = false;
}
});
}
return isDataValid;
}
const handleUpdate = () => {
const isDataValid = validateData();
if (isDataValid) {
setSchemaUIData((previous) =>
previous.schemaProperties
? {
...previous,
schemaProperties: previous.schemaProperties.map(
(property) =>
property.objectFieldId !== objectFieldId
? property
: {
...property,
...localUIData,
}
),
}
: previous
);
closeModal();
openToast({
message: Liferay.Language.get('schema-property-was-updated'),
type: 'success',
});
}
else {
return;
}
};
return (
<>
{Liferay.Language.get('edit-property')}
setLocalUIData((previous) => ({
...previous,
name: value,
}))
}
onKeyPress={(event) =>
event.key === 'Enter' && event.preventDefault()
}
placeholder={Liferay.Language.get('enter-name')}
value={localUIData.name}
/>
{displayError.name && (
{Liferay.Language.get(
'please-enter-a-property-name'
)}
)}
{(displayError.name || displayError.dataType) && (
{Liferay.Language.get(
'there-are-errors-on-the-form-please-check-if-any-mandatory-fields-have-not-been-completed'
)}
)}
{Liferay.Language.get('cancel')}
{Liferay.Language.get('update')}
}
/>
>
);
}