From 983b3ed49af5227e48700a3de7bc464a82ccc689 Mon Sep 17 00:00:00 2001 From: Paola Machado Date: Fri, 23 Jun 2023 17:26:24 -0300 Subject: [PATCH] Convert ExpandedStory to be a functional component (#866) --- .../components/story/ExpandedStory/index.jsx | 172 ++++++++++-------- 1 file changed, 92 insertions(+), 80 deletions(-) diff --git a/app/assets/javascripts/components/story/ExpandedStory/index.jsx b/app/assets/javascripts/components/story/ExpandedStory/index.jsx index cf0a48820..896315f60 100644 --- a/app/assets/javascripts/components/story/ExpandedStory/index.jsx +++ b/app/assets/javascripts/components/story/ExpandedStory/index.jsx @@ -1,81 +1,96 @@ -import React from 'react'; -import PropTypes from 'prop-types'; -import ExpandedStoryControls from './ExpandedStoryControls'; -import ExpandedStoryDefault from './ExpandedStoryDefault'; -import ExpandedStoryRelease from './ExpandedStoryRelease'; -import { editStory, saveStory, deleteStory, cloneStory, showHistory } from '../../../actions/story'; -import { connect } from 'react-redux'; -import * as Story from '../../../models/beta/story'; -import ProjectPropTypes from '../../shapes/project'; +import React, { useEffect, useRef } from "react"; +import classNames from "classnames"; +import PropTypes from "prop-types"; +import ExpandedStoryControls from "./ExpandedStoryControls"; +import ExpandedStoryDefault from "./ExpandedStoryDefault"; +import ExpandedStoryRelease from "./ExpandedStoryRelease"; +import { + editStory, + saveStory, + deleteStory, + cloneStory, + showHistory, +} from "../../../actions/story"; +import { connect } from "react-redux"; +import * as Story from "../../../models/beta/story"; +import ProjectPropTypes from "../../shapes/project"; -export class ExpandedStory extends React.Component { - constructor(props) { - super(props); +export function ExpandedStory({ + story, + onToggle, + editStory, + saveStory, + cloneStory, + showHistory, + deleteStory, + project, + className, + title, + from, +}) { + const titleRef = useRef(); - this.titleRef = React.createRef(); - } + const loading = story._editing.loading; + const disabled = !Story.canEdit(story); - componentDidMount() { - this.titleRef.current.focus(); - } + const handleEditStory = (newAttributes) => { + editStory(story.id, newAttributes, from); + }; - render() { - const { - story, - onToggle, - editStory, - saveStory, - cloneStory, - showHistory, - deleteStory, - project, - className, - title, - from - } = this.props; + const handleSaveStory = () => { + saveStory(story.id, project.id, from); + }; - const loading = story._editing.loading ? "Story__enable-loading" : ""; - const disabled = !Story.canEdit(story); + const handleDeleteStory = () => { + deleteStory(story.id, project.id, from); + }; - return ( -
-
+ useEffect(() => { + titleRef?.current?.focus(); + }, [titleRef]); - saveStory(story.id, project.id, from)} - onDelete={() => deleteStory(story.id, project.id, from)} - canSave={Story.canSave(story)} - canDelete={Story.canDelete(story)} + return ( +
+
+ + + + {Story.isRelease(story._editing) ? ( + - - { - Story.isRelease(story._editing) - ? editStory(story.id, newAttributes, from)} - onClone={cloneStory} - disabled={disabled} - /> - : editStory(story.id, newAttributes, from)} - project={project} - onClone={cloneStory} - showHistory={showHistory} - disabled={disabled} - /> - } -
- ); - } + ) : ( + + )} +
+ ); } ExpandedStory.propTypes = { @@ -88,18 +103,15 @@ ExpandedStory.propTypes = { onToggle: PropTypes.func.isRequired, project: ProjectPropTypes.isRequired, title: PropTypes.string, - className: PropTypes.string + className: PropTypes.string, }; const mapStateToProps = ({ project }) => ({ project }); -export default connect( - mapStateToProps, - { - editStory, - saveStory, - deleteStory, - cloneStory, - showHistory - } -)(ExpandedStory); +export default connect(mapStateToProps, { + editStory, + saveStory, + deleteStory, + cloneStory, + showHistory, +})(ExpandedStory);