mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Portal: Refactored data-attributes file to use async / await
no issue Will assist with later changes, like adding Captcha. Since Captcha is an optional feature, it would complicate the promise chain considerably.
This commit is contained in:
parent
8dd5b0883a
commit
5b42cf2338
1 changed files with 13 additions and 12 deletions
|
@ -3,7 +3,7 @@ import {getCheckoutSessionDataFromPlanAttribute, getUrlHistory} from './utils/he
|
|||
import {HumanReadableError, chooseBestErrorMessage} from './utils/errors';
|
||||
import i18nLib from '@tryghost/i18n';
|
||||
|
||||
export function formSubmitHandler({event, form, errorEl, siteUrl, submitHandler},
|
||||
export async function formSubmitHandler({event, form, errorEl, siteUrl, submitHandler},
|
||||
t = (str) => {
|
||||
return str;
|
||||
}) {
|
||||
|
@ -60,12 +60,13 @@ export function formSubmitHandler({event, form, errorEl, siteUrl, submitHandler}
|
|||
}
|
||||
}
|
||||
|
||||
return fetch(`${siteUrl}/members/api/integrity-token/`, {
|
||||
method: 'GET'
|
||||
}).then((res) => {
|
||||
return res.text();
|
||||
}).then((integrityToken) => {
|
||||
return fetch(`${siteUrl}/members/api/send-magic-link/`, {
|
||||
try {
|
||||
const integrityTokenRes = await fetch(`${siteUrl}/members/api/integrity-token/`, {
|
||||
method: 'GET'
|
||||
});
|
||||
const integrityToken = await integrityTokenRes.text();
|
||||
|
||||
const magicLinkRes = await fetch(`${siteUrl}/members/api/send-magic-link/`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
|
@ -75,23 +76,23 @@ export function formSubmitHandler({event, form, errorEl, siteUrl, submitHandler}
|
|||
integrityToken
|
||||
})
|
||||
});
|
||||
}).then(function (res) {
|
||||
|
||||
form.addEventListener('submit', submitHandler);
|
||||
form.classList.remove('loading');
|
||||
if (res.ok) {
|
||||
if (magicLinkRes.ok) {
|
||||
form.classList.add('success');
|
||||
} else {
|
||||
return HumanReadableError.fromApiResponse(res).then((e) => {
|
||||
return HumanReadableError.fromApiResponse(magicLinkRes).then((e) => {
|
||||
throw e;
|
||||
});
|
||||
}
|
||||
}).catch((err) => {
|
||||
} catch (err) {
|
||||
if (errorEl) {
|
||||
// This theme supports a custom error element
|
||||
errorEl.innerText = chooseBestErrorMessage(err, t('There was an error sending the email, please try again'), t);
|
||||
}
|
||||
form.classList.add('error');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function planClickHandler({event, el, errorEl, siteUrl, site, member, clickHandler}) {
|
||||
|
|
Loading…
Add table
Reference in a new issue