Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: update summary each minute
  • Loading branch information
Angeluz-07 committed Jun 29, 2020
commit 6eeb799f59cd10c4fbf87a7c2439d89ed94ee2b2
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { filter, takeUntil } from 'rxjs/operators';
import { SubstractDatePipe } from './../../../shared/pipes/substract-date/substract-date.pipe';
import { interval, Subscription, Subject } from 'rxjs';
import { Entry } from './../../../shared/models/entry.model';
import { getEntriesSummary } from './../../store/entry.selectors';
import { TimeEntriesSummary } from '../../models/time.entry.summary';
import { LoadEntriesSummary, LoadActiveEntry, EntryActionTypes } from './../../store/entry.actions';
import { EntryState } from './../../store/entry.reducer';
import { Store, select, ActionsSubject } from '@ngrx/store';
import { Store, ActionsSubject } from '@ngrx/store';
import { Component, OnInit, OnDestroy } from '@angular/core';
import * as moment from 'moment';

@Component({
selector: 'app-time-entries-summary',
Expand All @@ -19,51 +19,48 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy {
timeEntriesSummary: TimeEntriesSummary;
currentWorkingTime: string;
destroyed$ = new Subject<boolean>();

loadActiveEntry: Subscription;
loadActiveEntryLost: Subscription;
stopEntry: Subscription;
startEntry: Subscription;
loadActiveEntry$: Subscription;
loadActiveEntryLost$: Subscription;
stopEntry$: Subscription;
startEntry$: Subscription;
timeEntriesSummary$: Subscription;
timeInterval;

constructor(private store: Store<EntryState>, private actionsSubject$: ActionsSubject) { }

ngOnDestroy(): void {
this.loadActiveEntry.unsubscribe();
this.loadActiveEntryLost.unsubscribe();
this.stopEntry.unsubscribe();
this.startEntry.unsubscribe();
this.loadActiveEntry$.unsubscribe();
this.loadActiveEntryLost$.unsubscribe();
this.stopEntry$.unsubscribe();
this.startEntry$.unsubscribe();
this.timeEntriesSummary$.unsubscribe();
}

ngOnInit(): void {
this.store.dispatch(new LoadActiveEntry());

this.loadActiveEntryLost = this.actionsSubject$.pipe(
this.loadActiveEntryLost$ = this.actionsSubject$.pipe(
filter((action: any) => (
action.type === EntryActionTypes.LOAD_ACTIVE_ENTRY_FAIL
))
).subscribe(() => {
this.blankCurrentWorkingTime();
});

this.loadActiveEntry = this.actionsSubject$.pipe(
this.loadActiveEntry$ = this.actionsSubject$.pipe(
filter((action: any) => (
action.type === EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS
))
).subscribe((action) => {
this.updateCurrentWorkingHours(action.payload);
});

this.stopEntry = this.actionsSubject$.pipe(
this.stopEntry$ = this.actionsSubject$.pipe(
filter((action: any) => (
action.type === EntryActionTypes.STOP_TIME_ENTRY_RUNNING_SUCCESS
))
).subscribe(() => {
this.destroyed$.next(true);
this.blankCurrentWorkingTime();
});

this.startEntry = this.actionsSubject$.pipe(
this.startEntry$ = this.actionsSubject$.pipe(
filter((action: any) => (
action.type === EntryActionTypes.CREATE_ENTRY_SUCCESS
))
Expand All @@ -73,9 +70,12 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy {
});

this.store.dispatch(new LoadEntriesSummary());
const timeEntriesSummary$ = this.store.pipe(select(getEntriesSummary));
timeEntriesSummary$.subscribe((response) => {
this.timeEntriesSummary = response;
this.timeEntriesSummary$ = this.actionsSubject$.pipe(
filter((action: any) => (
action.type === EntryActionTypes.LOAD_ENTRIES_SUMMARY_SUCCESS
))
).subscribe((response) => {
this.timeEntriesSummary = response.payload;
});
}

Expand All @@ -91,7 +91,15 @@ export class TimeEntriesSummaryComponent implements OnInit, OnDestroy {
this.timeInterval = interval(1000).pipe(
takeUntil(this.destroyed$)
).subscribe(() => {
this.currentWorkingTime = new SubstractDatePipe().transform(new Date(), new Date(entry.start_date), true);
const endDate = new Date();
const startDate = new Date(entry.start_date);
this.currentWorkingTime = new SubstractDatePipe().transform(endDate, startDate, true);

const aMinuteHasElapsed = moment(startDate).diff(endDate, 'seconds') % 60 === 0 ;
if (aMinuteHasElapsed) {
this.store.dispatch(new LoadEntriesSummary());
}

});
}
}
Expand Down