Warn the user if they haven't invited anyone

This commit is contained in:
Robin Ward
2016-09-20 12:28:22 -04:00
parent f803ff840d
commit 42f6e52dc6
11 changed files with 1014 additions and 18 deletions

View File

@@ -2,3 +2,4 @@
//= require select2.js
//= require jquery.ui.widget.js
//= require jquery.fileupload.js
//= require sweetalert.min.js

View File

@@ -26,7 +26,10 @@ export default Ember.Component.extend({
},
updateField() {
this.set('field.value', JSON.stringify(this.get('users')));
const users = this.get('users');
this.set('field.value', JSON.stringify(users));
this.set('field.warning', users.length ? null : 'invites.none_added');
},
actions: {

View File

@@ -11,6 +11,8 @@ jQuery.fn.wiggle = function (times, duration) {
return this;
};
const alreadyWarned = {};
export default Ember.Component.extend({
classNames: ['wizard-step'],
saving: null,
@@ -66,6 +68,14 @@ export default Ember.Component.extend({
Ember.run.scheduleOnce('afterRender', () => $('.invalid input[type=text]').wiggle(2, 100));
},
advance() {
this.set('saving', true);
this.get('step').save()
.then(response => this.sendAction('goNext', response))
.catch(() => this.animateInvalidFields())
.finally(() => this.set('saving', false));
},
actions: {
quit() {
document.location = "/";
@@ -80,14 +90,29 @@ export default Ember.Component.extend({
if (this.get('saving')) { return; }
const step = this.get('step');
step.checkFields();
const result = step.validate();
if (result.warnings.length) {
const unwarned = result.warnings.filter(w => !alreadyWarned[w]);
if (unwarned.length) {
unwarned.forEach(w => alreadyWarned[w] = true);
return window.swal({
customClass: 'wizard-warning',
title: "",
text: unwarned.map(w => I18n.t(`wizard.${w}`)).join("\n"),
type: 'warning',
showCancelButton: true,
confirmButtonColor: "#6699ff"
}, confirmed => {
if (confirmed) {
this.advance();
}
});
}
}
if (step.get('valid')) {
this.set('saving', true);
step.save()
.then(response => this.sendAction('goNext', response))
.catch(() => this.animateInvalidFields())
.finally(() => this.set('saving', false));
this.advance();
} else {
this.animateInvalidFields();
this.autoFocus();

View File

@@ -15,14 +15,21 @@ export default Ember.Object.extend(ValidState, {
return lookup;
},
checkFields() {
validate() {
let allValid = true;
const result = { warnings: [] };
this.get('fields').forEach(field => {
field.check();
allValid = allValid && field.get('valid');
allValid = allValid && field.check();
const warning = field.get('warning');
if (warning) {
result.warnings.push(warning);
}
});
this.setValid(allValid);
return result;
},
fieldError(id, description) {

View File

@@ -5,13 +5,19 @@ export default Ember.Object.extend(ValidState, {
type: null,
value: null,
required: null,
warning: null,
check() {
if (!this.get('required')) {
return this.setValid(true);
this.setValid(true);
return true;
}
const val = this.get('value');
this.setValid(val && val.length > 0);
const valid = val && val.length > 0;
this.setValid(valid);
return valid;
}
});

View File

@@ -16,6 +16,8 @@ componentTest('can add users', {
const firstVal = JSON.parse(this.get('field.value'));
assert.equal(firstVal.length, 0, 'empty JSON at first');
assert.ok(this.get('field.warning'), 'it has a warning since no users were added');
click('.add-user');
andThen(() => {
assert.ok(this.$('.users-list .invite-list-user').length === 0, "doesn't add a blank user");
@@ -33,6 +35,7 @@ componentTest('can add users', {
assert.equal(val.length, 1);
assert.equal(val[0].email, 'eviltrout@example.com', 'adds the email to the JSON');
assert.ok(val[0].role.length, 'adds the role to the JSON');
assert.ok(!this.get('field.warning'), 'no warning once the user is added');
});
fillIn('.invite-email', 'eviltrout@example.com');

View File

@@ -26,9 +26,9 @@ test('text - required - validation', assert => {
});
test('text - optional - validation', assert => {
const w = WizardField.create({ type: 'text' });
assert.ok(w.get('unchecked'));
const f = WizardField.create({ type: 'text' });
assert.ok(f.get('unchecked'));
w.check();
assert.ok(w.get('valid'));
f.check();
assert.ok(f.get('valid'));
});