Skip to content

Commit

Permalink
Refactor SC+BackgroundSync
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 7, 2024
1 parent f33866b commit 8c88383
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 376 deletions.
21 changes: 0 additions & 21 deletions assets/dist/controller.d.ts

This file was deleted.

55 changes: 0 additions & 55 deletions assets/dist/controller.js

This file was deleted.

1 change: 0 additions & 1 deletion assets/jest.config.js

This file was deleted.

15 changes: 10 additions & 5 deletions assets/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
{
"name": "@pwa/connection-status",
"description": "PWA for Symfony",
"name": "@spomky-labs/pwa-bundle",
"description": "Stimulus Controllers for the PWA Bundle",
"license": "MIT",
"version": "1.0.0",
"main": "dist/controller.js",
"types": "dist/controller.d.ts",
"symfony": {
"controllers": {
"connection-status": {
"main": "dist/controller.js",
"main": "src/connection-status_controller.js",
"name": "pwa/connection-status",
"webpackMode": "eager",
"fetch": "eager",
"enabled": true
},
"backgroundsync-form": {
"main": "src/backgroundsync-form_controller.js",
"name": "pwa/backgroundsync-form",
"webpackMode": "eager",
"fetch": "eager",
"enabled": true
}
},
"importmap": {
Expand Down
43 changes: 43 additions & 0 deletions assets/src/backgroundsync-form_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

import { Controller } from '@hotwired/stimulus';

/* stimulusFetch: 'lazy' */
export default class extends Controller {
static values = {
params: { type: Array, default: {
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
redirect: 'follow',
referrerPolicy: 'no-referrer'
}},
headers: { type: Array, default: [] },
redirection: { type: String, default: null },
};

async onSubmit(event) {
event.preventDefault();
const form = event.currentTarget;
if (!form instanceof HTMLFormElement || !form.checkValidity()) {
return;
}
const url = form.action;
const encType = form.encType ?? 'application/x-www-form-urlencoded';
const method = form.method ?? 'POST';
const params = this.paramsValue;
params.headers = this.headersValue;
params.headers['Content-Type'] = encType;

try {
params.body = new FormData(form);
await fetch(url, params);
} catch (error) {
// No need to do anything here
} finally {
if (this.redirectionValue) {
window.location.href = this.redirectionValue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,38 @@

import { Controller } from '@hotwired/stimulus';

enum Status {
OFFLINE = 'OFFLINE',
ONLINE = 'ONLINE',
}
/* stimulusFetch: 'lazy' */
export default class extends Controller {
static targets = ['message', 'attribute'];
static values = {
onlineMessage: { type: String, default: 'You are online.' },
offlineMessage: { type: String, default: 'You are offline.' },
};

declare readonly onlineMessageValue: string;
declare readonly offlineMessageValue: string;
declare readonly attributeTargets: HTMLElement[];
declare readonly messageTargets: HTMLElement[];

connect() {
this.dispatchEvent('connect', {});
if (navigator.onLine) {
this.statusChanged({
status: Status.ONLINE,
status: 'ONLINE',
message: this.onlineMessageValue,
});
} else {
this.statusChanged({
status: Status.OFFLINE,
status: 'OFFLINE',
message: this.offlineMessageValue,
});
}

window.addEventListener('offline', () => {
window.addEventListener('online', () => {
this.statusChanged({
status: Status.OFFLINE,
message: this.offlineMessageValue,
status: 'ONLINE',
message: this.onlineMessageValue,
});
});
window.addEventListener('online', () => {
window.addEventListener('offline', () => {
this.statusChanged({
status: Status.ONLINE,
message: this.onlineMessageValue,
status: 'OFFLINE',
message: this.offlineMessageValue,
});
});
}
Expand Down
53 changes: 0 additions & 53 deletions assets/test/controller.test.ts

This file was deleted.

7 changes: 0 additions & 7 deletions babel.config.js

This file was deleted.

19 changes: 0 additions & 19 deletions jest.config.js

This file was deleted.

79 changes: 0 additions & 79 deletions package.json

This file was deleted.

Loading

0 comments on commit 8c88383

Please sign in to comment.