Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-147634 - Add support for a custom Form block endpoint #2291

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
5b45f9c
add support for custom endpoint
elan-tbx May 10, 2024
eea752e
update tests
elan-tbx May 14, 2024
80515d9
MWPW-147599: CaaS Bulk Publisher not working after MS authentication …
sanrai May 14, 2024
2efcb33
Preflight Accessibility tab update (#2254)
rgclayton May 14, 2024
c755c30
MWPW-146407: add new card style to caas configurator (#2279)
sheridansunier May 14, 2024
cde37b5
MWPW-141780 - Clean up Accordion block attributes (#2276)
elan-tbx May 14, 2024
c4a1f4a
MWPW-147158: adds support for footer divider at the collection level …
cmiqueo May 14, 2024
48ede00
MWPW-147594: Consumers mappings for origin not being sent when using …
sanrai May 14, 2024
fe2bab3
Stage automation, update the body after merging another PR in (#2289)
mokimo May 14, 2024
47c10b4
Remove label requirements & merge workflows (#2288)
mokimo May 14, 2024
a926237
Quiz entry block (#2294)
colloyd May 14, 2024
e1e2ddc
update tests again
elan-tbx May 14, 2024
04a53a6
consolidate args to config obj
elan-tbx May 14, 2024
b7235b1
Merge branch 'stage' into ebartholomew/MWPW-147634-form-endpoints
elan-tbx May 14, 2024
ec902b3
Merge branch 'stage' into ebartholomew/MWPW-147634-form-endpoints
elan-tbx May 17, 2024
8ba9aca
Merge branch 'stage' into ebartholomew/MWPW-147634-form-endpoints
elan-tbx May 20, 2024
0eac17f
Merge branch 'stage' into ebartholomew/MWPW-147634-form-endpoints
elan-tbx May 21, 2024
3bc8267
Merge branch 'stage' into ebartholomew/MWPW-147634-form-endpoints
elan-tbx May 21, 2024
2241638
add empty default for createForm
elan-tbx May 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions libs/blocks/form/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function createButton({ type, label }, thankYou) {
button.addEventListener('click', async (event) => {
const form = button.closest('form');
if (form.checkValidity()) {
form.setAttribute('submitted', 'true');
event.preventDefault();
button.setAttribute('disabled', '');
const submission = await submitForm(form);
Expand Down Expand Up @@ -232,7 +233,7 @@ function lowercaseKeys(obj) {
}, {});
}

async function createForm(formURL, thankYou, formData) {
async function createForm({ formURL, thankYou, formData, endpoint } = {}) {
const { pathname } = new URL(formURL);
let json = formData;
/* c8 ignore next 4 */
Expand All @@ -243,7 +244,7 @@ async function createForm(formURL, thankYou, formData) {
json.data = json.data.map((obj) => lowercaseKeys(obj));
const form = createTag('form');
const rules = [];
const [action] = pathname.split('.json');
const [action] = endpoint ? [endpoint] : pathname.split('.json');
form.dataset.action = action;

const typeToElement = {
Expand Down Expand Up @@ -290,8 +291,10 @@ async function createForm(formURL, thankYou, formData) {
}

export default async function decorate(block, formData = null) {
const form = block.querySelector('a[href$=".json"]');
const [form, endpoint] = block.querySelectorAll(':scope > div:first-child a');
const thankYou = block.querySelector(':scope > div:last-of-type > div');
thankYou.remove();
if (form) form.replaceWith(await createForm(form.href, thankYou, formData));
const config = { formURL: form?.href, thankYou, formData, endpoint: endpoint?.href };
if (endpoint) endpoint.parentElement.remove();
if (form) form.replaceWith(await createForm(config));
}
9 changes: 2 additions & 7 deletions test/blocks/form/form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,9 @@ describe('Form Block', async () => {

it('submits successfully', async () => {
const form = await waitForElement('form');
const reqCheck = form.querySelector('.group-container.required input');
const submit = form.querySelector('.form-submit-wrapper button');
reqCheck.checked = true;
reqCheck.closest('.field-wrapper').dispatchEvent(new Event('input'));
submit.dispatchEvent(new Event('click'));
form.querySelector('.form-submit-wrapper button').dispatchEvent(new Event('click'));
await new Promise((resolve) => { setTimeout(() => resolve(), 150); });
const thanks = document.querySelector('.thank-you');
expect(thanks).to.exist;
expect(form.getAttribute('submitted')).to.equal('true');
});

it('clears successfully', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/blocks/form/mocks/body.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="form">
<div>
<div><a href="https://main--milo--adobecom.hlx.page/drafts/ebartholomew/forms/test-form.json">https://main--milo--adobecom.hlx.page/drafts/ebartholomew/forms/test-form.json</a></div>
<div><a href="https://main--milo--adobecom.hlx.page/drafts/ebartholomew/forms/test-form">https://main--milo--adobecom.hlx.page/drafts/ebartholomew/forms/test-form</a></div>
</div>
<div>
<div>Thank you</div>
Expand Down
2 changes: 1 addition & 1 deletion test/blocks/form/mocks/form-data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"total":23,"offset":0,"limit":23,"data":[{"Field":"legal","Label":"This is legal text","Placeholder":"","Type":"legal","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"title","Label":"This is a Heading","Placeholder":"","Type":"heading","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"firstName","Label":"First Name:","Placeholder":"First Name","Type":"","Required":"x","Options":"","Rules":"","Default":"","Extra":""},{"Field":"lastName","Label":"Last Name:","Placeholder":"Last Name","Type":"","Required":"x","Options":"","Rules":"","Default":"","Extra":""},{"Field":"email","Label":"Email:","Placeholder":"Email","Type":"email","Required":"x","Options":"","Rules":"","Default":"","Extra":""},{"Field":"favoriteNumber","Label":"What is your favorite number?","Placeholder":"","Type":"number","Required":"x","Options":"","Rules":"","Default":"","Extra":""},{"Field":"favoriteColor","Label":"What is your favorite color?","Placeholder":"Select One","Type":"select","Required":"x","Options":"Purple, Green, Red, Other","Rules":"","Default":"Red","Extra":""},{"Field":"coffeeTea","Label":"Do you prefer coffee or tea?","Placeholder":"","Type":"radio-group","Required":"","Options":"Coffee, Tea","Rules":"","Default":"Coffee","Extra":""},{"Field":"birthday","Label":"Please enter your birthday","Placeholder":"","Type":"date","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"agree","Label":"Do you agree?","Placeholder":"","Type":"checkbox","Required":"x","Options":"Yes","Rules":"","Default":"","Extra":""},{"Field":"genres","Label":"What genres of music do you like? (Check all that apply)","Placeholder":"","Type":"checkbox-group","Required":"","Options":"Hip-Hop, Rock, Alternative, Country, Dance, Pop","Rules":"","Default":"Hip-Hop, Alternative, Dance","Extra":""},{"Field":"tellUs","Label":"Tell us what you think!","Placeholder":"","Type":"text-area","Required":"x","Options":"","Rules":"","Default":"","Extra":""},{"Field":"hiddenFieldEq","Label":"This should be hidden if the number is equal to 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"=\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldNe","Label":"This should be hidden if the number is not equal to 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"!=\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldGt","Label":"This should be hidden if the number is greater than 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \">\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldGe","Label":"This should be hidden if the number is greater than or equal to 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \">=\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldLt","Label":"This should be hidden if the number is less than 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"<\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldLe","Label":"This should be hidden if the number is less than or equal to 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"<=\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldInc","Label":"This should be hidden if \"Pop\" is checked","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"genres\", \"operator\": \"inc\", \"value\": \"Pop\"}}","Default":"","Extra":""},{"Field":"hiddenFieldExc","Label":"This should be hidden if \"Pop\" is NOT checked","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"genres\", \"operator\": \"exc\", \"value\": \"Pop\"}}","Default":"","Extra":""},{"Field":"hiddenField","Label":"This should always be visible because the rule contains an invalid operator","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"foo\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"submit","Label":"Submit","Placeholder":"","Type":"submit","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"clear","Label":"Clear","Placeholder":"","Type":"clear","Required":"","Options":"","Rules":"","Default":"","Extra":""}],":type":"sheet"}
{"total":23,"offset":0,"limit":23,"data":[{"Field":"legal","Label":"This is legal text","Placeholder":"","Type":"legal","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"title","Label":"This is a Heading","Placeholder":"","Type":"heading","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"firstName","Label":"First Name:","Placeholder":"First Name","Type":"","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"lastName","Label":"Last Name:","Placeholder":"Last Name","Type":"","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"email","Label":"Email:","Placeholder":"Email","Type":"email","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"favoriteNumber","Label":"What is your favorite number?","Placeholder":"","Type":"number","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"favoriteColor","Label":"What is your favorite color?","Placeholder":"Select One","Type":"select","Required":"","Options":"Purple, Green, Red, Other","Rules":"","Default":"Red","Extra":""},{"Field":"coffeeTea","Label":"Do you prefer coffee or tea?","Placeholder":"","Type":"radio-group","Required":"","Options":"Coffee, Tea","Rules":"","Default":"Coffee","Extra":""},{"Field":"birthday","Label":"Please enter your birthday","Placeholder":"","Type":"date","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"agree","Label":"Do you agree?","Placeholder":"","Type":"checkbox","Required":"","Options":"Yes","Rules":"","Default":"","Extra":""},{"Field":"genres","Label":"What genres of music do you like? (Check all that apply)","Placeholder":"","Type":"checkbox-group","Required":"","Options":"Hip-Hop, Rock, Alternative, Country, Dance, Pop","Rules":"","Default":"Hip-Hop, Alternative, Dance","Extra":""},{"Field":"tellUs","Label":"Tell us what you think!","Placeholder":"","Type":"text-area","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"hiddenFieldEq","Label":"This should be hidden if the number is equal to 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"=\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldNe","Label":"This should be hidden if the number is not equal to 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"!=\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldGt","Label":"This should be hidden if the number is greater than 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \">\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldGe","Label":"This should be hidden if the number is greater than or equal to 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \">=\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldLt","Label":"This should be hidden if the number is less than 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"<\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldLe","Label":"This should be hidden if the number is less than or equal to 10","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"<=\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"hiddenFieldInc","Label":"This should be hidden if \"Pop\" is checked","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"genres\", \"operator\": \"inc\", \"value\": \"Pop\"}}","Default":"","Extra":""},{"Field":"hiddenFieldExc","Label":"This should be hidden if \"Pop\" is NOT checked","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"genres\", \"operator\": \"exc\", \"value\": \"Pop\"}}","Default":"","Extra":""},{"Field":"hiddenField","Label":"This should always be visible because the rule contains an invalid operator","Placeholder":"","Type":"","Required":"","Options":"","Rules":"{\"type\": \"hidden\", \"condition\": {\"key\": \"favoriteNumber\", \"operator\": \"foo\", \"value\": \"10\"}}","Default":"","Extra":""},{"Field":"submit","Label":"Submit","Placeholder":"","Type":"submit","Required":"","Options":"","Rules":"","Default":"","Extra":""},{"Field":"clear","Label":"Clear","Placeholder":"","Type":"clear","Required":"","Options":"","Rules":"","Default":"","Extra":""}],":type":"sheet"}
Loading