From 52f2627e6c1bceeff2cecbd1e3dbebafe17854ba Mon Sep 17 00:00:00 2001 From: Rafi Date: Thu, 29 Oct 2020 13:36:30 +0530 Subject: [PATCH] Refactoring EditorAction (#10894) * Refactoring EditorAction * Removing useCallback * Renaming variables to isVersionX --- .../article-form/components/EditorActions.jsx | 159 ++++++++---------- 1 file changed, 74 insertions(+), 85 deletions(-) diff --git a/app/javascript/article-form/components/EditorActions.jsx b/app/javascript/article-form/components/EditorActions.jsx index ad2b246de..acc3d438e 100644 --- a/app/javascript/article-form/components/EditorActions.jsx +++ b/app/javascript/article-form/components/EditorActions.jsx @@ -1,4 +1,5 @@ -import { h, Component } from 'preact'; +import { h } from 'preact'; +import { useState } from 'preact/hooks'; import PropTypes from 'prop-types'; import { Options } from './Options'; import { Button } from '@crayons'; @@ -18,102 +19,90 @@ const Icon = () => ( ); -export class EditorActions extends Component { - constructor(props) { - super(props); - this.state = { - moreConfigShowing: false, - }; - } +export const EditorActions = ({ + onSaveDraft, + onPublish, + onClearChanges, + published, + edited, + version, + passedData, + onConfigChange, + submitting, +}) => { + const [moreConfigShowing, setMoreConfig] = useState(false); - setCommonProps = ({ moreConfigShowing = false }) => { - return { - moreConfigShowing, - }; - }; + const isVersion1 = version === 'v1'; + const isVersion2 = version === 'v2'; - toggleMoreConfig = (e) => { - const { moreConfigShowing } = this.state; + const toggleMoreConfig = (e) => { e.preventDefault(); - this.setState({ - ...this.setCommonProps({ moreConfigShowing: !moreConfigShowing }), - }); + setMoreConfig(!moreConfigShowing); }; - render() { - const { - onSaveDraft, - onPublish, - onClearChanges, - published, - edited, - version, - passedData, - onConfigChange, - submitting, - } = this.props; - - const { moreConfigShowing } = this.state; - - return submitting ? ( + if (submitting) { + return (
- ) : ( -
- - - {published || version === 'v1' ? ( - '' - ) : ( - - )} - {version === 'v2' && ( -
-
- )} - {edited && ( - - )} -
); } -} + + return ( +
+ + + {!(published || isVersion1) && ( + + )} + + {isVersion2 && ( +
+
+ )} + + {edited && ( + + )} +
+ ); +}; EditorActions.propTypes = { onSaveDraft: PropTypes.func.isRequired,