From 5db1b402a34c54fc3213bb2b53f43b275ec1121c Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Thu, 7 Mar 2024 09:12:41 +0100 Subject: [PATCH] FIX BackgroundSync SC --- assets/src/backgroundsync-form_controller.js | 31 ++++++++++++---- tests/Functional/ServiceWorkerCommandTest.php | 35 ------------------- 2 files changed, 25 insertions(+), 41 deletions(-) delete mode 100644 tests/Functional/ServiceWorkerCommandTest.php diff --git a/assets/src/backgroundsync-form_controller.js b/assets/src/backgroundsync-form_controller.js index a0dc7c7..25ec11a 100644 --- a/assets/src/backgroundsync-form_controller.js +++ b/assets/src/backgroundsync-form_controller.js @@ -16,7 +16,7 @@ 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()) { @@ -24,17 +24,36 @@ export default class extends Controller { } 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(); } } } diff --git a/tests/Functional/ServiceWorkerCommandTest.php b/tests/Functional/ServiceWorkerCommandTest.php deleted file mode 100644 index a090c93..0000000 --- a/tests/Functional/ServiceWorkerCommandTest.php +++ /dev/null @@ -1,35 +0,0 @@ -find('pwa:create:sw'); - $commandTester = new CommandTester($command); - $output = sprintf('%s/samples/my-sw.js', self::$kernel->getCacheDir()); - - // When - $commandTester->execute([ - 'output' => $output, - '--force' => true, - ]); - - // Then - $commandTester->assertCommandIsSuccessful(); - - static::assertStringContainsString('PWA Service Worker Generator', $commandTester->getDisplay()); - static::assertFileExists($output); - } -}