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

META-INF.resources.js.data_set.details.Details.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 ClayButton from '@clayui/button';
import ClayForm, {ClayInput} from '@clayui/form';
import ClayIcon from '@clayui/icon';
import ClayLayout from '@clayui/layout';
import ClayLink from '@clayui/link';
import ClayList from '@clayui/list';
import {ClayTooltipProvider} from '@clayui/tooltip';
import classNames from 'classnames';
import {LearnMessage} from 'frontend-js-components-web';
import {fetch, navigate, sub} from 'frontend-js-web';
import React, {useRef, useState} from 'react';

import {IDataSet} from '../..//utils/types';
import RequiredMark from '../../components/RequiredMark';
import {API_URL, DEFAULT_FETCH_HEADERS} from '../../utils/constants';
import getAPIExplorerURL from '../../utils/getAPIExplorerURL';
import openDefaultFailureToast from '../../utils/openDefaultFailureToast';
import openDefaultSuccessToast from '../../utils/openDefaultSuccessToast';
import {IDataSetSectionProps} from '../DataSet';

const getURLPreview = ({
	additionalAPIURLParameters = '',
	restApplication,
	restEndpoint,
}: {
	additionalAPIURLParameters: IDataSet['additionalAPIURLParameters'];
	restApplication: IDataSet['restApplication'];
	restEndpoint: IDataSet['restEndpoint'];
}) => {
	const encodedAdditionalAPIURLParameters = encodeURI(
		additionalAPIURLParameters.trim()
	);

	// This also removes the version (for example: `/v1.0`) in the rest endpoint
	// to avoid repeating the version when combining the restApplication and
	// restEndpoint.

	return (
		restApplication +
		restEndpoint
			.split('/')
			.filter((_, index) => index !== 1)
			.join('/') +
		'?' +
		encodedAdditionalAPIURLParameters
	);
};

const Details = ({
	backURL,
	dataSet,
	namespace,
	onDataSetUpdate,
}: IDataSetSectionProps) => {
	const [labelValidationError, setLabelValidationError] = useState(false);

	const [urlPreview, setURLPreview] = useState(
		getURLPreview({
			additionalAPIURLParameters: dataSet.additionalAPIURLParameters,
			restApplication: dataSet.restApplication,
			restEndpoint: dataSet.restEndpoint,
		})
	);

	const descriptionRef = useRef(null);
	const labelRef = useRef(null);
	const parametersRef = useRef(null);

	const handleKeyUpParameters = (
		event: React.ChangeEvent
	) => {
		setURLPreview(
			getURLPreview({
				additionalAPIURLParameters: event.currentTarget.value,
				restApplication: dataSet.restApplication,
				restEndpoint: dataSet.restEndpoint,
			})
		);
	};

	const updateFDSView = async () => {
		const body = {
			additionalAPIURLParameters: parametersRef.current?.value,
			description: descriptionRef.current?.value,
			label: labelRef.current?.value,
		};

		const response = await fetch(
			`${API_URL.DATA_SETS}/by-external-reference-code/${dataSet.externalReferenceCode}`,
			{
				body: JSON.stringify(body),
				headers: DEFAULT_FETCH_HEADERS,
				method: 'PATCH',
			}
		);

		if (!response.ok) {
			openDefaultFailureToast();

			return;
		}

		const responseJSON = await response.json();

		if (responseJSON?.id) {
			openDefaultSuccessToast();

			const controlMenuHeaderTitles = document.getElementsByClassName(
				'control-menu-level-1-heading'
			);

			if (controlMenuHeaderTitles.length === 1) {
				controlMenuHeaderTitles[0].innerHTML = Liferay.Util.escapeHTML(
					labelRef.current?.value ?? ''
				);
			}
			onDataSetUpdate(responseJSON);
		}
		else {
			openDefaultFailureToast();
		}
	};

	const {restApplication, restEndpoint, restSchema} = dataSet;

	const apiExplorerURL = getAPIExplorerURL(restApplication);

	return (
		
			
				
					

{Liferay.Language.get('details')}

setLabelValidationError( !labelRef.current?.value ) } ref={labelRef} type="text" /> {labelValidationError && ( {Liferay.Language.get( 'this-field-is-required' )} )}

{Liferay.Language.get('rest-information')}

{Liferay.Language.get('application')} {restApplication} {Liferay.Language.get('schema')} {restSchema} {Liferay.Language.get('endpoint')} {restEndpoint}

{Liferay.Language.get('advanced-optional-parameters')}

{Liferay.Language.get('save')} navigate(backURL)} > {Liferay.Language.get('cancel')}
); }; export default Details;




© 2015 - 2025 Weber Informatics LLC | Privacy Policy