@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
84 lines (82 loc) • 3.58 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2023 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { EnhancedDialog } from '../EnhancedDialog';
import { FormattedMessage } from 'react-intl';
import React, { useEffect, useState } from 'react';
import RenameContentDialogContainer from './RenameContentDialogContainer';
import { fetchDependant } from '../../services/dependencies';
import useActiveSiteId from '../../hooks/useActiveSiteId';
import { parseLegacyItemToDetailedItem } from '../../utils/content';
import useWithPendingChangesCloseRequest from '../../hooks/useWithPendingChangesCloseRequest';
import { ensureSingleSlash, isBlank } from '../../utils/string';
export function RenameContentDialog(props) {
const { path, value, onRenamed, onSubmittingAndOrPendingChange, ...dialogProps } = props;
const [dependantItems, setDependantItems] = useState(null);
const [fetchingDependantItems, setFetchingDependantItems] = useState(false);
const [error, setError] = useState(null);
const siteId = useActiveSiteId();
const pendingChangesCloseRequest = useWithPendingChangesCloseRequest(dialogProps.onClose);
useEffect(() => {
if (!isBlank(value) && !isBlank(path)) {
setFetchingDependantItems(true);
fetchDependant(siteId, ensureSingleSlash(`${path}/${value}`)).subscribe({
next: (response) => {
const dependants = parseLegacyItemToDetailedItem(response);
setDependantItems(dependants);
setFetchingDependantItems(false);
},
error: ({ response }) => {
setError(response);
setFetchingDependantItems(false);
}
});
}
}, [path, value, siteId]);
return React.createElement(
EnhancedDialog,
{
title: React.createElement(FormattedMessage, { defaultMessage: 'Rename Content' }),
onWithPendingChangesCloseRequest: pendingChangesCloseRequest,
maxWidth: dependantItems?.length > 0 ? 'md' : 'xs',
...dialogProps
},
React.createElement(RenameContentDialogContainer, {
path: path,
value: value,
dependantItems: dependantItems,
fetchingDependantItems: fetchingDependantItems,
onSubmittingAndOrPendingChange: onSubmittingAndOrPendingChange,
onRenamed: onRenamed,
error: error
})
);
}
export default RenameContentDialog;