import React, { useState } from 'react'; import PropTypes from 'prop-types'; import { Duplicate } from '@buffetjs/icons'; import { Label, Padded, Text } from '@buffetjs/core'; import Select from 'react-select'; import { useDispatch } from 'react-redux'; import { useTheme } from 'styled-components'; import { useIntl } from 'react-intl'; import { BaselineAlignment, DropdownIndicator, ModalConfirm, selectStyles, useContentManagerEditViewDataManager, request, } from 'strapi-helper-plugin'; import { getTrad } from '../../utils'; import { cleanData, generateOptions } from './utils'; const CMEditViewCopyLocale = ({ appLocales, currentLocale, localizations, readPermissions }) => { const { formatMessage } = useIntl(); const dispatch = useDispatch(); const { allLayoutData, slug } = useContentManagerEditViewDataManager(); const [isLoading, setIsLoading] = useState(false); const [isOpen, setIsOpen] = useState(false); const [value, setValue] = useState(null); const theme = useTheme(); const handleConfirmCopyLocale = async () => { if (!value) { handleToggle(); return; } const requestURL = `/content-manager/collection-types/${slug}/${value.value}`; try { setIsLoading(true); const response = await request(requestURL, { method: 'GET' }); const cleanedData = cleanData(response, allLayoutData, localizations); dispatch({ type: 'ContentManager/CrudReducer/GET_DATA_SUCCEEDED', data: cleanedData }); strapi.notification.toggle({ type: 'success', message: { id: getTrad('CMEditViewCopyLocale.copy-success'), defaultMessage: 'Locale copied!', }, }); } catch (err) { console.error(err); strapi.notification.toggle({ type: 'warning', message: { id: getTrad('CMEditViewCopyLocale.copy-failure'), defaultMessage: 'Failed to copy locale', }, }); } finally { setIsLoading(false); handleToggle(); } }; const handleChange = value => { setValue(value); }; const handleToggle = () => { setIsOpen(prev => !prev); }; if (!localizations.length) { return null; } const options = generateOptions(appLocales, currentLocale, localizations, readPermissions); const styles = selectStyles(theme); return ( <> {formatMessage({ id: getTrad('CMEditViewCopyLocale.copy-text'), defaultMessage: 'Fill in from another locale', })}