Skip to content

Commit

Permalink
Responding to connectivity changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Popovkov57 committed Sep 30, 2022
1 parent 8f3a440 commit c1ad015
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/app/model/connection.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable } from "@angular/core";
import { Observable, Subject } from "rxjs";

@Injectable()
export class ConnectionService {

private connEvents: Subject<boolean>;

constructor() {
this.connEvents = new Subject<boolean>();
window.addEventListener("online",
(e) => this.handleConnectionChange(e));
window.addEventListener("offline",
(e) => this.handleConnectionChange(e));
}

private handleConnectionChange(event:any) {
this.connEvents.next(this.connected);
}

get connected(): boolean {
return window.navigator.onLine;
}

get Changes(): Observable<boolean> {
return this.connEvents;
}

}
3 changes: 2 additions & 1 deletion src/app/model/model.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { OrderRepository } from "./order.repository";
import { RestDataSource } from "./rest.datasource";
import { HttpClientModule } from "@angular/common/http";
import { AuthService } from "./auth.service";
import { ConnectionService } from "./connection.service";

@NgModule({
imports: [HttpClientModule],
providers: [ProductRepository, Cart, Order, OrderRepository,
{ provide: StaticDataSource, useClass: RestDataSource }, RestDataSource, AuthService]
{ provide: StaticDataSource, useClass: RestDataSource }, RestDataSource, AuthService, ConnectionService]
})

export class ModelModule {}
4 changes: 2 additions & 2 deletions src/app/store/cartDetail.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ <h2 class="text-center">Your Cart</h2>
<button class="btn btn-primary m-1" routerLink="/store">
Continue Shopping
</button>
<button class="btn btn-secondary m-1" routerLink="/checkout" [disabled]="cart.lines.length == 0">
Checkout
<button class="btn btn-secondary m-1" routerLink="/checkout" [disabled]="cart.lines.length == 0 && connected">
{{ connected ? 'Checkout' : 'Offline' }}
</button>
</div>
</div>
Expand Down
8 changes: 7 additions & 1 deletion src/app/store/cartDetail.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Component } from "@angular/core";
import { Cart } from "../model/cart.model";
import { ConnectionService } from "../model/connection.service";

@Component({
templateUrl: "cartDetail.component.html"
})

export class CartDetailComponent {

constructor(public cart: Cart) {}
public connected: boolean = true;

constructor(public cart: Cart, private connection: ConnectionService) {
this.connected = this.connection.connected;
connection.Changes.subscribe((state) => this.connected = state);
}

}

0 comments on commit c1ad015

Please sign in to comment.