Skip to content

Commit

Permalink
FIX BackgroundSync SC
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 7, 2024
1 parent 71c7f9f commit 5db1b40
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
31 changes: 25 additions & 6 deletions assets/src/backgroundsync-form_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,44 @@ export default class extends Controller {
redirection: { type: String, default: null },
};

async onSubmit(event) {
async send(event) {
event.preventDefault();
const form = this.element;
if (!form instanceof HTMLFormElement || !form.checkValidity()) {
return;
}

const url = form.action;
const redirectTo = this.redirectionValue || url;
try {
const params = this.paramsValue;
params.headers = this.headersValue;
params.headers['Content-Type'] = form.encType ?? 'application/x-www-form-urlencoded';
params.body = new FormData(form);
if (form.enctype === 'multipart/form-data') {
params.body = new FormData(form);
} else if (form.enctype === 'application/json') {
params.body = JSON.stringify(Object.fromEntries(new FormData(form)));
} else if (form.enctype === 'application/x-www-form-urlencoded') {
params.headers['Content-Type'] = 'application/x-www-form-urlencoded';
params.body = new URLSearchParams(new FormData(form));
} else {
console.error('Unsupported form enctype');
}
params.method = form.method.toUpperCase();
await fetch(url, params);
const response = await fetch(url, params);
console.log(new URLSearchParams(params.body).toString(), params, params.headers);
if (response.redirected) {
window.location.assign(response.url);
return;
}
if (redirectTo) {
window.location.assign(this.redirectionValue || url);
}
} catch (error) {
// No need to do anything here
if (redirectTo) {
window.location.assign(this.redirectionValue || url);
}
} finally {
window.location.href = this.redirectionValue || url;
form.reset();
}
}
}
35 changes: 0 additions & 35 deletions tests/Functional/ServiceWorkerCommandTest.php

This file was deleted.

0 comments on commit 5db1b40

Please sign in to comment.