Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable save changes button until a "save-able" change has been made;… #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,20 @@ class ImageSequence extends React.Component {
super(props);
this.state = {
choiceId: null,
hasChanged: false
};
}

onUpdateChoice = (itemId, choiceId, newChoice, fileIds) => {
this.setState({ hasChanged: true });
this.props.updateChoice(itemId, choiceId, newChoice, fileIds);
}

onSave = () => {
this.setState({ hasChanged: false });
this.props.save();
}

getFeedback() {
const { question } = this.props.item;
const strings = this.props.localizeStrings('imageSequence');
Expand Down Expand Up @@ -63,6 +74,21 @@ class ImageSequence extends React.Component {
}

render() {
let saveOptions = (
<SaveOptions
save={this.onSave}
disabled
/>
);

if (this.state.hasChanged) {
saveOptions = (
<SaveOptions
save={this.onSave}
/>
);
}

return (
<div style={{ display: this.props.isActive ? 'block' : 'none' }}>
<ImageOrder
Expand All @@ -71,10 +97,10 @@ class ImageSequence extends React.Component {
activeChoice={this.state.activeChoice}
deleteChoice={this.props.deleteChoice}
item={this.props.item}
updateChoice={this.props.updateChoice}
updateChoice={this.onUpdateChoice}
duplicateAnswers={this.props.duplicateAnswers}
/>
<SaveOptions save={this.props.save} />
{saveOptions}
<div className="au-c-question__feedback">
{ this.getFeedback() }
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('image sequence component', () => {

it('renders the component', () => {
expect(result.find('.au-c-question__feedback').length).toBe(2);
expect(result.state().hasChanged).toEqual(false);
});

it('renders two Feedback components', () => {
Expand All @@ -66,4 +67,16 @@ describe('image sequence component', () => {
feedback.at(0).nodes[0].props.updateItem();
expect(calledFunc).toBeTruthy();
});

it('changes state when call onUpdateChoice', () => {
expect(result.state().hasChanged).toEqual(false);
result.instance().onUpdateChoice();
expect(result.state().hasChanged).toEqual(true);
});

it('sets disabled flag correctly on the Save button', () => {
expect(result.find({ disabled: true }).length).toEqual(1);
result.setState({ hasChanged: true });
expect(result.find({ disabled: true }).length).toEqual(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,42 @@ class MovableWordSentence extends React.Component {
duplicateAnswers: React.PropTypes.arrayOf(React.PropTypes.string),
};

constructor(props) {
super(props);
this.state = {
hasChanged: false
};
}

onUpdateChoice = (itemId, choiceId, newChoice, fileIds) => {
this.setState({ hasChanged: true });
this.props.updateChoice(itemId, choiceId, newChoice, fileIds);
}

onSave = () => {
this.setState({ hasChanged: false });
this.props.save();
}

render() {
const { question, id } = this.props.item;
const strings = this.props.localizeStrings('movableWordSentence');

let saveOptions = (
<SaveOptions
save={this.onSave}
disabled
/>
);

if (this.state.hasChanged) {
saveOptions = (
<SaveOptions
save={this.onSave}
/>
);
}

return (
<div>
<div
Expand All @@ -44,7 +76,7 @@ class MovableWordSentence extends React.Component {
key={`assessmentChoice_${choice.id}_${this.props.language}`}
{...choice}
updateChoice={
(newChoice, fileIds) => this.props.updateChoice(id, choice.id, newChoice, fileIds)
(newChoice, fileIds) => this.onUpdateChoice(id, choice.id, newChoice, fileIds)
}
isActive={this.props.isActive && choice.id === this.props.activeChoice}
deleteChoice={() => this.props.deleteChoice(choice)}
Expand All @@ -58,7 +90,7 @@ class MovableWordSentence extends React.Component {
<Add
createChoice={() => this.props.createChoice()}
/>
<SaveOptions save={this.props.save} />
{saveOptions}
</div>
<div className="au-c-question__feedback">
<Feedback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('movable word sentece component', () => {

it('renders the movable word sentence component', () => {
expect(result.find('.au-c-movable__answers'));
expect(result.state().hasChanged).toEqual(false);
});

it('renders Option', () => {
Expand Down Expand Up @@ -109,4 +110,16 @@ describe('movable word sentece component', () => {
result.find('.au-c-movable__answers').simulate('blur', { target: { value: 'Preposition' } });
expect(calledFunc).toBeTruthy();
});

it('changes state when call onUpdateChoice', () => {
expect(result.state().hasChanged).toEqual(false);
result.instance().onUpdateChoice();
expect(result.state().hasChanged).toEqual(true);
});

it('sets disabled flag correctly on the Save button', () => {
expect(result.find({ disabled: true }).length).toEqual(1);
result.setState({ hasChanged: true });
expect(result.find({ disabled: true }).length).toEqual(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import localize from '../../../../locales/localize';

function saveOption(props) {
const strings = props.localizeStrings('saveOption');
let classes = 'au-c-btn au-c-btn--sm au-c-btn--maroon au-u-ml-md';

if (props.disabled) {
classes += ' is-inactive';
}
return (
<button
className="au-c-btn au-c-btn--sm au-c-btn--maroon au-u-ml-md"
className={classes}
onClick={props.save}
disabled={props.disabled ? props.disabled : false}
>
{strings.saveOptions}
</button>
Expand All @@ -16,6 +22,7 @@ function saveOption(props) {
saveOption.propTypes = {
save: React.PropTypes.func.isRequired,
localizeStrings: React.PropTypes.func.isRequired,
disabled: React.PropTypes.bool
};

export default localize(saveOption);
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ describe('save option button component', () => {
button.simulate('click');
expect(calledFunction).toBeTruthy();
});

it('has inactive class if disabled', () => {
expect(result.find('.is-inactive').length).toEqual(0);
props.disabled = true;
result = shallow(<SaveOptionButton {...props} />);
expect(result.find('.is-inactive').length).toEqual(1);
});
});