Skip to content

Commit

Permalink
added warning window when cancelling an expanded story
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielRMuller committed Feb 11, 2019
1 parent 08e4234 commit 928e8d5
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
import React from 'react';

const ExpandedStoryControls = ({ onCancel, onSave, onDelete, readOnly }) => {
return (
<div className="form-group Story__controls">
<input className="save"
onClick={onSave}
type="button"
value={I18n.t('save')}
disabled={readOnly}
/>

<input className="delete"
onClick={() => {
if (window.confirm(I18n.t('story destroy confirm'))){
onDelete();
}
}}
type="button"
value={I18n.t('delete')}
disabled={readOnly}
/>

<input className="cancel"
onClick={onCancel}
type="button"
value={I18n.t('cancel')}
/>
</div>
);
import React, { Component } from 'react';

class ExpandedStoryControls extends Component {
constructor(props) {
super(props);

this.handleCancel = this.handleCancel.bind(this);
}

handleCancel() {
const { onCancel, isDirty } = this.props;

if (isDirty && !window.confirm(I18n.t('story unsaved changes'))) {
return;
}
onCancel();
}

render() {
const { onSave, onDelete, readOnly } = this.props;

return (
<div className="form-group Story__controls">
<input className="save"
onClick={onSave}
type="button"
value={I18n.t('save')}
disabled={readOnly}
/>

<input className="delete"
onClick={() => {
if (window.confirm(I18n.t('story destroy confirm'))){
onDelete();
}
}}
type="button"
value={I18n.t('delete')}
disabled={readOnly}
/>

<input className="cancel"
onClick={this.handleCancel}
type="button"
value={I18n.t('cancel')}
/>
</div>
);
}
};

export default ExpandedStoryControls;
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const ExpandedStory = ({
<div className="Story Story--expanded">
<ExpandedStoryControls
onCancel={onToggle}
isDirty={story._editing._isDirty}
onSave={() => updateStory(story, project.id)}
onDelete={() => deleteStory(story.id, project.id)}
readOnly={Story.isAccepted(story)}
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ en:
reset_tour_success: "Tour was successfully reset"
reset_tour_fail: "An error has ocurred, please try again"
story destroy confirm: "Are you sure you want to destroy this story?"
story unsaved changes: "You have unsaved changes, are you sure you want to cancel?"
stories_destroy_success: "Stories were successfully destroyed"
stories_destroy_fail: "Stories couldn't be destroyed due some validation errors"
update_stories_successfully: "Stories were successfully updated"
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ es:
reset_tour_success: "La excursión se restableció correctamente"
reset_tour_fail: "Ha ocurrido un error, por favor intente otra vez"
story destroy confirm: "¿Estás seguro de que quieres destruir esta historia?"
story unsaved changes: "¿Usted tiene cambios no guardados, está seguro de que desea cancelar?"
stories_destroy_success: "Las historias fueron destruidas con éxito"
stories_destroy_fail: "Las historias no se pudieron destruir debido a algunos errores de validación"
update_stories_successfully: "Las historias se actualizaron con éxito"
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pt-BR:
reset_tour_success: "A tour foi resetada com sucesso"
reset_tour_fail: "Ocorreu um erro, por favor tente novamente"
story destroy confirm: "Você tem certeza que deseja excluir esta história?"
story unsaved changes: "Você tem mudanças não salvas, tem certeza que deseja cancelar?"
stories_destroy_success: "As histórias foram destruídas com sucesso"
stories_destroy_fail: "As histórias não puderam ser destruídas por alguns erros de validação"
update_stories_successfully: "As histórias foram atualizadas com sucesso"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,60 @@ describe('<ExpandedStoryControls />', () => {
});
});

describe('when the user click on cancell', () => {
describe('when the user click on cancel', () => {
beforeEach(function() {
sinon.stub(window, 'confirm')
});

afterEach(function() {
window.confirm.restore();
});

it('triggers the toggle callback', () => {
const onCancel = sinon.spy();
const handleCancel = sinon.spy();

const wrapper = shallow(
<ExpandedStoryControls
readOnly={false}
onCancel={handleCancel}
/>
);

wrapper.find('.cancel').simulate('click');

expect(handleCancel).toHaveBeenCalled();
});

it('triggers a warning window when there is unsaved changes', () => {
const handleCancel = sinon.spy();

const wrapper = shallow(
<ExpandedStoryControls
readOnly={false}
isDirty={true}
onCancel={handleCancel}
/>
);

wrapper.find('.cancel').simulate('click');

expect(window.confirm).toHaveBeenCalled();
});

it('does not trigger a warning window when no changes were made', () => {
const handleCancel = sinon.spy();

const wrapper = shallow(
<ExpandedStoryControls
readOnly={false}
onCancel={onCancel}
isDirty={false}
onCancel={handleCancel}
/>
);

wrapper.find('.cancel').simulate('click');

expect(onCancel).toHaveBeenCalled();
expect(window.confirm).not.toHaveBeenCalled();
});
});

Expand Down

0 comments on commit 928e8d5

Please sign in to comment.