Handled duplicate newsletter name errors correctly

refs https://github.com/TryGhost/Team/issues/1564

- Shows the error inline
- Depends on https://github.com/TryGhost/Ghost/pull/14694
This commit is contained in:
Simon Backx 2022-05-04 15:57:18 +02:00
parent ae1010ea03
commit bb4b3a7eec
2 changed files with 34 additions and 12 deletions

View File

@ -43,30 +43,41 @@ export default class EditNewsletterModal extends Component {
@task
*saveTask() {
const newsletter = this.args.data.newsletter;
try {
yield this.args.data.newsletter.validate({});
yield newsletter.validate({});
const newEmail = this.args.data.newsletter.senderEmail;
const newEmail = newsletter.senderEmail;
const result = yield this.args.data.newsletter.save();
const result = yield newsletter.save();
if (result._meta?.sent_email_verification) {
yield this.modals.open(ConfirmNewsletterEmailModal, {
newEmail,
currentEmail: this.args.data.newsletter.senderEmail
currentEmail: newsletter.senderEmail
});
}
this.args.data.afterSave?.(result);
return result;
} catch (e) {
if (e === undefined) {
} catch (error) {
if (error === undefined) {
// Validation error
return;
}
throw e;
// Do we have an error that we can show inline?
if (error.payload && error.payload.errors) {
for (const payloadError of error.payload.errors) {
if (payloadError.type === 'ValidationError' && payloadError.property && (payloadError.context || payloadError.message)) {
// Context has a better error message for validation errors
newsletter.errors.add(payloadError.property, payloadError.context || payloadError.message);
}
}
}
throw error;
}
}
}

View File

@ -39,23 +39,34 @@ export default class NewNewsletterModal extends Component {
@task
*saveTask() {
const newsletter = this.args.data.newsletter;
try {
yield this.args.data.newsletter.validate({});
yield newsletter.validate({});
const result = yield this.args.data.newsletter.save({
const result = yield newsletter.save({
adapterOptions: {optInExisting: this.optInExisting}
});
this.args.data.afterSave?.(result);
return result;
} catch (e) {
if (e === undefined) {
} catch (error) {
if (error === undefined) {
// Validation error
return;
}
throw e;
// Do we have an error that we can show inline?
if (error.payload && error.payload.errors) {
for (const payloadError of error.payload.errors) {
if (payloadError.type === 'ValidationError' && payloadError.property && (payloadError.context || payloadError.message)) {
// Context has a better error message for validation errors
newsletter.errors.add(payloadError.property, payloadError.context || payloadError.message);
}
}
}
throw error;
}
}
}