-
Notifications
You must be signed in to change notification settings - Fork 1
TTA 115 time tracker doesnt notifies the user when they lost internet connection or have a slow connection #928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
abigailscl
wants to merge
20
commits into
master
from
TTA-115-time-tracker-doesnt-notifies-the-user-when-they-lost-internet-connection-or-have-a-slow-connection
Closed
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d0c5298
TTA-115 implement a stable, slow or offline connection warning
e04763f
TTA-115 implement a stable, slow or offline connection warning
b56113b
TTA-115 rebase master
84bde1c
TTA-115 Refactor end points
da0522a
feat: TTA-115 implement notification when the endpoint fails for a sl…
4665966
refactor: TTA-115 the if clausses in status network service and conne…
07809f5
refactor: TTA-115 create unit test for function checkTypeError in sta…
fe5b055
refactor: TTA-115 unit test for function checkTypeError in status net…
1d40b7d
refactor: TTA-115 fix linters errors
5f65148
refactor: TTA-115 increase test coverage in status network service
a912865
refactor: TTA-115 increase test coverage in directives
940f4b8
refactor: TTA-115 solve github comment
470a043
fix: TTA-115 Merge master into TTA-115
5f8aac5
Merge remote-tracking branch 'origin/master' into TTA-115-time-tracke…
2e9e2cb
fix: TTA-115 fix code smells
e88247c
fix: change API URL in prod :'v
37cec9b
Update src/app/modules/internet-connection-status/internet-connection…
Yalian 8e72487
refactor: TTA-115 solve review comments
5bc7915
refactor: TTA-115 solve linting
aabefaf
refactor: TTA-115 changed test descriptions - lint
mmaquina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
TTA-115 implement a stable, slow or offline connection warning
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
<app-internet-connection-status listen="true"> | ||
|
||
<ng-container *fast> | ||
</ng-container> | ||
|
||
<ng-container *slow> | ||
</ng-container> | ||
|
||
<ng-container *offline> | ||
</ng-container> | ||
|
||
</app-internet-connection-status> | ||
<router-outlet></router-outlet> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...modules/internet-connection-status/internet-connection-directives/connection.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Directive, Attribute, ElementRef, OnInit } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[appConnection]' | ||
}) | ||
|
||
export class ConnectionDirective implements OnInit { | ||
constructor( | ||
@Attribute('slowSrc') private slowSrc, | ||
@Attribute('fastSrc') private fastSrc, | ||
@Attribute('offlineSrc') private offlineSrc, | ||
private host: ElementRef<HTMLImageElement> | ||
) { | ||
} | ||
|
||
ngOnInit() { | ||
const { effectiveType } = navigator.connection; | ||
let src; | ||
if (/\fast-5g|3g|4g/.test(effectiveType)) { | ||
src = this.fastSrc; | ||
}else if (/\slow-2g|2g/.test(effectiveType)) { | ||
src = this.slowSrc; | ||
}else { | ||
src = this.offlineSrc; | ||
} | ||
this.host.nativeElement.setAttribute('src', src); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/app/modules/internet-connection-status/internet-connection-directives/fast.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Directive, TemplateRef } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[appFast]' | ||
}) | ||
|
||
export class FastDirective { | ||
|
||
constructor(public tpl: TemplateRef<any>) { } | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...pp/modules/internet-connection-status/internet-connection-directives/offline.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Directive, TemplateRef } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[appOffline]' | ||
}) | ||
|
||
export class OfflineDirective { | ||
|
||
constructor(public tpl: TemplateRef<any>) { } | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/app/modules/internet-connection-status/internet-connection-directives/slow.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Directive, TemplateRef } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[appSlow]' | ||
}) | ||
|
||
export class SlowDirective { | ||
|
||
constructor(public tpl: TemplateRef<any>) { } | ||
|
||
} |
Empty file.
Empty file.
88 changes: 88 additions & 0 deletions
88
src/app/modules/internet-connection-status/internet-connection-status.component.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,88 @@ | ||||||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||||||
import { InternetConnectionStatusComponent } from './internet-connection-status.component'; | ||||||
import { ToastrService } from 'ngx-toastr'; | ||||||
import { of } from 'rxjs'; | ||||||
|
||||||
describe('InternetConnectionStatusComponent', () => { | ||||||
let component: InternetConnectionStatusComponent; | ||||||
let fixture: ComponentFixture<InternetConnectionStatusComponent>; | ||||||
const toastrServiceStub = { | ||||||
error: () => { | ||||||
return 'test error'; | ||||||
}, | ||||||
warning: () => { | ||||||
return 'warning error'; | ||||||
}, | ||||||
success: () => { | ||||||
return 'success error'; | ||||||
} | ||||||
}; | ||||||
|
||||||
beforeEach(async () => { | ||||||
await TestBed.configureTestingModule({ | ||||||
declarations: [ InternetConnectionStatusComponent ], | ||||||
providers: [{ provide: ToastrService, useValue: toastrServiceStub }] | ||||||
}) | ||||||
.compileComponents(); | ||||||
}); | ||||||
|
||||||
beforeEach(() => { | ||||||
fixture = TestBed.createComponent(InternetConnectionStatusComponent); | ||||||
component = fixture.componentInstance; | ||||||
fixture.detectChanges(); | ||||||
}); | ||||||
|
||||||
it('should show a stable connection warning', () => { | ||||||
component.connection$ = of('4g'); | ||||||
fixture.detectChanges(); | ||||||
component.ngOnInit(); | ||||||
expect(component.isFast).toBe(true); | ||||||
}); | ||||||
|
||||||
it('should show a slow connection warning', () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
component.connection$ = of('2g'); | ||||||
fixture.detectChanges(); | ||||||
component.ngOnInit(); | ||||||
expect(component.isFast).toBe(false); | ||||||
}); | ||||||
|
||||||
it('should show offline warning', () => { | ||||||
component.connection$ = of('Offline'); | ||||||
fixture.detectChanges(); | ||||||
component.ngOnInit(); | ||||||
expect(component.isFast).toBe(false); | ||||||
}); | ||||||
|
||||||
it('should change the network type', () => { | ||||||
component.connection$ = of('5g'); | ||||||
fixture.detectChanges(); | ||||||
component.ngOnInit(); | ||||||
component.connection$.subscribe((networkType: string) => { | ||||||
expect(networkType).toEqual('5g'); | ||||||
}); | ||||||
|
||||||
jasmine.clock().install(); | ||||||
jasmine.clock().tick(300); | ||||||
|
||||||
component.connection$ = of('2g'); | ||||||
fixture.detectChanges(); | ||||||
console.log(window.navigator); | ||||||
component.connection$.subscribe((networkType: string) => { | ||||||
expect(networkType).toEqual('2g'); | ||||||
}); | ||||||
|
||||||
jasmine.clock().uninstall(); | ||||||
}); | ||||||
|
||||||
it('should return when window.navigator.connection is undefined', () => { | ||||||
const attributeCustome = '__defineGetter__'; | ||||||
window.navigator[attributeCustome]('connection', () => { | ||||||
return undefined; | ||||||
}); | ||||||
fixture = TestBed.createComponent(InternetConnectionStatusComponent); | ||||||
component = fixture.componentInstance; | ||||||
fixture.detectChanges(); | ||||||
const statusInit = component.ngOnInit(); | ||||||
expect(statusInit).toEqual(undefined); | ||||||
}); | ||||||
}); |
85 changes: 85 additions & 0 deletions
85
src/app/modules/internet-connection-status/internet-connection-status.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Component, OnInit, ContentChild, Attribute, OnDestroy} from '@angular/core'; | ||
import { FastDirective} from '../../modules/internet-connection-status/internet-connection-directives/fast.directive'; | ||
import { SlowDirective} from '../../modules/internet-connection-status/internet-connection-directives/slow.directive'; | ||
import { OfflineDirective} from '../../modules/internet-connection-status/internet-connection-directives/offline.directive'; | ||
import { Observable, of, Subscription } from 'rxjs'; | ||
import { take, map } from 'rxjs/operators'; | ||
import { ToastrService } from 'ngx-toastr'; | ||
|
||
type Connection = { | ||
effectiveType: string; | ||
}; | ||
|
||
declare global { | ||
interface Navigator { | ||
connection: { | ||
effectiveType: string; | ||
addEventListener: (a: any, b: any) => {}; | ||
removeEventListener: (a: any, b: any) => {}; | ||
}; | ||
} | ||
} | ||
|
||
|
||
@Component({ | ||
selector: 'app-internet-connection-status', | ||
templateUrl: './internet-connection-status.component.html', | ||
styleUrls: ['./internet-connection-status.component.scss'] | ||
}) | ||
export class InternetConnectionStatusComponent implements OnInit { | ||
isFast = true; | ||
connectionType: string; | ||
networkType: string; | ||
@ContentChild(FastDirective) fast: FastDirective; | ||
@ContentChild(SlowDirective) slow: SlowDirective; | ||
@ContentChild(OfflineDirective) offline: OfflineDirective; | ||
|
||
private subscription: Subscription; | ||
|
||
constructor(@Attribute('listen') private withChanges: boolean, private toastrService: ToastrService) { | ||
} | ||
|
||
connection$ = new Observable((observer) => { | ||
const { effectiveType } = navigator.connection; | ||
observer.next(effectiveType); | ||
|
||
const onConnectionChange = () => { | ||
this.networkType = navigator.connection.effectiveType; | ||
observer.next(this.networkType); | ||
}; | ||
|
||
navigator.connection.addEventListener('change', onConnectionChange); | ||
|
||
return () => { | ||
navigator.connection.removeEventListener('change', onConnectionChange); | ||
observer.complete(); | ||
}; | ||
}); | ||
|
||
ngOnInit() { | ||
const connection = navigator.connection; | ||
console.log('navigator component', connection); | ||
if (!connection || !connection.effectiveType) { | ||
return; | ||
} | ||
|
||
this.subscription = this.connection$ | ||
.pipe(take(this.withChanges ? Number.POSITIVE_INFINITY : 1)) | ||
.subscribe((effectiveType: string) => { | ||
|
||
this.connectionType = effectiveType; | ||
|
||
if (/\fast-5g|3g|4g/.test(effectiveType)){ | ||
this.toastrService.success('You have a good connection'); | ||
this.isFast = true; | ||
}else if (/\slow-2g|2g/.test(effectiveType)) { | ||
this.toastrService.warning('Caution your connection is slow'); | ||
this.isFast = false; | ||
} else { | ||
this.toastrService.error('You are offline'); | ||
this.isFast = false; | ||
} | ||
}); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.