Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Added support for external urls for redirect (#1778)
Browse files Browse the repository at this point in the history
refs TryGhost/Ghost#12366

Adds support for saving external URL's for custom redirects and improves error handling to validate on blur/before saving

Co-authored-by: Fabien O'Carroll <fabien@allou.is>
  • Loading branch information
rishabhgrg and allouis committed Nov 23, 2020
1 parent 27c7331 commit 36999ce
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/components/gh-url-input.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
@value={{this.value}}
@type="text"
@input={{setValue}}
@focus-out={{validateUrlInput}}
/>

10 changes: 7 additions & 3 deletions app/components/gh-url-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ export default class GhUrlInput extends Component {
this.baseUrl = ensureEndsWith(args.baseUrl, '/');
this.value = args.value && args.value !== '/' ? (new URL(removeLeadingSlash(args.value), this.baseUrl)).href : '';
this.setResult = args.setResult;
this.validateUrl = args.validateUrl;
}

@action
setValue(event) {
this.value = event.target.value;
if (this.result !== null) {
this.setResult(this.result);
}
this.setResult(this.result);
}

@action
validateUrlInput() {
this.validateUrl(this.result);
}

get result() {
Expand Down
2 changes: 2 additions & 0 deletions app/components/modal-portal-settings.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
@value={{readonly this.settings.membersFreeSignupRedirect}}
@baseUrl={{readonly this.siteUrl}}
@setResult={{action "setFreeSignupRedirect"}}
@validateUrl={{action "validateFreeSignupRedirect"}}
/>
</div>
<GhErrorMessage
Expand All @@ -286,6 +287,7 @@
@value={{readonly this.settings.membersPaidSignupRedirect}}
@baseUrl={{readonly this.siteUrl}}
@setResult={{action "setPaidSignupRedirect"}}
@validateUrl={{action "validatePaidSignupRedirect"}}
/>
</div>
<GhErrorMessage
Expand Down
40 changes: 33 additions & 7 deletions app/components/modal-portal-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default ModalComponent.extend({
customIcon: null,
showLinksPage: false,
showLeaveSettingsModal: false,
freeSignupRedirect: undefined,
paidSignupRedirect: undefined,
confirm() {},

allowSelfSignup: alias('settings.membersAllowFreeSignup'),
Expand Down Expand Up @@ -148,11 +150,13 @@ export default ModalComponent.extend({
if (portalButtonIcon && !defaultIconKeys.includes(portalButtonIcon)) {
this.set('customIcon', this.settings.get('portalButtonIcon'));
}

this.siteUrl = this.config.get('blogUrl');
},

didInsertElement() {
this._super(...arguments);
this.get('settings.errors').clear();
run.later(this, function () {
this.set('hidePreviewFrame', false);
}, 1200);
Expand All @@ -177,11 +181,11 @@ export default ModalComponent.extend({
},

setPaidSignupRedirect(url) {
this._validateSignupRedirect(url, 'membersPaidSignupRedirect');
this.set('paidSignupRedirect', url);
},

setFreeSignupRedirect(url) {
this._validateSignupRedirect(url, 'membersFreeSignupRedirect');
this.set('freeSignupRedirect', url);
},

confirm() {
Expand Down Expand Up @@ -271,6 +275,14 @@ export default ModalComponent.extend({

leaveSettings() {
this.closeModal();
},

validateFreeSignupRedirect() {
return this._validateSignupRedirect(this.get('freeSignupRedirect'), 'membersFreeSignupRedirect');
},

validatePaidSignupRedirect() {
return this._validateSignupRedirect(this.get('paidSignupRedirect'), 'membersPaidSignupRedirect');
}
},

Expand All @@ -286,18 +298,27 @@ export default ModalComponent.extend({
},

_validateSignupRedirect(url, type) {
let errMessage = `Only URLs under your Ghost domain are allowed`;
let errMessage = `Please enter a valid URL`;
this.get('settings.errors').remove(type);
this.get('settings.hasValidated').removeObject(type);

if (url === null) {
this.get('settings.errors').add(type, errMessage);
this.get('settings.hasValidated').pushObject(type);
return false;
}

if (url === undefined) {
// Not initialised
return;
}

if (url.href.startsWith(this.siteUrl)) {
const path = url.href.replace(this.siteUrl, '');
this.settings.set(type, path);
return;
} else {
this.settings.set(type, url.href);
}

this.get('settings.errors').add(type, errMessage);
this.get('settings.hasValidated').pushObject(type);
},

_validateAccentColor(color) {
Expand Down Expand Up @@ -347,6 +368,11 @@ export default ModalComponent.extend({
}),

saveTask: task(function* () {
this.send('validateFreeSignupRedirect');
this.send('validatePaidSignupRedirect');
if (this.get('settings.errors').length !== 0) {
return;
}
yield this.settings.save();
this.closeModal();
}).drop()
Expand Down

0 comments on commit 36999ce

Please sign in to comment.