From 30284909e9c1084a29a5ab88af3cfd221574bff8 Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Tue, 6 Sep 2022 14:39:18 +0200 Subject: [PATCH] Added members-autoredirect data attribute selector. (#261) closes https://github.com/TryGhost/Team/issues/1800 - Added data attribute selector for custom login / signup forms to optionally prevent redirects coming from the magic link. --- ghost/portal/src/data-attributes.js | 4 +++- ghost/portal/src/tests/data-attributes.test.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ghost/portal/src/data-attributes.js b/ghost/portal/src/data-attributes.js index 443af0de9c..0cf0b1ce27 100644 --- a/ghost/portal/src/data-attributes.js +++ b/ghost/portal/src/data-attributes.js @@ -11,6 +11,7 @@ function formSubmitHandler({event, form, errorEl, siteUrl, submitHandler}) { form.classList.remove('success', 'invalid', 'error'); let emailInput = event.target.querySelector('input[data-members-email]'); let nameInput = event.target.querySelector('input[data-members-name]'); + let autoRedirect = form?.dataset?.membersAutoredirect || 'true'; let email = emailInput?.value; let name = (nameInput && nameInput.value) || undefined; let emailType = undefined; @@ -36,7 +37,8 @@ function formSubmitHandler({event, form, errorEl, siteUrl, submitHandler}) { email: email, emailType: emailType, labels: labels, - name: name + name: name, + autoRedirect: (autoRedirect === 'true') }) }).then(function (res) { form.addEventListener('submit', submitHandler); diff --git a/ghost/portal/src/tests/data-attributes.test.js b/ghost/portal/src/tests/data-attributes.test.js index cbaf229a32..c71163a747 100644 --- a/ghost/portal/src/tests/data-attributes.test.js +++ b/ghost/portal/src/tests/data-attributes.test.js @@ -131,7 +131,7 @@ describe('Member Data attributes:', () => { expect(window.fetch).toHaveBeenCalledTimes(1); - expect(window.fetch).toHaveBeenCalledWith('https://portal.localhost/members/api/send-magic-link/', {body: '{"email":"jamie@example.com","emailType":"signup","labels":["Gold"],"name":"Jamie Larsen"}', headers: {'Content-Type': 'application/json'}, method: 'POST'}); + expect(window.fetch).toHaveBeenCalledWith('https://portal.localhost/members/api/send-magic-link/', {body: `{"email":"jamie@example.com","emailType":"signup","labels":["Gold"],"name":"Jamie Larsen","autoRedirect":${true}}`, headers: {'Content-Type': 'application/json'}, method: 'POST'}); }); });