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

META-INF.resources.js.FieldsTable.js Maven / Gradle / Ivy

There is a newer version: 1.0.101
Show 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 ClayAlert from '@clayui/alert';
import {ClayCheckbox} from '@clayui/form';
import ClayIcon from '@clayui/icon';
import ClayLabel from '@clayui/label';
import ClayTable from '@clayui/table';
import {ClayTooltipProvider} from '@clayui/tooltip';
import {sub} from 'frontend-js-web';
import React, {useEffect, useRef, useState} from 'react';

import {
	CSV_FORMAT,
	DISALLOWED_CSV_ENTITY_TYPES,
	EXPORT_FILE_FORMAT_SELECTED_EVENT,
	SCHEMA_SELECTED_EVENT,
	TEMPLATE_SELECTED_EVENT,
	TEMPLATE_SOILED_EVENT,
} from './constants';
import getFieldsFromSchema from './getFieldsFromSchema';

function FieldsTable({portletNamespace}) {
	const [fields, setFields] = useState([]);
	const [selectedExportFileFormat, setSelectedExportFileFormat] =
		useState('');
	const [selectedFields, setSelectedFields] = useState([]);
	const useTemplateMappingRef = useRef();

	const isForbidden = (field) =>
		field.unsupportedFormats?.includes(selectedExportFileFormat);

	useEffect(() => {
		const handleSchemaUpdated = (event) => {
			if (event.schema) {
				const newFields = getFieldsFromSchema(event.schema);

				const formattedFields = [
					...newFields.required,
					...newFields.optional,
				];

				setFields(formattedFields);

				if (!useTemplateMappingRef.current) {
					setSelectedFields(formattedFields);
				}
			}
			else {
				setFields([]);

				if (!useTemplateMappingRef.current) {
					setSelectedFields([]);
				}
			}
		};

		const handleTemplateUpdate = ({template}) => {
			if (template) {
				useTemplateMappingRef.current = true;

				setSelectedFields(
					Object.keys(template.mappings).map((fields) => ({
						name: fields,
					}))
				);
			}
			else {
				useTemplateMappingRef.current = false;
			}
		};

		const handleTemplateSoiled = () => {
			useTemplateMappingRef.current = false;
		};

		const handleExportFileFormatUpdated = ({
			selectedExportFileFormat,
			selectedSchema,
		}) => {
			setSelectedExportFileFormat(selectedExportFileFormat);
			if (
				selectedExportFileFormat === CSV_FORMAT.toUpperCase() &&
				DISALLOWED_CSV_ENTITY_TYPES.includes(selectedSchema)
			) {
				setFields([]);
				setSelectedFields([]);
			}
		};

		Liferay.on(
			EXPORT_FILE_FORMAT_SELECTED_EVENT,
			handleExportFileFormatUpdated
		);
		Liferay.on(SCHEMA_SELECTED_EVENT, handleSchemaUpdated);
		Liferay.on(TEMPLATE_SELECTED_EVENT, handleTemplateUpdate);
		Liferay.on(TEMPLATE_SOILED_EVENT, handleTemplateSoiled);

		return () => {
			Liferay.detach(
				EXPORT_FILE_FORMAT_SELECTED_EVENT,
				handleExportFileFormatUpdated
			);
			Liferay.detach(SCHEMA_SELECTED_EVENT, handleSchemaUpdated);
			Liferay.detach(TEMPLATE_SELECTED_EVENT, handleTemplateUpdate);
			Liferay.detach(TEMPLATE_SOILED_EVENT, handleTemplateSoiled);
		};
	}, []);

	if (!fields.length) {
		return null;
	}

	return (
		
{Liferay.Language.get('fields')}
{Liferay.Language.get( 'select-fields-to-include-in-the-exported-file' )}
{ if ( selectedFields.length === fields.length ) { setSelectedFields([]); } else { setSelectedFields(fields); } }} /> {Liferay.Language.get('attribute-code')} {fields.map((field) => { const included = !isForbidden(field) && selectedFields.some( (selectedField) => selectedField.name === field.name ); return ( { Liferay.fire( TEMPLATE_SOILED_EVENT ); if (included) { setSelectedFields( selectedFields.filter( (selected) => selected.name !== field.name ) ); } else { setSelectedFields([ ...selectedFields, field, ]); } }} value={field.name} /> {isForbidden(field) && ( <> {Liferay.Language.get( 'not-supported' )} )} ); })}
); } export default FieldsTable;




© 2015 - 2025 Weber Informatics LLC | Privacy Policy