From 42b9518d24568aba831ca1274a13f40b5cc30793 Mon Sep 17 00:00:00 2001
From: Bhavishya Sahay <145124737+BhavishyaSahay@users.noreply.github.com>
Date: Sun, 28 Jul 2024 12:56:01 +0530
Subject: [PATCH] Refactored ContentTypeSchema.jsx to functional based
components
---
.../Controlpanels/ContentTypeSchema.jsx | 330 ++++++------------
1 file changed, 116 insertions(+), 214 deletions(-)
diff --git a/packages/volto/src/components/manage/Controlpanels/ContentTypeSchema.jsx b/packages/volto/src/components/manage/Controlpanels/ContentTypeSchema.jsx
index 6aaeeff4ab..01bd55d66b 100644
--- a/packages/volto/src/components/manage/Controlpanels/ContentTypeSchema.jsx
+++ b/packages/volto/src/components/manage/Controlpanels/ContentTypeSchema.jsx
@@ -1,23 +1,19 @@
-/**
- * Content type schema.
- * @module components/manage/Controlpanels/ContentTypeSchema
- */
+import React, { useEffect, useRef, useState } from 'react';
+import { defineMessages, useIntl } from 'react-intl';
+import { connect } from 'react-redux';
+import { compose } from 'redux';
+import { toast } from 'react-toastify';
+import { createPortal } from 'react-dom';
+import { nth } from 'lodash';
+import PropTypes from 'prop-types';
+import { Button, Header } from 'semantic-ui-react';
import { getSchema, putSchema } from '@plone/volto/actions';
import { getParentUrl } from '@plone/volto/helpers';
-import { nth } from 'lodash';
import { Error, Icon, Toast, Toolbar } from '@plone/volto/components';
import { Form } from '@plone/volto/components/manage/Form';
import clearSVG from '@plone/volto/icons/clear.svg';
import saveSVG from '@plone/volto/icons/save.svg';
-import PropTypes from 'prop-types';
-import React, { Component } from 'react';
-import { defineMessages, injectIntl } from 'react-intl';
-import { createPortal } from 'react-dom';
-import { connect } from 'react-redux';
-import { toast } from 'react-toastify';
-import { compose } from 'redux';
-import { Button, Header } from 'semantic-ui-react';
const messages = defineMessages({
title: {
@@ -54,149 +50,60 @@ const messages = defineMessages({
},
});
-/**
- * ContentTypeSchema class.
- * @class ContentTypeSchema
- * @extends Component
- */
-class ContentTypeSchema extends Component {
- /**
- * Property types.
- * @property {Object} propTypes Property types.
- * @static
- */
- static propTypes = {
- getSchema: PropTypes.func.isRequired,
- putSchema: PropTypes.func.isRequired,
- pathname: PropTypes.string.isRequired,
- id: PropTypes.string.isRequired,
- };
+const ContentTypeSchema = (props) => {
+ const { getSchema, putSchema, pathname, id, schema, schemaRequest, history } =
+ props;
- /**
- * Default properties
- * @property {Object} defaultProps Default properties.
- * @static
- */
- static defaultProps = {};
+ const [error, setError] = useState(null);
+ const [isClient, setIsClient] = useState(false);
- /**
- * Constructor
- * @method constructor
- * @param {Object} props Component properties
- * @constructs WysiwygEditor
- */
- constructor(props) {
- super(props);
- this.state = {
- error: null,
- schema: null,
- content: null,
- isClient: false,
- };
+ const formRef = useRef();
+ const intl = useIntl();
- this.onCancel = this.onCancel.bind(this);
- this.onSubmit = this.onSubmit.bind(this);
- this.form = React.createRef();
- }
+ useEffect(() => {
+ getSchema(id);
+ setIsClient(true);
+ }, [getSchema, id]);
- /**
- * Component did mount
- * @method componentDidMount
- * @returns {undefined}
- */
- componentDidMount() {
- this.props.getSchema(this.props.id);
- this.setState({ isClient: true });
- }
-
- /**
- * Component will receive props
- * @method componentWillReceiveProps
- * @param {Object} nextProps Next properties
- * @returns {undefined}
- */
- UNSAFE_componentWillReceiveProps(nextProps) {
- // Schema error
- if (this.props.schemaRequest.loading && nextProps.schemaRequest.error) {
- this.setState({
- error: nextProps.schemaRequest.error,
- });
+ useEffect(() => {
+ if (schemaRequest.loading && schemaRequest.error) {
+ setError(schemaRequest.error);
}
- // Schema GET
- if (this.props.schemaRequest.loading && nextProps.schemaRequest.loaded) {
- let properties = nextProps.schema?.properties || {};
- let content = {};
- let value, key;
- for (key in properties) {
- value = properties[key].default;
- if (value) {
- content[key] = value;
- }
- }
-
- this.setState({
- schema: nextProps.schema,
- content: content,
- });
- }
-
- // Schema updated
- if (
- this.props.schemaRequest.put.loading &&
- nextProps.schemaRequest.put.loaded
- ) {
- // this.props.getSchema(this.props.id);
+ if (schemaRequest.put.loading && schemaRequest.put.loaded) {
toast.info(