mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 09:32:41 +00:00
refactor: keep opening hours content up to date
This commit is contained in:
@@ -18,11 +18,7 @@ import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
|
|||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import {Subscription} from 'rxjs';
|
import {Subscription} from 'rxjs';
|
||||||
import {logger} from '../_helpers/ts-logger';
|
import {logger} from '../_helpers/ts-logger';
|
||||||
// TODO: use import for opening_hours when the change is published with a new update
|
import opening_hours from 'opening_hours';
|
||||||
// see https://github.com/opening-hours/opening_hours.js/pull/407
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires, unicorn/prefer-module
|
|
||||||
const ohFunction = require('opening_hours');
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@Pipe({
|
@Pipe({
|
||||||
name: 'join',
|
name: 'join',
|
||||||
@@ -119,7 +115,7 @@ export class StringSplitPipe implements PipeTransform {
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
@Pipe({
|
@Pipe({
|
||||||
name: 'openingHours',
|
name: 'openingHours',
|
||||||
pure: true,
|
pure: false,
|
||||||
})
|
})
|
||||||
export class OpeningHoursPipe implements PipeTransform, OnDestroy {
|
export class OpeningHoursPipe implements PipeTransform, OnDestroy {
|
||||||
locale: string;
|
locale: string;
|
||||||
@@ -166,7 +162,7 @@ export class OpeningHoursPipe implements PipeTransform, OnDestroy {
|
|||||||
let openingHours;
|
let openingHours;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
openingHours = new ohFunction(aString, {
|
openingHours = new opening_hours(aString, {
|
||||||
address: {
|
address: {
|
||||||
country_code: 'de',
|
country_code: 'de',
|
||||||
state: 'Hessen',
|
state: 'Hessen',
|
||||||
@@ -184,7 +180,7 @@ export class OpeningHoursPipe implements PipeTransform, OnDestroy {
|
|||||||
const isOpen: boolean = openingHours.getState();
|
const isOpen: boolean = openingHours.getState();
|
||||||
const isUnknown: boolean = openingHours.getUnknown();
|
const isUnknown: boolean = openingHours.getUnknown();
|
||||||
|
|
||||||
const nextChange: Date = openingHours.getNextChange();
|
const nextChange = openingHours.getNextChange();
|
||||||
const nextChangeIsOpen: boolean = openingHours.getState(nextChange);
|
const nextChangeIsOpen: boolean = openingHours.getState(nextChange);
|
||||||
const nextChangeUnknown: boolean = openingHours.getUnknown(nextChange);
|
const nextChangeUnknown: boolean = openingHours.getUnknown(nextChange);
|
||||||
const nextChangeIsToday: boolean = moment().isSame(nextChange, 'day');
|
const nextChangeIsToday: boolean = moment().isSame(nextChange, 'day');
|
||||||
|
|||||||
@@ -13,13 +13,21 @@
|
|||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Component, ContentChild, Input, TemplateRef} from '@angular/core';
|
import {
|
||||||
|
Component,
|
||||||
|
ContentChild,
|
||||||
|
Input,
|
||||||
|
OnDestroy,
|
||||||
|
OnInit,
|
||||||
|
TemplateRef,
|
||||||
|
} from '@angular/core';
|
||||||
|
import opening_hours from 'opening_hours';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'stapps-opening-hours',
|
selector: 'stapps-opening-hours',
|
||||||
templateUrl: 'opening-hours.html',
|
templateUrl: 'opening-hours.html',
|
||||||
})
|
})
|
||||||
export class OpeningHoursComponent {
|
export class OpeningHoursComponent implements OnDestroy, OnInit {
|
||||||
@ContentChild(TemplateRef) content: TemplateRef<unknown>;
|
@ContentChild(TemplateRef) content: TemplateRef<unknown>;
|
||||||
|
|
||||||
@Input() openingHours?: string;
|
@Input() openingHours?: string;
|
||||||
@@ -27,4 +35,42 @@ export class OpeningHoursComponent {
|
|||||||
@Input() colorize = true;
|
@Input() colorize = true;
|
||||||
|
|
||||||
@Input() showNextChange = true;
|
@Input() showNextChange = true;
|
||||||
|
|
||||||
|
timer: NodeJS.Timeout;
|
||||||
|
|
||||||
|
updateTimer() {
|
||||||
|
if (typeof this.openingHours !== 'string') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
clearTimeout(this.timer);
|
||||||
|
|
||||||
|
const ohObject = new opening_hours(this.openingHours, {
|
||||||
|
address: {
|
||||||
|
country_code: 'de',
|
||||||
|
state: 'Hessen',
|
||||||
|
},
|
||||||
|
lon: 8.667_97,
|
||||||
|
lat: 50.129_16,
|
||||||
|
});
|
||||||
|
|
||||||
|
const millisecondsRemaining =
|
||||||
|
// eslint-disable-next-line unicorn/prefer-date-now
|
||||||
|
(ohObject.getNextChange()?.getTime() ?? 0) - new Date().getTime();
|
||||||
|
|
||||||
|
if (millisecondsRemaining > 0) {
|
||||||
|
this.timer = setTimeout(() => {
|
||||||
|
// pseudo update value to tigger openingHours pipe
|
||||||
|
this.openingHours = `${this.openingHours}`;
|
||||||
|
this.updateTimer();
|
||||||
|
}, millisecondsRemaining);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
this.updateTimer();
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnDestroy() {
|
||||||
|
clearTimeout(this.timer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user