-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix issue for user preferences (#175)
Co-authored-by: Raul Jordan <[email protected]>
- Loading branch information
1 parent
20b895b
commit acd815e
Showing
8 changed files
with
267 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { UserService } from './user.service'; | ||
|
||
describe('UserService', () => { | ||
let service: UserService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(UserService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains 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,57 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { User } from '../types/user'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class UserService { | ||
constructor() { | ||
this.setUser(this.getUser()); | ||
} | ||
|
||
private userKeyStore = 'user-prysm'; | ||
|
||
private user: User | null | undefined; | ||
private onUserChange = new BehaviorSubject<User>((null as any) as User); | ||
user$ = this.onUserChange.asObservable(); | ||
|
||
changeAccountListPerPage(accountListPerPage: number): void { | ||
if (!this.user) { | ||
return; | ||
} | ||
this.user.acountsPerPage = accountListPerPage; | ||
this.saveChanges(); | ||
} | ||
|
||
changeGainsAndLosesPageSize(pageSize: number): void { | ||
if (!this.user) { | ||
return; | ||
} | ||
this.user.gainAndLosesPageSize = pageSize; | ||
this.saveChanges(); | ||
} | ||
|
||
private saveChanges(): void { | ||
if (this.user) { | ||
localStorage.setItem(this.userKeyStore, JSON.stringify(this.user)); | ||
this.onUserChange.next(this.user); | ||
} | ||
} | ||
|
||
private getUser(): User { | ||
const userStr = localStorage.getItem(this.userKeyStore); | ||
if (!userStr) { | ||
const user = new User(); | ||
return user; | ||
} else { | ||
const user = new User(JSON.parse(userStr)); | ||
return user; | ||
} | ||
} | ||
|
||
private setUser(user: User): void { | ||
this.user = user; | ||
this.saveChanges(); | ||
} | ||
} |
This file contains 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,8 @@ | ||
export class User { | ||
constructor(init?: Partial<User>) { | ||
Object.assign(this, init); | ||
} | ||
acountsPerPage = 5; | ||
gainAndLosesPageSize = 5; | ||
pageSizeOptions: number[] = [5, 10, 50, 100, 250]; | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.