From 334d7473343b1be99c94d13b9e16aa8680d984a0 Mon Sep 17 00:00:00 2001 From: Tisha Soumya Date: Tue, 19 Sep 2023 16:46:26 +0530 Subject: [PATCH] Refactor Content Property Model (#4968) Co-authored-by: nileshgulia1 --- news/4968.feature | 1 + .../Contents/ContentsPropertiesModal.jsx | 235 +++++++----------- 2 files changed, 88 insertions(+), 148 deletions(-) create mode 100644 news/4968.feature diff --git a/news/4968.feature b/news/4968.feature new file mode 100644 index 0000000000..20340aa2eb --- /dev/null +++ b/news/4968.feature @@ -0,0 +1 @@ +Refactor ContentsPropertiesModal -@Tishasoumya-02 diff --git a/src/components/manage/Contents/ContentsPropertiesModal.jsx b/src/components/manage/Contents/ContentsPropertiesModal.jsx index 2a0391b7a8..91ce72d48b 100644 --- a/src/components/manage/Contents/ContentsPropertiesModal.jsx +++ b/src/components/manage/Contents/ContentsPropertiesModal.jsx @@ -1,15 +1,10 @@ -/** - * Contents properties modal. - * @module components/manage/Contents/ContentsPropertiesModal - */ - -import React, { Component } from 'react'; +import React, { useEffect } from 'react'; import PropTypes from 'prop-types'; -import { connect } from 'react-redux'; -import { compose } from 'redux'; +import { useDispatch, useSelector } from 'react-redux'; import { isEmpty, map } from 'lodash'; -import { defineMessages, injectIntl } from 'react-intl'; +import { defineMessages, useIntl } from 'react-intl'; +import { usePrevious } from '@plone/volto/helpers'; import { updateContent } from '@plone/volto/actions'; import { ModalForm } from '@plone/volto/components'; @@ -72,152 +67,96 @@ const messages = defineMessages({ }, }); -/** - * ContentsPropertiesModal class. - * @class ContentsPropertiesModal - * @extends Component - */ -class ContentsPropertiesModal extends Component { - /** - * Property types. - * @property {Object} propTypes Property types. - * @static - */ - static propTypes = { - updateContent: PropTypes.func.isRequired, - items: PropTypes.arrayOf(PropTypes.string).isRequired, - request: PropTypes.shape({ - loading: PropTypes.bool, - loaded: PropTypes.bool, - }).isRequired, - open: PropTypes.bool.isRequired, - onOk: PropTypes.func.isRequired, - onCancel: PropTypes.func.isRequired, - }; - - /** - * Constructor - * @method constructor - * @param {Object} props Component properties - * @constructs ContentsUploadModal - */ - constructor(props) { - super(props); - this.onSubmit = this.onSubmit.bind(this); - } +const ContentsPropertiesModal = (props) => { + const { onCancel, onOk, open, items } = props; + const intl = useIntl(); + const dispatch = useDispatch(); + const request = useSelector((state) => state.content.update); + const prevrequestloading = usePrevious(request.loading); - /** - * Component will receive props - * @method componentWillReceiveProps - * @param {Object} nextProps Next properties - * @returns {undefined} - */ - UNSAFE_componentWillReceiveProps(nextProps) { - if (this.props.request.loading && nextProps.request.loaded) { - this.props.onOk(); + useEffect(() => { + if (prevrequestloading && request.loaded) { + onOk(); } - } + }, [onOk, prevrequestloading, request.loaded]); - /** - * Submit handler - * @method onSubmit - * @param {Object} data Form data - * @returns {undefined} - */ - onSubmit(data) { + const onSubmit = (data) => { if (isEmpty(data)) { - this.props.onOk(); + onOk(); } else { - this.props.updateContent( - this.props.items, - map(this.props.items, () => data), + dispatch( + updateContent( + items, + map(items, () => data), + ), ); } - } + }; - /** - * Render method. - * @method render - * @returns {string} Markup for the component. - */ - render() { - return ( - this.props.open && ( - - ) - ); - } -} + }, + required: [], + }} + /> + ) + ); +}; -export default compose( - injectIntl, - connect( - (state) => ({ - request: state.content.update, - }), - { updateContent }, - ), -)(ContentsPropertiesModal); +ContentsPropertiesModal.propTypes = { + items: PropTypes.arrayOf(PropTypes.string).isRequired, + open: PropTypes.bool.isRequired, + onOk: PropTypes.func.isRequired, + onCancel: PropTypes.func.isRequired, +}; +export default ContentsPropertiesModal;