Skip to content

Commit

Permalink
Convert ExpandedStoryTask to be a functional component (#886)
Browse files Browse the repository at this point in the history
* Convert ExpandedStoryTask to be a functional component

* test: Remove WrapperInstance

---------

Co-authored-by: Talysson de Oliveira Cassiano <[email protected]>
  • Loading branch information
gabrielcicero45 and talyssonoc authored Nov 6, 2023
1 parent dc9af32 commit 8e0c0b1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,88 +1,56 @@
import React, { Component } from 'react';
import React, { useState } from 'react';
import TasksList from '../task/TasksList';
import PropTypes from 'prop-types';
import { editingStoryPropTypesShape } from '../../../models/beta/story';
import ExpandedStorySection from './ExpandedStorySection';

class ExpandedStoryTask extends Component {
constructor(props) {
super(props);
this.state = {
task: ''
};
this.onInputChange = this.onInputChange.bind(this);
this.onHandleSubmit = this.onHandleSubmit.bind(this);
}
const ExpandedStoryTask = ({ story, onToggle, onDelete, onSave, disabled }) => {
const [task, setTask] = useState('');

onInputChange(e) {
this.setState({
task: e.target.value
});
}
const onInputChange = (e) => {
setTask(e.target.value);
};

onHandleSubmit() {
const { onSave } = this.props;

const task = this.state.task;
const onHandleSubmit = () => {
onSave(task);
this.setState({
task: ''
});
}

hasAnEmptyValue() {
return !this.state.task.trim()
}

render() {
const { story, onToggle, onDelete, disabled } = this.props;

if (disabled && !story.tasks.length) return null;

return (
<ExpandedStorySection
title={I18n.t('story.tasks')}
identifier="tasks"
>
<div className="list-task" >
<TasksList
tasks={story.tasks}
onDelete={onDelete}
onToggle={onToggle}
disabled={disabled}
/>
setTask('');
};

const hasAnEmptyValue = () => {
return !task.trim();
};

if (disabled && !story.tasks.length) return null;

return (
<ExpandedStorySection title={I18n.t('story.tasks')} identifier="tasks">
<div className="list-task">
<TasksList tasks={story.tasks} onDelete={onDelete} onToggle={onToggle} disabled={disabled} />
</div>

{!disabled && (
<div className="task-form">
<input value={task} className="form-control input-sm" onChange={onInputChange} />
<button
type="submit"
className="add-task-button"
onClick={onHandleSubmit}
disabled={hasAnEmptyValue()}
>
{I18n.t('add task')}
</button>
</div>

{
!disabled && (
<div className="task-form">
<input
value={this.state.task}
className="form-control input-sm"
onChange={this.onInputChange}
/>
<button
type='submit'
className='add-task-button'
onClick={this.onHandleSubmit}
disabled={this.hasAnEmptyValue()}
>
{I18n.t('add task')}
</button>
</div>
)
}
</ExpandedStorySection>
);
}
)}
</ExpandedStorySection>
);
};

ExpandedStoryTask.propTypes = {
story: editingStoryPropTypesShape.isRequired,
onToggle: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
onSave: PropTypes.func.isRequired,
disabled: PropTypes.bool.isRequired
disabled: PropTypes.bool.isRequired,
};

export default ExpandedStoryTask;
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ describe('<ExpandedStoryTask />', () => {
});

const wrapper = mount(<ExpandedStoryTask {...defaultProps()} />);
const wrapperInstance = wrapper.instance();
const button = wrapper.find('button');
const input = wrapper.find('input');

return { wrapper, wrapperInstance, button, input };
return { wrapper, button, input };
};

it('renders component title', () => {
Expand Down Expand Up @@ -59,14 +58,6 @@ describe('<ExpandedStoryTask />', () => {

expect(onSaveSpy).toHaveBeenCalledWith(task);
});

it('calls setState with a empty task', () => {
const { wrapperInstance } = setup();

wrapperInstance.onHandleSubmit();

expect(wrapperInstance.state.task).toEqual('');
});
});
});

Expand Down

0 comments on commit 8e0c0b1

Please sign in to comment.