|
1 | | -import { Injectable } from '@angular/core'; |
2 | | -import { BehaviorSubject, Observable } from 'rxjs'; |
| 1 | +import {Injectable} from '@angular/core'; |
| 2 | +import {Observable, tap} from 'rxjs'; |
3 | 3 | import { |
| 4 | + CommonPaginationState, |
4 | 5 | OperationDataResult, |
5 | 6 | PaginationModel, |
6 | 7 | SortModel, |
7 | | - FiltrationStateModel, |
8 | | - CommonPaginationState, |
9 | 8 | } from 'src/app/common/models'; |
10 | | -import { updateTableSort, getOffset } from 'src/app/common/helpers'; |
11 | | -import { map } from 'rxjs/operators'; |
12 | | -import { CustomersPnService } from '../../services'; |
13 | | -import { CustomersPnModel } from '../../models'; |
14 | | - |
15 | | -export interface CustomersState { |
16 | | - pagination: CommonPaginationState; |
17 | | - filters: FiltrationStateModel; |
18 | | - total: number; |
19 | | -} |
20 | | - |
21 | | -function createInitialState(): CustomersState { |
22 | | - return <CustomersState>{ |
23 | | - pagination: { |
24 | | - pageSize: 10, |
25 | | - sort: 'Id', |
26 | | - isSortDsc: false, |
27 | | - offset: 0, |
28 | | - }, |
29 | | - filters: { |
30 | | - nameFilter: '', |
31 | | - }, |
32 | | - total: 0, |
33 | | - }; |
34 | | -} |
35 | | - |
36 | | -@Injectable({ providedIn: 'root' }) |
| 9 | +import {updateTableSort, getOffset} from 'src/app/common/helpers'; |
| 10 | +import {CustomersPnService} from '../../services'; |
| 11 | +import {Store} from '@ngrx/store'; |
| 12 | +import { |
| 13 | + selectCustomersFilters, |
| 14 | + selectCustomersPagination, |
| 15 | + customersUpdateTotalCustomers, |
| 16 | + customersUpdateFilters, |
| 17 | + customersUpdatePagination, |
| 18 | + CustomersFiltrationModel, |
| 19 | + selectCustomersPaginationPageSize, |
| 20 | + selectCustomersNameFilters, |
| 21 | + selectCustomersTotal, |
| 22 | +} from '../../state'; |
| 23 | +import {CustomersPnModel} from '../../models'; |
| 24 | +import {map} from 'rxjs/operators'; |
| 25 | + |
| 26 | +@Injectable({providedIn: 'root'}) |
37 | 27 | export class CustomersStateService { |
38 | | - private state$ = new BehaviorSubject<CustomersState>(createInitialState()); |
| 28 | + private selectCustomersFilters$ = this.store.select(selectCustomersFilters); |
| 29 | + private selectCustomersPagination$ = this.store.select(selectCustomersPagination); |
| 30 | + currentPagination: CommonPaginationState; |
| 31 | + currentFilters: CustomersFiltrationModel; |
39 | 32 |
|
40 | 33 | constructor( |
41 | | - private service: CustomersPnService |
42 | | - ) {} |
43 | | - |
44 | | - private get state(): CustomersState { |
45 | | - return this.state$.value; |
46 | | - } |
47 | | - |
48 | | - private updateState(update: Partial<CustomersState>) { |
49 | | - this.state$.next({ ...this.state, ...update }); |
| 34 | + private store: Store, |
| 35 | + private service: CustomersPnService, |
| 36 | + ) { |
| 37 | + this.selectCustomersPagination$.subscribe(x => this.currentPagination = x); |
| 38 | + this.selectCustomersFilters$.subscribe(x => this.currentFilters = x); |
50 | 39 | } |
51 | 40 |
|
52 | 41 | getAllCustomers(): Observable<OperationDataResult<CustomersPnModel>> { |
53 | 42 | return this.service |
54 | 43 | .getAllCustomers({ |
55 | | - ...this.state.pagination, |
56 | | - ...this.state.filters, |
57 | | - sortColumnName: this.state.pagination.sort, |
58 | | - name: this.state.filters.nameFilter, |
| 44 | + ...this.currentFilters, |
| 45 | + ...this.currentPagination, |
| 46 | + sortColumnName: this.currentPagination.sort, |
| 47 | + name: this.currentFilters.nameFilter, |
59 | 48 | }) |
60 | 49 | .pipe( |
61 | | - map((response) => { |
| 50 | + tap((response) => { |
62 | 51 | if (response && response.success && response.model) { |
63 | | - this.updateState({ total: response.model.total }); |
| 52 | + this.store.dispatch(customersUpdateTotalCustomers(response.model.total)); |
64 | 53 | } |
65 | | - return response; |
66 | 54 | }) |
67 | 55 | ); |
68 | 56 | } |
69 | 57 |
|
70 | 58 | updateNameFilter(nameFilter: string) { |
71 | | - this.updateState({ |
72 | | - filters: { |
73 | | - ...this.state.filters, |
74 | | - nameFilter: nameFilter, |
75 | | - }, |
76 | | - pagination: { |
77 | | - ...this.state.pagination, |
78 | | - offset: 0, |
79 | | - }, |
80 | | - }); |
| 59 | + this.store.dispatch(customersUpdateFilters({nameFilter: nameFilter})); |
| 60 | + this.store.dispatch(customersUpdatePagination({...this.currentPagination, offset: 0})); |
81 | 61 | } |
82 | 62 |
|
83 | 63 | updatePageSize(pageSize: number) { |
84 | | - this.updateState({ |
85 | | - pagination: { |
86 | | - ...this.state.pagination, |
87 | | - pageSize: pageSize, |
88 | | - }, |
89 | | - }); |
| 64 | + this.store.dispatch(customersUpdatePagination({...this.currentPagination, pageSize: pageSize})); |
90 | 65 | this.checkOffset(); |
91 | 66 | } |
92 | 67 |
|
93 | 68 | getPageSize(): Observable<number> { |
94 | | - return this.state$.pipe(map(state => state.pagination.pageSize)); |
| 69 | + return this.store.select(selectCustomersPaginationPageSize); |
95 | 70 | } |
96 | 71 |
|
97 | 72 | getSort(): Observable<SortModel> { |
98 | | - return this.state$.pipe( |
99 | | - map(state => new SortModel(state.pagination.sort, state.pagination.isSortDsc)) |
| 73 | + return this.selectCustomersPagination$.pipe( |
| 74 | + map(pagination => new SortModel(pagination.sort, pagination.isSortDsc)) |
100 | 75 | ); |
101 | 76 | } |
102 | 77 |
|
103 | 78 | getNameFilter(): Observable<string> { |
104 | | - return this.state$.pipe(map(state => state.filters.nameFilter)); |
| 79 | + return this.store.select(selectCustomersNameFilters); |
105 | 80 | } |
106 | 81 |
|
107 | 82 | changePage(offset: number) { |
108 | | - this.updateState({ |
109 | | - pagination: { |
110 | | - ...this.state.pagination, |
111 | | - offset: offset, |
112 | | - }, |
113 | | - }); |
| 83 | + this.store.dispatch(customersUpdatePagination({...this.currentPagination, offset: offset})); |
114 | 84 | } |
115 | 85 |
|
116 | 86 | onDelete() { |
117 | | - this.updateState({ total: this.state.total - 1 }); |
| 87 | + const currentTotal = this.currentPagination.total || 0; |
| 88 | + this.store.dispatch(customersUpdateTotalCustomers(currentTotal - 1)); |
118 | 89 | this.checkOffset(); |
119 | 90 | } |
120 | 91 |
|
121 | 92 | onSortTable(sort: string) { |
122 | 93 | const localPageSettings = updateTableSort( |
123 | 94 | sort, |
124 | | - this.state.pagination.sort, |
125 | | - this.state.pagination.isSortDsc |
| 95 | + this.currentPagination.sort, |
| 96 | + this.currentPagination.isSortDsc |
126 | 97 | ); |
127 | | - this.updateState({ |
128 | | - pagination: { |
129 | | - ...this.state.pagination, |
130 | | - isSortDsc: localPageSettings.isSortDsc, |
131 | | - sort: localPageSettings.sort, |
132 | | - }, |
133 | | - }); |
| 98 | + this.store.dispatch(customersUpdatePagination({...this.currentPagination, ...localPageSettings})); |
134 | 99 | } |
135 | 100 |
|
136 | 101 | checkOffset() { |
| 102 | + const currentTotal = this.currentPagination.total || 0; |
137 | 103 | const newOffset = getOffset( |
138 | | - this.state.pagination.pageSize, |
139 | | - this.state.pagination.offset, |
140 | | - this.state.total |
| 104 | + this.currentPagination.pageSize, |
| 105 | + this.currentPagination.offset, |
| 106 | + currentTotal |
141 | 107 | ); |
142 | | - if (newOffset !== this.state.pagination.offset) { |
143 | | - this.updateState({ |
144 | | - pagination: { |
145 | | - ...this.state.pagination, |
146 | | - offset: newOffset, |
147 | | - }, |
148 | | - }); |
| 108 | + if (newOffset !== this.currentPagination.offset) { |
| 109 | + this.store.dispatch(customersUpdatePagination({...this.currentPagination, offset: newOffset})); |
149 | 110 | } |
150 | 111 | } |
151 | 112 |
|
152 | 113 | getPagination(): Observable<PaginationModel> { |
153 | | - return this.state$.pipe( |
154 | | - map(state => new PaginationModel( |
155 | | - state.total, |
156 | | - state.pagination.pageSize, |
157 | | - state.pagination.offset |
| 114 | + return this.store.select(selectCustomersTotal).pipe( |
| 115 | + map(total => new PaginationModel( |
| 116 | + total, |
| 117 | + this.currentPagination.pageSize, |
| 118 | + this.currentPagination.offset |
158 | 119 | )) |
159 | 120 | ); |
160 | 121 | } |
|
0 commit comments