All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.js.components.ObjectDetails.EditObjectDetails.tsx Maven / Gradle / Ivy

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 ClayPanel from '@clayui/panel';
import {API, openToast, stringUtils} from '@liferay/object-js-components-web';
import React, {useEffect, useState} from 'react';

import {Error, handleErrors} from '../../utils/errors';
import ObjectManagementToolbar from '../ObjectManagementToolbar';
import {AccountRestrictionContainer} from './AccountRestrictionContainer';
import {ConfigurationContainer} from './ConfigurationContainer';
import {EntryDisplayContainer} from './EntryDisplayContainer';
import {ExternalDataSourceContainer} from './ExternalDataSourceContainer';
import {ObjectDataContainer} from './ObjectDataContainer';
import {ScopeContainer} from './ScopeContainer';
import {SeoContainer} from './SeoContainer';
import Sheet from './Sheet';
import {TranslationsContainer} from './TranslationsContainer';
import {useObjectDetailsForm} from './useObjectDetailsForm';

import './ObjectDetails.scss';

export type Scope = {
	items: LabelValueObject[];
	label: string;
};

interface EditObjectDetailsProps {
	backURL: string;
	companies: Scope[];
	dbTableName: string;
	hasPublishObjectPermission: boolean;
	hasUpdateObjectDefinitionPermission: boolean;
	isApproved: boolean;
	isRootDescendantNode: boolean;
	isRootNode: boolean;
	label: LocalizedValue;
	nonRelationshipObjectFieldsInfo: {
		label: LocalizedValue;
		name: string;
	}[];
	objectDefinitionExternalReferenceCode: string;
	objectDefinitionId: number;
	pluralLabel: LocalizedValue;
	portletNamespace: string;
	shortName: string;
	sites: Scope[];
	storageTypes: LabelValueObject[];
}

function setAccountRelationshipFieldMandatory(
	values: Partial
) {
	const {objectFields} = values;

	const newObjectFields = objectFields?.map((field) => {
		if (field.name === values.accountEntryRestrictedObjectFieldName) {
			return {
				...field,
				required: true,
			};
		}

		return field;
	});

	return {
		...values,
		objectFields: newObjectFields,
	};
}

export default function EditObjectDetails({
	backURL,
	companies,
	dbTableName,
	hasPublishObjectPermission,
	hasUpdateObjectDefinitionPermission,
	isApproved,
	isRootDescendantNode,
	isRootNode,
	label,
	nonRelationshipObjectFieldsInfo,
	objectDefinitionExternalReferenceCode,
	objectDefinitionId,
	pluralLabel,
	portletNamespace,
	shortName,
	sites,
	storageTypes,
}: EditObjectDetailsProps) {
	const [backEndErrors, setBackEndErrors] = useState({});
	const [objectFields, setObjectFields] = useState([]);

	const {errors, handleChange, handleValidate, setValues, values} =
		useObjectDetailsForm({
			initialValues: {
				defaultLanguageId: 'en_US',
				externalReferenceCode: objectDefinitionExternalReferenceCode,
				id: objectDefinitionId,
				label,
				name: shortName,
				pluralLabel,
			},
			onSubmit: () => {},
		});

	const onSubmit = async (draft: boolean) => {
		const validationErrors = handleValidate();

		if (!Object.keys(validationErrors).length) {
			let objectDefinition = values;

			if (values.accountEntryRestricted) {
				objectDefinition = setAccountRelationshipFieldMandatory(values);
			}

			try {
				await API.save({
					item: objectDefinition,
					method: 'PUT',
					url: `/o/object-admin/v1.0/object-definitions/by-external-reference-code/${objectDefinition.externalReferenceCode}`,
				});
			}
			catch (error) {
				const {detail, title} = error as Error;

				handleErrors({detail, title}, setBackEndErrors);

				return;
			}

			if (!draft) {
				try {
					const publishResponse: any =
						await API.postObjectDefinitionPublish(
							values.id as number
						);

					if (!publishResponse.ok) {
						const errorDetails = await publishResponse.json();

						throw errorDetails;
					}
					else {
						openToast({
							message: Liferay.Language.get(
								'the-object-was-published-successfully'
							),
							type: 'success',
						});

						setTimeout(() => window.location.reload(), 1000);

						return;
					}
				}
				catch (error) {
					const {detail, title} = error as Error;

					handleErrors({detail, title}, setBackEndErrors);

					return;
				}
			}

			openToast({
				message: Liferay.Language.get(
					'the-object-was-saved-successfully'
				),
				type: 'success',
			});

			setTimeout(() => window.location.reload(), 1000);
		}
	};

	useEffect(() => {
		const makeFetch = async () => {
			const objectFieldsResponse =
				await API.getObjectDefinitionByExternalReferenceCodeObjectFields(
					objectDefinitionExternalReferenceCode
				);
			const objectDefinitionResponse =
				await API.getObjectDefinitionByExternalReferenceCode(
					objectDefinitionExternalReferenceCode
				);

			setValues(objectDefinitionResponse);
			setObjectFields(objectFieldsResponse);
		};

		makeFetch();

		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [objectDefinitionId]);

	const showSeoSection =
		Liferay.FeatureFlags['LPD-21926'] &&
		values.friendlyURLSeparator !== undefined &&
		!(
			(Liferay.FeatureFlags['LPS-135430'] &&
				values.storageType !== 'default') ||
			(!values.modifiable && values.system)
		);

	return (
		<>
			
{Liferay.FeatureFlags['LPS-135430'] && ( {Liferay.Language.get( 'external-data-source' )}
} displayType="unstyled" >
)} {values.modifiable && ( )} {showSeoSection && ( )}
); }



© 2015 - 2025 Weber Informatics LLC | Privacy Policy