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

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

/**
 * 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 {Provider as ClayIconProvider} from '@clayui/core';
import ClayLink from '@clayui/link';
import ClayLoadingIndicator from '@clayui/loading-indicator';
import {fetch} from 'frontend-js-web';
import React, {useEffect, useState} from 'react';

import {AnalyticsReportsProvider} from '../AnalyticsReportsContext';
import {AssetTypes} from '../types/global';
import EmptyState from './EmptyState';

interface IAppSetupProps extends React.HTMLAttributes {
	contentPerformanceDataFetchURL: string;
}

type Data = {
	analyticsSettingsPortletURL: string;
	assetId: string;
	assetLibrary: boolean;
	assetType: AssetTypes | null;
	connectedToAnalyticsCloud: boolean;
	connectedToAssetLibrary: boolean;
	groupId: string;
	isAdmin: boolean;
	siteEditDepotEntryDepotAdminPortletURL: string;
	siteSyncedToAnalyticsCloud: boolean;
};

const AppSetup: React.FC = ({
	children,
	contentPerformanceDataFetchURL,
}) => {
	const [data, setData] = useState(null);
	const [loading, setLoading] = useState(true);
	const [error, setError] = useState('');

	useEffect(() => {
		async function fetchData() {
			try {
				const response = await fetch(contentPerformanceDataFetchURL, {
					method: 'GET',
				});

				const data = await response.json();

				if (data.error) {
					throw new Error(data.error);
				}

				setData(data);
				setLoading(false);
				setError('');
			}
			catch (error: any) {
				console.error(error);

				setData(null);
				setLoading(false);
				setError(error.toString());
			}
		}

		fetchData();
	}, [contentPerformanceDataFetchURL]);

	if (loading) {
		return (
			
		);
	}

	if (error) {
		return ;
	}

	if (data && !data.connectedToAnalyticsCloud) {
		if (data.isAdmin) {
			return (
				
					
						{Liferay.Language.get('connect')}
					
				
			);
		}

		return (
			
		);
	}

	if (data && data.assetLibrary && !data.connectedToAssetLibrary) {
		if (data.isAdmin) {
			return (
				
					
						{Liferay.Language.get('connect')}
					
				
			);
		}

		return (
			
		);
	}

	if (data && !data.siteSyncedToAnalyticsCloud) {
		if (data.isAdmin) {
			return (
				
					
						{Liferay.Language.get('sync')}
					
				
			);
		}

		return (
			
		);
	}

	return (
		
			
				{children}
			
		
	);
};

export default AppSetup;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy