From 0c63574f8d23c5efcdee2d0bb8a0670a7d034182 Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Mon, 19 Dec 2022 20:41:52 +0330 Subject: [PATCH 01/14] fix(pwa/ff): `ok-text` in `job-add-form` --- uniquely/flight-finder-pwa/src/component/job-add-form.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uniquely/flight-finder-pwa/src/component/job-add-form.ts b/uniquely/flight-finder-pwa/src/component/job-add-form.ts index 5c006339b..0541349e7 100644 --- a/uniquely/flight-finder-pwa/src/component/job-add-form.ts +++ b/uniquely/flight-finder-pwa/src/component/job-add-form.ts @@ -99,7 +99,7 @@ export class JobAddForm extends AlwatrElement { ${l10n.localize('origin')} From 58c8da5389009ab44646d605dab7e01316f823a1 Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Mon, 19 Dec 2022 20:44:05 +0330 Subject: [PATCH 02/14] refactor(pwa/ff): `job-list` to `job-data` --- .../flight-finder-pwa/src/director/index.ts | 6 ++-- .../flight-finder-pwa/src/director/job-add.ts | 4 +-- .../src/director/{job-list.ts => job-data.ts} | 29 ++++++++++++------- .../src/director/job-delete.ts | 4 +-- 4 files changed, 26 insertions(+), 17 deletions(-) rename uniquely/flight-finder-pwa/src/director/{job-list.ts => job-data.ts} (56%) diff --git a/uniquely/flight-finder-pwa/src/director/index.ts b/uniquely/flight-finder-pwa/src/director/index.ts index 884183416..55a65b8dc 100644 --- a/uniquely/flight-finder-pwa/src/director/index.ts +++ b/uniquely/flight-finder-pwa/src/director/index.ts @@ -1,4 +1,4 @@ -import './job-add'; -import './job-delete'; -import './job-list.js'; +import './job-add.js'; +import './job-delete.js'; +import './job-data.js'; import './toast.js'; diff --git a/uniquely/flight-finder-pwa/src/director/job-add.ts b/uniquely/flight-finder-pwa/src/director/job-add.ts index 43c4eae20..4d3164dc0 100644 --- a/uniquely/flight-finder-pwa/src/director/job-add.ts +++ b/uniquely/flight-finder-pwa/src/director/job-add.ts @@ -1,7 +1,7 @@ import {fetch} from '@alwatr/fetch'; import {SignalInterface} from '@alwatr/signal'; -import {jobListSignal} from './job-list.js'; +import {jobDataSignal} from './job-data.js'; import {showToastSignal} from './toast.js'; import type {Job} from '../type.js'; @@ -34,5 +34,5 @@ jobAddSignal.addListener(async (job) => { }); } - jobListSignal.request(null); + jobDataSignal.request(null); }); diff --git a/uniquely/flight-finder-pwa/src/director/job-list.ts b/uniquely/flight-finder-pwa/src/director/job-data.ts similarity index 56% rename from uniquely/flight-finder-pwa/src/director/job-list.ts rename to uniquely/flight-finder-pwa/src/director/job-data.ts index a9dd2316a..a66b4f332 100644 --- a/uniquely/flight-finder-pwa/src/director/job-list.ts +++ b/uniquely/flight-finder-pwa/src/director/job-data.ts @@ -1,42 +1,51 @@ +import {createLogger} from '@alwatr/logger'; import {fetch} from '@alwatr/fetch'; import {SignalInterface} from '@alwatr/signal'; import {showToastSignal} from './toast.js'; -import type {Job} from '../type.js'; +import type {Job, jobMeta} from '../type.js'; import type {AlwatrServiceResponse} from '@alwatr/fetch'; -export const jobListSignal = new SignalInterface('job-list'); +export const logger = createLogger('[director/job-data]'); +export const jobDataSignal = new SignalInterface('job-data'); async function _dispatchJobList(response: Response): Promise { + logger.logMethodArgs('_dispatchJobList', {response}); + if (response.ok !== true) { throw new Error('fetch_failed'); } - const responseData = (await response.json()) as AlwatrServiceResponse>; + const responseData = (await response.json()) as AlwatrServiceResponse, jobMeta>; if (responseData.ok !== true) { throw new Error('fetch_failed'); } - jobListSignal.dispatch(Object.values(responseData.data)); + jobDataSignal.dispatch(responseData); } -jobListSignal.setProvider(async () => { +jobDataSignal.setProvider(async () => { try { + logger.logMethod('jobListProvider'); + const response = await fetch({ url: window.appConfig?.api ? window.appConfig.api + '/job' : '/job', token: window.appConfig?.token, cacheStrategy: 'stale_while_revalidate', revalidateCallback(response) { - _dispatchJobList(response); + logger.logMethodArgs('revalidateCallback', {response}); + + _dispatchJobList(response.clone()); }, cache: 'no-cache', }); await _dispatchJobList(response); - } - catch (error) { + } catch (error) { + logger.error('jobListProvider', 'fetch_failed', error); + showToastSignal.dispatch({ message: 'عملیات با خطا رو به رو شد', }); @@ -44,5 +53,5 @@ jobListSignal.setProvider(async () => { return; }); -jobListSignal.request(null); -setInterval(()=>jobListSignal.request(null), 60_000); +jobDataSignal.request(null); +setInterval(() => jobDataSignal.request(null), 60_000); diff --git a/uniquely/flight-finder-pwa/src/director/job-delete.ts b/uniquely/flight-finder-pwa/src/director/job-delete.ts index 6c98da671..45b1f3eec 100644 --- a/uniquely/flight-finder-pwa/src/director/job-delete.ts +++ b/uniquely/flight-finder-pwa/src/director/job-delete.ts @@ -1,7 +1,7 @@ import {fetch} from '@alwatr/fetch'; import {SignalInterface} from '@alwatr/signal'; -import {jobListSignal} from './job-list.js'; +import {jobDataSignal} from './job-data.js'; import {showToastSignal} from './toast.js'; import type {AlwatrServiceResponse} from '@alwatr/fetch'; @@ -33,5 +33,5 @@ jobDeleteSignal.addListener(async (id) => { }); } - jobListSignal.request(null); + jobDataSignal.request(null); }); From b2b2b7c0171191905b0e03f9d8d9f231c10c6a7d Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Mon, 19 Dec 2022 20:48:52 +0330 Subject: [PATCH 03/14] feat(pwa/ff): define response meta type --- uniquely/flight-finder-pwa/src/type.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/uniquely/flight-finder-pwa/src/type.ts b/uniquely/flight-finder-pwa/src/type.ts index 644a3cd91..166509458 100644 --- a/uniquely/flight-finder-pwa/src/type.ts +++ b/uniquely/flight-finder-pwa/src/type.ts @@ -1,4 +1,4 @@ -import type {AlwatrDocumentObject} from '@alwatr/fetch'; +import type {AlwatrDocumentObject, AlwatrServiceResponse} from '@alwatr/fetch'; import type {ToastOptions} from '@ionic/core'; declare global { @@ -8,14 +8,16 @@ declare global { interface AlwatrSignals { readonly 'job-add': Pick; readonly 'job-delete': string; - readonly 'job-list': Array; + readonly 'job-data': AlwatrServiceResponse, jobMeta> & {ok: true}; readonly toast: Partial & {message: string}; } interface AlwatrRequestSignals { - readonly 'job-list': null; + readonly 'job-data': null; } } +export type jobMeta = {formatVersion: 4; lastAutoId: 18; lastUpdated: 1671454823773; reversion: 5742}; + export type dayParts = 'earlyMorning' | 'morning' | 'midday' | 'afternoon' | 'evening' | 'night'; export interface Job extends AlwatrDocumentObject { @@ -42,8 +44,8 @@ export type JobResult = { price: number; time: string; seatCount: number; - airline: string, - airplane: string, - flightId: string, - arrivalTime: string, + airline: string; + airplane: string; + flightId: string; + arrivalTime: string; }; From 259723826e79f1183d0c05bebe3d25b9c9fcc60f Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Mon, 19 Dec 2022 20:51:26 +0330 Subject: [PATCH 04/14] feat(pwa/ff): last updated jobs --- .../src/component/page-flight-finder.ts | 20 ++++++++++++++----- .../flight-finder-pwa/src/util/human-time.ts | 18 +++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 uniquely/flight-finder-pwa/src/util/human-time.ts diff --git a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts index cb7ef21cf..5c27aae40 100644 --- a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts +++ b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts @@ -8,6 +8,7 @@ import {map} from 'lit/directives/map.js'; import ionNormalize from '../style/ionic.normalize.js'; import ionTheming from '../style/ionic.theming.js'; +import {getHumanTime} from '../util/human-time.js'; import './job-item.js'; import './job-add-form.js'; @@ -51,9 +52,11 @@ export class PageFlightFinder extends AlwatrElement { ]; private __jobList: Array = []; + private __relativeTime: string = ''; - static jobListSignal = new SignalInterface('job-list'); + static jobDataSignal = new SignalInterface('job-data'); static jobAddSignal = new SignalInterface('job-add'); + static relativeTimeFormatter = new Intl.RelativeTimeFormat('fa-IR', {style: 'narrow'}); override connectedCallback(): void { super.connectedCallback(); @@ -62,8 +65,16 @@ export class PageFlightFinder extends AlwatrElement { this.requestUpdate(); }); - PageFlightFinder.jobListSignal.addListener((jobList) => { - this.__jobList = jobList; + PageFlightFinder.jobDataSignal.addListener((jobList) => { + this.__jobList = Object.values(jobList.data); + + const now = new Date().getTime(); + const relativeTimeObject = getHumanTime(now - (jobList.meta?.lastUpdated ?? now)); + + this.__relativeTime = PageFlightFinder.relativeTimeFormatter.format( + -Math.floor(relativeTimeObject.humanTime), + relativeTimeObject.units + ); this.requestUpdate(); }); } @@ -94,7 +105,7 @@ export class PageFlightFinder extends AlwatrElement { ${l10n.localize('search_list')} - ۵ ${l10n.localize('seconds_ago')} + ${this.__relativeTime} @@ -113,7 +124,6 @@ export class PageFlightFinder extends AlwatrElement { modal.dismiss(); }); - await modal.present(); } } diff --git a/uniquely/flight-finder-pwa/src/util/human-time.ts b/uniquely/flight-finder-pwa/src/util/human-time.ts new file mode 100644 index 000000000..a33f1f581 --- /dev/null +++ b/uniquely/flight-finder-pwa/src/util/human-time.ts @@ -0,0 +1,18 @@ +export function getHumanTime(timestamp: number): { + humanTime: number; + units: 'seconds' | 'minutes' | 'hours'; +} { + const time = Math.abs(timestamp); + let humanTime = time / 1000; + let units: 'seconds' | 'minutes' | 'hours' = 'seconds'; + + if (time > 1000 * 60 * 60) { + humanTime = time / (1000 * 60 * 60); + units = 'hours'; + } else if (time > 1000 * 60) { + humanTime = time / (1000 * 60); + units = 'minutes'; + } + + return {humanTime, units}; +} From ee9118c3e3d010d10f92974779ba681faf361747 Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Mon, 19 Dec 2022 20:57:54 +0330 Subject: [PATCH 05/14] lint(pwa/ff): reported issues --- .../flight-finder-pwa/src/component/page-flight-finder.ts | 6 +++--- uniquely/flight-finder-pwa/src/director/job-data.ts | 5 +++-- uniquely/flight-finder-pwa/src/util/human-time.ts | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts index 5c27aae40..f839885fd 100644 --- a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts +++ b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts @@ -52,7 +52,7 @@ export class PageFlightFinder extends AlwatrElement { ]; private __jobList: Array = []; - private __relativeTime: string = ''; + private __relativeTime = ''; static jobDataSignal = new SignalInterface('job-data'); static jobAddSignal = new SignalInterface('job-add'); @@ -72,8 +72,8 @@ export class PageFlightFinder extends AlwatrElement { const relativeTimeObject = getHumanTime(now - (jobList.meta?.lastUpdated ?? now)); this.__relativeTime = PageFlightFinder.relativeTimeFormatter.format( - -Math.floor(relativeTimeObject.humanTime), - relativeTimeObject.units + -Math.floor(relativeTimeObject.humanTime), + relativeTimeObject.units, ); this.requestUpdate(); }); diff --git a/uniquely/flight-finder-pwa/src/director/job-data.ts b/uniquely/flight-finder-pwa/src/director/job-data.ts index a66b4f332..36b18978d 100644 --- a/uniquely/flight-finder-pwa/src/director/job-data.ts +++ b/uniquely/flight-finder-pwa/src/director/job-data.ts @@ -1,5 +1,5 @@ -import {createLogger} from '@alwatr/logger'; import {fetch} from '@alwatr/fetch'; +import {createLogger} from '@alwatr/logger'; import {SignalInterface} from '@alwatr/signal'; import {showToastSignal} from './toast.js'; @@ -43,7 +43,8 @@ jobDataSignal.setProvider(async () => { }); await _dispatchJobList(response); - } catch (error) { + } + catch (error) { logger.error('jobListProvider', 'fetch_failed', error); showToastSignal.dispatch({ diff --git a/uniquely/flight-finder-pwa/src/util/human-time.ts b/uniquely/flight-finder-pwa/src/util/human-time.ts index a33f1f581..24faf2fa0 100644 --- a/uniquely/flight-finder-pwa/src/util/human-time.ts +++ b/uniquely/flight-finder-pwa/src/util/human-time.ts @@ -9,7 +9,8 @@ export function getHumanTime(timestamp: number): { if (time > 1000 * 60 * 60) { humanTime = time / (1000 * 60 * 60); units = 'hours'; - } else if (time > 1000 * 60) { + } + else if (time > 1000 * 60) { humanTime = time / (1000 * 60); units = 'minutes'; } From 78fb9689af4e003268f1d8b62473f2ae6cf9e8f8 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Tue, 20 Dec 2022 21:25:28 +0330 Subject: [PATCH 06/14] fix(ff-pwa): safari issue --- uniquely/flight-finder-pwa/src/component/job-item.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/uniquely/flight-finder-pwa/src/component/job-item.ts b/uniquely/flight-finder-pwa/src/component/job-item.ts index 67fc3322e..3296b826d 100644 --- a/uniquely/flight-finder-pwa/src/component/job-item.ts +++ b/uniquely/flight-finder-pwa/src/component/job-item.ts @@ -79,6 +79,11 @@ export class JobItem extends AlwatrElement { .job .job__subtitle .job__subtitle-price { color: var(--ion-color-base); } + + ion-item-options { + -ms-flex-pack: start; + justify-content: flex-start; + } `, ]; From 26464e79bb860ab142e2863e38cbc7bb4ce8170d Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Tue, 20 Dec 2022 22:49:18 +0330 Subject: [PATCH 07/14] refactor(ff-pwa): director fetch storage --- .../src/director/job-data.ts | 62 +++++++------------ uniquely/flight-finder-pwa/src/type.ts | 6 +- 2 files changed, 24 insertions(+), 44 deletions(-) diff --git a/uniquely/flight-finder-pwa/src/director/job-data.ts b/uniquely/flight-finder-pwa/src/director/job-data.ts index 36b18978d..186efc17b 100644 --- a/uniquely/flight-finder-pwa/src/director/job-data.ts +++ b/uniquely/flight-finder-pwa/src/director/job-data.ts @@ -1,57 +1,39 @@ -import {fetch} from '@alwatr/fetch'; +import {serviceRequest} from '@alwatr/fetch'; +import {AlwatrDocumentStorage} from '@alwatr/fetch/src/type.js'; import {createLogger} from '@alwatr/logger'; import {SignalInterface} from '@alwatr/signal'; import {showToastSignal} from './toast.js'; -import type {Job, jobMeta} from '../type.js'; -import type {AlwatrServiceResponse} from '@alwatr/fetch'; +import type {Job} from '../type.js'; export const logger = createLogger('[director/job-data]'); export const jobDataSignal = new SignalInterface('job-data'); -async function _dispatchJobList(response: Response): Promise { - logger.logMethodArgs('_dispatchJobList', {response}); - - if (response.ok !== true) { - throw new Error('fetch_failed'); - } - - const responseData = (await response.json()) as AlwatrServiceResponse, jobMeta>; - - if (responseData.ok !== true) { - throw new Error('fetch_failed'); - } - - jobDataSignal.dispatch(responseData); -} - jobDataSignal.setProvider(async () => { - try { - logger.logMethod('jobListProvider'); - - const response = await fetch({ - url: window.appConfig?.api ? window.appConfig.api + '/job' : '/job', - token: window.appConfig?.token, - cacheStrategy: 'stale_while_revalidate', - revalidateCallback(response) { - logger.logMethodArgs('revalidateCallback', {response}); - - _dispatchJobList(response.clone()); - }, - cache: 'no-cache', - }); + logger.logMethod('jobListProvider'); + const firstTime = jobDataSignal.value == null; - await _dispatchJobList(response); + try { + jobDataSignal.dispatch( + > await serviceRequest({ + url: window.appConfig?.api ? window.appConfig.api + '/job' : '/job', + token: window.appConfig?.token, + cache: 'no-cache', + cacheStrategy: firstTime ? 'cache_only' : 'network_only', + }), + ); } catch (error) { - logger.error('jobListProvider', 'fetch_failed', error); - - showToastSignal.dispatch({ - message: 'عملیات با خطا رو به رو شد', - }); + if ((error as Error).message !== 'fetch_cache_not_found') { + logger.error('jobListProvider', 'fetch_failed', error); + showToastSignal.dispatch({ + message: 'عملیات با خطا رو به رو شد', + }); + } } - return; + + if (firstTime) jobDataSignal.request(null); }); jobDataSignal.request(null); diff --git a/uniquely/flight-finder-pwa/src/type.ts b/uniquely/flight-finder-pwa/src/type.ts index 166509458..7f686584f 100644 --- a/uniquely/flight-finder-pwa/src/type.ts +++ b/uniquely/flight-finder-pwa/src/type.ts @@ -1,4 +1,4 @@ -import type {AlwatrDocumentObject, AlwatrServiceResponse} from '@alwatr/fetch'; +import type {AlwatrDocumentObject, AlwatrDocumentStorage} from '@alwatr/fetch/type.js'; import type {ToastOptions} from '@ionic/core'; declare global { @@ -8,7 +8,7 @@ declare global { interface AlwatrSignals { readonly 'job-add': Pick; readonly 'job-delete': string; - readonly 'job-data': AlwatrServiceResponse, jobMeta> & {ok: true}; + readonly 'job-data': AlwatrDocumentStorage; readonly toast: Partial & {message: string}; } interface AlwatrRequestSignals { @@ -16,8 +16,6 @@ declare global { } } -export type jobMeta = {formatVersion: 4; lastAutoId: 18; lastUpdated: 1671454823773; reversion: 5742}; - export type dayParts = 'earlyMorning' | 'morning' | 'midday' | 'afternoon' | 'evening' | 'night'; export interface Job extends AlwatrDocumentObject { From 35cbb0c9429561028b77dcf578d67e9196fd77b3 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Tue, 20 Dec 2022 23:06:30 +0330 Subject: [PATCH 08/14] fix(ff-pwa): requestJobStorage --- .../flight-finder-pwa/src/director/job-data.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/uniquely/flight-finder-pwa/src/director/job-data.ts b/uniquely/flight-finder-pwa/src/director/job-data.ts index 186efc17b..5f1cc4b1b 100644 --- a/uniquely/flight-finder-pwa/src/director/job-data.ts +++ b/uniquely/flight-finder-pwa/src/director/job-data.ts @@ -1,5 +1,5 @@ import {serviceRequest} from '@alwatr/fetch'; -import {AlwatrDocumentStorage} from '@alwatr/fetch/src/type.js'; +import {AlwatrDocumentStorage, CacheStrategy} from '@alwatr/fetch/src/type.js'; import {createLogger} from '@alwatr/logger'; import {SignalInterface} from '@alwatr/signal'; @@ -10,9 +10,8 @@ import type {Job} from '../type.js'; export const logger = createLogger('[director/job-data]'); export const jobDataSignal = new SignalInterface('job-data'); -jobDataSignal.setProvider(async () => { +async function requestJobStorage(cacheStrategy: CacheStrategy): Promise { logger.logMethod('jobListProvider'); - const firstTime = jobDataSignal.value == null; try { jobDataSignal.dispatch( @@ -20,7 +19,7 @@ jobDataSignal.setProvider(async () => { url: window.appConfig?.api ? window.appConfig.api + '/job' : '/job', token: window.appConfig?.token, cache: 'no-cache', - cacheStrategy: firstTime ? 'cache_only' : 'network_only', + cacheStrategy, }), ); } @@ -32,9 +31,10 @@ jobDataSignal.setProvider(async () => { }); } } +} - if (firstTime) jobDataSignal.request(null); -}); +jobDataSignal.setProvider(() => requestJobStorage('network_first')); + +requestJobStorage('cache_only').then(() => requestJobStorage('network_first')); -jobDataSignal.request(null); setInterval(() => jobDataSignal.request(null), 60_000); From d4ff63cf966195e1a01c4161d248ae90c8a88531 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Tue, 20 Dec 2022 23:32:24 +0330 Subject: [PATCH 09/14] refactor(ff-pwa): updateTimer --- .../src/component/page-flight-finder.ts | 33 +++++++++++-------- .../flight-finder-pwa/src/util/human-time.ts | 19 ----------- 2 files changed, 20 insertions(+), 32 deletions(-) delete mode 100644 uniquely/flight-finder-pwa/src/util/human-time.ts diff --git a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts index f839885fd..7ba84ff4f 100644 --- a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts +++ b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts @@ -3,12 +3,11 @@ import {l10n} from '@alwatr/i18n'; import {SignalInterface} from '@alwatr/signal'; import {modalController} from '@ionic/core'; import {css, html} from 'lit'; -import {customElement} from 'lit/decorators.js'; +import {customElement, query} from 'lit/decorators.js'; import {map} from 'lit/directives/map.js'; import ionNormalize from '../style/ionic.normalize.js'; import ionTheming from '../style/ionic.theming.js'; -import {getHumanTime} from '../util/human-time.js'; import './job-item.js'; import './job-add-form.js'; @@ -51,13 +50,26 @@ export class PageFlightFinder extends AlwatrElement { `, ]; - private __jobList: Array = []; - private __relativeTime = ''; static jobDataSignal = new SignalInterface('job-data'); static jobAddSignal = new SignalInterface('job-add'); static relativeTimeFormatter = new Intl.RelativeTimeFormat('fa-IR', {style: 'narrow'}); + private __jobList?: Array; + private __lastUpdate = 0; + @query('#timer') protected _timer?: HTMLSpanElement; + + constructor() { + super(); + this.__updateTimer = this.__updateTimer.bind(this); + } + + private __updateTimer(): void { + const timer = this._timer; + if (timer == null || this.__lastUpdate === 0) return; + timer.innerText = l10n.formatNumber(Math.round((Date.now() - this.__lastUpdate) / 1000)); + } + override connectedCallback(): void { super.connectedCallback(); @@ -67,16 +79,11 @@ export class PageFlightFinder extends AlwatrElement { PageFlightFinder.jobDataSignal.addListener((jobList) => { this.__jobList = Object.values(jobList.data); - - const now = new Date().getTime(); - const relativeTimeObject = getHumanTime(now - (jobList.meta?.lastUpdated ?? now)); - - this.__relativeTime = PageFlightFinder.relativeTimeFormatter.format( - -Math.floor(relativeTimeObject.humanTime), - relativeTimeObject.units, - ); + this.__lastUpdate = Date.now(); this.requestUpdate(); }); + + setInterval(this.__updateTimer, 1000); } override render(): TemplateResult { return html` @@ -105,7 +112,7 @@ export class PageFlightFinder extends AlwatrElement { ${l10n.localize('search_list')} - ${this.__relativeTime} + - ${l10n.localize('seconds_ago')} diff --git a/uniquely/flight-finder-pwa/src/util/human-time.ts b/uniquely/flight-finder-pwa/src/util/human-time.ts deleted file mode 100644 index 24faf2fa0..000000000 --- a/uniquely/flight-finder-pwa/src/util/human-time.ts +++ /dev/null @@ -1,19 +0,0 @@ -export function getHumanTime(timestamp: number): { - humanTime: number; - units: 'seconds' | 'minutes' | 'hours'; -} { - const time = Math.abs(timestamp); - let humanTime = time / 1000; - let units: 'seconds' | 'minutes' | 'hours' = 'seconds'; - - if (time > 1000 * 60 * 60) { - humanTime = time / (1000 * 60 * 60); - units = 'hours'; - } - else if (time > 1000 * 60) { - humanTime = time / (1000 * 60); - units = 'minutes'; - } - - return {humanTime, units}; -} From 712e2869d3dcc58fda48346712713e008be422dd Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Tue, 20 Dec 2022 23:44:36 +0330 Subject: [PATCH 10/14] chore(ff-pwa): update version --- uniquely/flight-finder-pwa/src/component/page-flight-finder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts index 7ba84ff4f..27066585f 100644 --- a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts +++ b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts @@ -102,7 +102,7 @@ export class PageFlightFinder extends AlwatrElement { -
v${Alwatr.version}-prv3
+
v${Alwatr.version}-prv4
`; } From f31b707df599a390203579d8f77156fb6f2f3b19 Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Wed, 21 Dec 2022 13:37:02 +0330 Subject: [PATCH 11/14] fix(pwa/ff): timer --- uniquely/flight-finder-pwa/l10n/fa-IR.json | 2 +- .../src/component/page-flight-finder.ts | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/uniquely/flight-finder-pwa/l10n/fa-IR.json b/uniquely/flight-finder-pwa/l10n/fa-IR.json index a1f728707..6130bb2d3 100644 --- a/uniquely/flight-finder-pwa/l10n/fa-IR.json +++ b/uniquely/flight-finder-pwa/l10n/fa-IR.json @@ -5,7 +5,7 @@ "flight_finder": "پرواز یاب", "search_list": "لیست جستجو", - "seconds_ago": "ثانیه پیش", + "minutes_ago": "دقیقه پیش", "origin": "مبدا", "destination": "مقصد", "confirm": "تایید", diff --git a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts index 27066585f..eaa514294 100644 --- a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts +++ b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts @@ -42,6 +42,11 @@ export class PageFlightFinder extends AlwatrElement { justify-content: center; } + span#timer{ + display:inline-block; + min-width:23px; + } + .version { direction: ltr; margin: 0 16px 8px; @@ -53,7 +58,6 @@ export class PageFlightFinder extends AlwatrElement { static jobDataSignal = new SignalInterface('job-data'); static jobAddSignal = new SignalInterface('job-add'); - static relativeTimeFormatter = new Intl.RelativeTimeFormat('fa-IR', {style: 'narrow'}); private __jobList?: Array; private __lastUpdate = 0; @@ -67,7 +71,10 @@ export class PageFlightFinder extends AlwatrElement { private __updateTimer(): void { const timer = this._timer; if (timer == null || this.__lastUpdate === 0) return; - timer.innerText = l10n.formatNumber(Math.round((Date.now() - this.__lastUpdate) / 1000)); + + const time = Math.floor((Date.now() - this.__lastUpdate) / 6_000) / 10; + + timer.innerText = l10n.formatNumber(time); } override connectedCallback(): void { @@ -79,11 +86,11 @@ export class PageFlightFinder extends AlwatrElement { PageFlightFinder.jobDataSignal.addListener((jobList) => { this.__jobList = Object.values(jobList.data); - this.__lastUpdate = Date.now(); + this.__lastUpdate = jobList.meta.lastUpdated; this.requestUpdate(); }); - setInterval(this.__updateTimer, 1000); + setInterval(this.__updateTimer, 3_000); } override render(): TemplateResult { return html` @@ -112,7 +119,7 @@ export class PageFlightFinder extends AlwatrElement { ${l10n.localize('search_list')} - - ${l10n.localize('seconds_ago')} + - ${l10n.localize('minutes_ago')} From a5e5cfda9a72268491352bf412d67f9053cb322d Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Wed, 21 Dec 2022 13:42:21 +0330 Subject: [PATCH 12/14] refactor(pwa/ff): `job-data` to `job-document-storage` --- .../flight-finder-pwa/src/component/page-flight-finder.ts | 4 ++-- uniquely/flight-finder-pwa/src/director/index.ts | 2 +- uniquely/flight-finder-pwa/src/director/job-add.ts | 2 +- uniquely/flight-finder-pwa/src/director/job-delete.ts | 2 +- .../src/director/{job-data.ts => job-document-storage.ts} | 4 ++-- uniquely/flight-finder-pwa/src/type.ts | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) rename uniquely/flight-finder-pwa/src/director/{job-data.ts => job-document-storage.ts} (89%) diff --git a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts index eaa514294..88291dd13 100644 --- a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts +++ b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts @@ -56,7 +56,7 @@ export class PageFlightFinder extends AlwatrElement { ]; - static jobDataSignal = new SignalInterface('job-data'); + static jobDocumentStorageSignal = new SignalInterface('job-document-storage'); static jobAddSignal = new SignalInterface('job-add'); private __jobList?: Array; @@ -84,7 +84,7 @@ export class PageFlightFinder extends AlwatrElement { this.requestUpdate(); }); - PageFlightFinder.jobDataSignal.addListener((jobList) => { + PageFlightFinder.jobDocumentStorageSignal.addListener((jobList) => { this.__jobList = Object.values(jobList.data); this.__lastUpdate = jobList.meta.lastUpdated; this.requestUpdate(); diff --git a/uniquely/flight-finder-pwa/src/director/index.ts b/uniquely/flight-finder-pwa/src/director/index.ts index 55a65b8dc..aab96cb94 100644 --- a/uniquely/flight-finder-pwa/src/director/index.ts +++ b/uniquely/flight-finder-pwa/src/director/index.ts @@ -1,4 +1,4 @@ import './job-add.js'; import './job-delete.js'; -import './job-data.js'; +import './job-document-storage.js'; import './toast.js'; diff --git a/uniquely/flight-finder-pwa/src/director/job-add.ts b/uniquely/flight-finder-pwa/src/director/job-add.ts index 4d3164dc0..922d7caab 100644 --- a/uniquely/flight-finder-pwa/src/director/job-add.ts +++ b/uniquely/flight-finder-pwa/src/director/job-add.ts @@ -1,7 +1,7 @@ import {fetch} from '@alwatr/fetch'; import {SignalInterface} from '@alwatr/signal'; -import {jobDataSignal} from './job-data.js'; +import {jobDataSignal} from './job-document-storage.js'; import {showToastSignal} from './toast.js'; import type {Job} from '../type.js'; diff --git a/uniquely/flight-finder-pwa/src/director/job-delete.ts b/uniquely/flight-finder-pwa/src/director/job-delete.ts index 45b1f3eec..abff3dbdc 100644 --- a/uniquely/flight-finder-pwa/src/director/job-delete.ts +++ b/uniquely/flight-finder-pwa/src/director/job-delete.ts @@ -1,7 +1,7 @@ import {fetch} from '@alwatr/fetch'; import {SignalInterface} from '@alwatr/signal'; -import {jobDataSignal} from './job-data.js'; +import {jobDataSignal} from './job-document-storage.js'; import {showToastSignal} from './toast.js'; import type {AlwatrServiceResponse} from '@alwatr/fetch'; diff --git a/uniquely/flight-finder-pwa/src/director/job-data.ts b/uniquely/flight-finder-pwa/src/director/job-document-storage.ts similarity index 89% rename from uniquely/flight-finder-pwa/src/director/job-data.ts rename to uniquely/flight-finder-pwa/src/director/job-document-storage.ts index 5f1cc4b1b..412bcfc3f 100644 --- a/uniquely/flight-finder-pwa/src/director/job-data.ts +++ b/uniquely/flight-finder-pwa/src/director/job-document-storage.ts @@ -7,8 +7,8 @@ import {showToastSignal} from './toast.js'; import type {Job} from '../type.js'; -export const logger = createLogger('[director/job-data]'); -export const jobDataSignal = new SignalInterface('job-data'); +export const logger = createLogger('[director/job-document-storage]'); +export const jobDataSignal = new SignalInterface('job-document-storage'); async function requestJobStorage(cacheStrategy: CacheStrategy): Promise { logger.logMethod('jobListProvider'); diff --git a/uniquely/flight-finder-pwa/src/type.ts b/uniquely/flight-finder-pwa/src/type.ts index 7f686584f..2d2feea29 100644 --- a/uniquely/flight-finder-pwa/src/type.ts +++ b/uniquely/flight-finder-pwa/src/type.ts @@ -8,11 +8,11 @@ declare global { interface AlwatrSignals { readonly 'job-add': Pick; readonly 'job-delete': string; - readonly 'job-data': AlwatrDocumentStorage; + readonly 'job-document-storage': AlwatrDocumentStorage; readonly toast: Partial & {message: string}; } interface AlwatrRequestSignals { - readonly 'job-data': null; + readonly 'job-document-storage': null; } } From ffedfb357589f4fd3f363ba4acdcf1cee2253e50 Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Wed, 21 Dec 2022 13:44:17 +0330 Subject: [PATCH 13/14] refactor(pwa/ff): jobDocumentStorageSignal --- uniquely/flight-finder-pwa/src/director/job-add.ts | 4 ++-- uniquely/flight-finder-pwa/src/director/job-delete.ts | 4 ++-- .../src/director/job-document-storage.ts | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/uniquely/flight-finder-pwa/src/director/job-add.ts b/uniquely/flight-finder-pwa/src/director/job-add.ts index 922d7caab..bb3904c77 100644 --- a/uniquely/flight-finder-pwa/src/director/job-add.ts +++ b/uniquely/flight-finder-pwa/src/director/job-add.ts @@ -1,7 +1,7 @@ import {fetch} from '@alwatr/fetch'; import {SignalInterface} from '@alwatr/signal'; -import {jobDataSignal} from './job-document-storage.js'; +import {jobDocumentStorageSignal} from './job-document-storage.js'; import {showToastSignal} from './toast.js'; import type {Job} from '../type.js'; @@ -34,5 +34,5 @@ jobAddSignal.addListener(async (job) => { }); } - jobDataSignal.request(null); + jobDocumentStorageSignal.request(null); }); diff --git a/uniquely/flight-finder-pwa/src/director/job-delete.ts b/uniquely/flight-finder-pwa/src/director/job-delete.ts index abff3dbdc..89ce99f2e 100644 --- a/uniquely/flight-finder-pwa/src/director/job-delete.ts +++ b/uniquely/flight-finder-pwa/src/director/job-delete.ts @@ -1,7 +1,7 @@ import {fetch} from '@alwatr/fetch'; import {SignalInterface} from '@alwatr/signal'; -import {jobDataSignal} from './job-document-storage.js'; +import {jobDocumentStorageSignal} from './job-document-storage.js'; import {showToastSignal} from './toast.js'; import type {AlwatrServiceResponse} from '@alwatr/fetch'; @@ -33,5 +33,5 @@ jobDeleteSignal.addListener(async (id) => { }); } - jobDataSignal.request(null); + jobDocumentStorageSignal.request(null); }); diff --git a/uniquely/flight-finder-pwa/src/director/job-document-storage.ts b/uniquely/flight-finder-pwa/src/director/job-document-storage.ts index 412bcfc3f..ccae37e4d 100644 --- a/uniquely/flight-finder-pwa/src/director/job-document-storage.ts +++ b/uniquely/flight-finder-pwa/src/director/job-document-storage.ts @@ -8,13 +8,13 @@ import {showToastSignal} from './toast.js'; import type {Job} from '../type.js'; export const logger = createLogger('[director/job-document-storage]'); -export const jobDataSignal = new SignalInterface('job-document-storage'); +export const jobDocumentStorageSignal = new SignalInterface('job-document-storage'); async function requestJobStorage(cacheStrategy: CacheStrategy): Promise { logger.logMethod('jobListProvider'); try { - jobDataSignal.dispatch( + jobDocumentStorageSignal.dispatch( > await serviceRequest({ url: window.appConfig?.api ? window.appConfig.api + '/job' : '/job', token: window.appConfig?.token, @@ -33,8 +33,8 @@ async function requestJobStorage(cacheStrategy: CacheStrategy): Promise { } } -jobDataSignal.setProvider(() => requestJobStorage('network_first')); +jobDocumentStorageSignal.setProvider(() => requestJobStorage('network_first')); requestJobStorage('cache_only').then(() => requestJobStorage('network_first')); -setInterval(() => jobDataSignal.request(null), 60_000); +setInterval(() => jobDocumentStorageSignal.request(null), 60_000); From 201f3c48ecbe6a657bd3c7ac0d1a42e276b7d69d Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Wed, 21 Dec 2022 14:00:15 +0330 Subject: [PATCH 14/14] fix(pwa/ff): timer selector --- .../flight-finder-pwa/src/component/page-flight-finder.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts index 88291dd13..dfa7ed044 100644 --- a/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts +++ b/uniquely/flight-finder-pwa/src/component/page-flight-finder.ts @@ -42,9 +42,9 @@ export class PageFlightFinder extends AlwatrElement { justify-content: center; } - span#timer{ - display:inline-block; - min-width:23px; + #timer { + display: inline-block; + min-width: 23px; } .version { @@ -55,7 +55,6 @@ export class PageFlightFinder extends AlwatrElement { `, ]; - static jobDocumentStorageSignal = new SignalInterface('job-document-storage'); static jobAddSignal = new SignalInterface('job-add');