Skip to content

Commit

Permalink
fix: last sync state is correctly set (#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal authored Apr 27, 2022
1 parent 44ea03b commit c04df2a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ import { SupportModule } from "./core/support/support.module";
AttendanceModule,
DashboardShortcutWidgetModule,
HistoricalDataModule,
SupportModule,
],
providers: [
{ provide: ErrorHandler, useClass: LoggingErrorHandler },
{ provide: MatPaginatorIntl, useValue: TranslatableMatPaginator() },
{ provide: RouteRegistry, useValue: routesRegistry },
AnalyticsService,
Angulartics2Matomo,
SupportModule,
],
bootstrap: [AppComponent],
})
Expand Down
10 changes: 10 additions & 0 deletions src/app/core/session/session-service/synced-session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { DatabaseUser } from "./local-user";
import { waitForChangeTo } from "../session-states/session-utils";
import { PouchDatabase } from "../../database/pouch-database";
import { zip } from "rxjs";
import { filter } from "rxjs/operators";

/**
* A synced session creates and manages a LocalSession and a RemoteSession
Expand All @@ -40,6 +41,7 @@ import { zip } from "rxjs";
*/
@Injectable()
export class SyncedSessionService extends SessionService {
static readonly LAST_SYNC_KEY = "LAST_SYNC";
private readonly LOGIN_RETRY_TIMEOUT = 60000;
private readonly POUCHDB_SYNC_BATCH_SIZE = 500;

Expand All @@ -58,6 +60,14 @@ export class SyncedSessionService extends SessionService {
super();
this._localSession = new LocalSession(pouchDatabase);
this._remoteSession = new RemoteSession(this.httpClient, loggingService);
this.syncState
.pipe(filter((state) => state === SyncState.COMPLETED))
.subscribe(() =>
localStorage.setItem(
SyncedSessionService.LAST_SYNC_KEY,
new Date().toISOString()
)
);
}

/**
Expand Down
13 changes: 0 additions & 13 deletions src/app/core/support/support.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { SupportComponent } from "./support/support.component";
import { MatButtonModule } from "@angular/material/button";
import { SessionService } from "../session/session-service/session.service";
import { filter } from "rxjs/operators";
import { SyncState } from "../session/session-states/sync-state.enum";
import { ConfirmationDialogModule } from "../confirmation-dialog/confirmation-dialog.module";
import { FlexModule } from "@angular/flex-layout";
import { MatExpansionModule } from "@angular/material/expansion";
Expand All @@ -22,14 +19,4 @@ import { MatExpansionModule } from "@angular/material/expansion";
})
export class SupportModule {
static dynamicComponents = [SupportComponent];
constructor(sessionService: SessionService) {
sessionService.syncState
.pipe(filter((state) => state === SyncState.COMPLETED))
.subscribe(() =>
localStorage.setItem(
SupportComponent.LAST_SYNC_KEY,
new Date().toISOString()
)
);
}
}
3 changes: 2 additions & 1 deletion src/app/core/support/support/support.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ConfirmationDialogService } from "../../confirmation-dialog/confirmatio
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { HttpClient } from "@angular/common/http";
import { NoopAnimationsModule } from "@angular/platform-browser/animations";
import { SyncedSessionService } from "../../session/session-service/synced-session.service";

describe("SupportComponent", () => {
let component: SupportComponent;
Expand Down Expand Up @@ -76,7 +77,7 @@ describe("SupportComponent", () => {

it("should correctly read sync and remote login status from local storage", () => {
const lastSync = new Date("2022-01-01").toISOString();
localStorage.setItem(SupportComponent.LAST_SYNC_KEY, lastSync);
localStorage.setItem(SyncedSessionService.LAST_SYNC_KEY, lastSync);
const lastRemoteLogin = new Date("2022-01-02").toISOString();
localStorage.setItem(RemoteSession.LAST_LOGIN_KEY, lastRemoteLogin);

Expand Down
4 changes: 2 additions & 2 deletions src/app/core/support/support/support.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { RemoteSession } from "../../session/session-service/remote-session";
import { RouteTarget } from "../../../app.routing";
import { ConfirmationDialogService } from "../../confirmation-dialog/confirmation-dialog.service";
import { HttpClient } from "@angular/common/http";
import { SyncedSessionService } from "../../session/session-service/synced-session.service";

@RouteTarget("Support")
@Component({
Expand All @@ -19,7 +20,6 @@ import { HttpClient } from "@angular/common/http";
styleUrls: ["./support.component.scss"],
})
export class SupportComponent implements OnInit {
static readonly LAST_SYNC_KEY = "LAST_SYNC";
currentUser: DatabaseUser;
currentSyncState: string;
lastSync: string;
Expand Down Expand Up @@ -61,7 +61,7 @@ export class SupportComponent implements OnInit {

private initLastSync() {
this.lastSync =
localStorage.getItem(SupportComponent.LAST_SYNC_KEY) || "never";
localStorage.getItem(SyncedSessionService.LAST_SYNC_KEY) || "never";
}

private initLastRemoteLogin() {
Expand Down

0 comments on commit c04df2a

Please sign in to comment.