Skip to content

Commit 147ceab

Browse files
Copilotrenemadsen
andcommitted
Migrate to ngrx state structure with actions, reducers, and selectors
Co-authored-by: renemadsen <[email protected]>
1 parent b7e5ac8 commit 147ceab

File tree

8 files changed

+178
-99
lines changed

8 files changed

+178
-99
lines changed

eform-client/src/app/plugins/modules/customers-pn/components/store/customers-state.service.ts

Lines changed: 60 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,121 @@
1-
import { Injectable } from '@angular/core';
2-
import { BehaviorSubject, Observable } from 'rxjs';
1+
import {Injectable} from '@angular/core';
2+
import {Observable, tap} from 'rxjs';
33
import {
4+
CommonPaginationState,
45
OperationDataResult,
56
PaginationModel,
67
SortModel,
7-
FiltrationStateModel,
8-
CommonPaginationState,
98
} 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'})
3727
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;
3932

4033
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);
5039
}
5140

5241
getAllCustomers(): Observable<OperationDataResult<CustomersPnModel>> {
5342
return this.service
5443
.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,
5948
})
6049
.pipe(
61-
map((response) => {
50+
tap((response) => {
6251
if (response && response.success && response.model) {
63-
this.updateState({ total: response.model.total });
52+
this.store.dispatch(customersUpdateTotalCustomers(response.model.total));
6453
}
65-
return response;
6654
})
6755
);
6856
}
6957

7058
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}));
8161
}
8262

8363
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}));
9065
this.checkOffset();
9166
}
9267

9368
getPageSize(): Observable<number> {
94-
return this.state$.pipe(map(state => state.pagination.pageSize));
69+
return this.store.select(selectCustomersPaginationPageSize);
9570
}
9671

9772
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))
10075
);
10176
}
10277

10378
getNameFilter(): Observable<string> {
104-
return this.state$.pipe(map(state => state.filters.nameFilter));
79+
return this.store.select(selectCustomersNameFilters);
10580
}
10681

10782
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}));
11484
}
11585

11686
onDelete() {
117-
this.updateState({ total: this.state.total - 1 });
87+
const currentTotal = this.currentPagination.total || 0;
88+
this.store.dispatch(customersUpdateTotalCustomers(currentTotal - 1));
11889
this.checkOffset();
11990
}
12091

12192
onSortTable(sort: string) {
12293
const localPageSettings = updateTableSort(
12394
sort,
124-
this.state.pagination.sort,
125-
this.state.pagination.isSortDsc
95+
this.currentPagination.sort,
96+
this.currentPagination.isSortDsc
12697
);
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}));
13499
}
135100

136101
checkOffset() {
102+
const currentTotal = this.currentPagination.total || 0;
137103
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
141107
);
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}));
149110
}
150111
}
151112

152113
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
158119
))
159120
);
160121
}

eform-client/src/app/plugins/modules/customers-pn/customers-pn.module.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms';
44
import { NgSelectModule } from '@ng-select/ng-select';
55
import { TranslateModule } from '@ngx-translate/core';
66
import { RouterModule } from '@angular/router';
7+
import { StoreModule } from '@ngrx/store';
78
import { CustomerPnLayoutComponent } from './layouts';
89
import {
910
CustomersPnFieldsService,
@@ -22,6 +23,7 @@ import {
2223
} from './components';
2324
import { EformSharedModule } from 'src/app/common/modules/eform-shared/eform-shared.module';
2425
import { CustomersStateService } from './components/store';
26+
import { customersReducer } from './state';
2527

2628
@NgModule({
2729
imports: [
@@ -34,6 +36,9 @@ import { CustomersStateService } from './components/store';
3436
NgSelectModule,
3537
EformSharedModule,
3638
RouterModule,
39+
StoreModule.forFeature('customersPn', {
40+
customersState: customersReducer,
41+
}),
3742
],
3843
declarations: [
3944
CustomerPnLayoutComponent,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {
2+
CustomersState,
3+
} from './';
4+
5+
export interface CustomersPnState {
6+
customersState: CustomersState;
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {createAction} from '@ngrx/store';
2+
import {CommonPaginationState} from 'src/app/common/models';
3+
import {CustomersFiltrationModel} from './';
4+
5+
export const customersUpdateFilters = createAction(
6+
'[Customers] Update Filters',
7+
(payload: CustomersFiltrationModel) => ({payload})
8+
);
9+
10+
export const customersUpdatePagination = createAction(
11+
'[Customers] Update Pagination',
12+
(payload: CommonPaginationState) => ({payload})
13+
);
14+
15+
export const customersUpdateTotalCustomers = createAction(
16+
'[Customers] Update Total Customers',
17+
(payload: number) => ({payload})
18+
);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {CommonPaginationState} from 'src/app/common/models';
2+
import {Action, createReducer, on} from '@ngrx/store';
3+
import {
4+
customersUpdateFilters,
5+
customersUpdatePagination,
6+
customersUpdateTotalCustomers
7+
} from './customers.actions';
8+
9+
export interface CustomersFiltrationModel {
10+
nameFilter: string;
11+
}
12+
13+
export interface CustomersState {
14+
pagination: CommonPaginationState;
15+
filters: CustomersFiltrationModel;
16+
total: number;
17+
}
18+
19+
export const customersInitialState: CustomersState = {
20+
pagination: {
21+
pageSize: 10,
22+
sort: 'Id',
23+
isSortDsc: false,
24+
offset: 0,
25+
pageIndex: 0,
26+
total: 0,
27+
},
28+
filters: {
29+
nameFilter: '',
30+
},
31+
total: 0,
32+
};
33+
34+
export const _customersReducer = createReducer(
35+
customersInitialState,
36+
on(customersUpdateFilters, (state, {payload}) => ({
37+
...state,
38+
filters: {
39+
...state.filters,
40+
...payload,
41+
},
42+
})),
43+
on(customersUpdatePagination, (state, {payload}) => ({
44+
...state,
45+
pagination: { ...state, ...payload, },
46+
})),
47+
on(customersUpdateTotalCustomers, (state, {payload}) => ({
48+
...state,
49+
pagination: {
50+
...state.pagination,
51+
total: payload,
52+
},
53+
total: payload,
54+
}
55+
)),
56+
);
57+
58+
export function customersReducer(state: CustomersState | undefined, action: Action) {
59+
return _customersReducer(state, action);
60+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
CustomersPnState,
3+
} from '../';
4+
import {createSelector} from '@ngrx/store';
5+
6+
const selectCustomersPn =
7+
(state: {customersPn: CustomersPnState}) => state.customersPn;
8+
export const selectCustomers =
9+
createSelector(selectCustomersPn, (state) => state.customersState);
10+
export const selectCustomersFilters =
11+
createSelector(selectCustomers, (state) => state.filters);
12+
export const selectCustomersPagination =
13+
createSelector(selectCustomers, (state) => state.pagination);
14+
export const selectCustomersPaginationSort =
15+
createSelector(selectCustomers, (state) => state.pagination.sort);
16+
export const selectCustomersPaginationIsSortDsc =
17+
createSelector(selectCustomers, (state) => state.pagination.isSortDsc ? 'desc' : 'asc');
18+
export const selectCustomersNameFilters =
19+
createSelector(selectCustomers, (state) => state.filters.nameFilter);
20+
export const selectCustomersPaginationPageSize =
21+
createSelector(selectCustomers, (state) => state.pagination.pageSize);
22+
export const selectCustomersTotal =
23+
createSelector(selectCustomers, (state) => state.total);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './customers.actions';
2+
export * from './customers.reducer';
3+
export * from './customers.selector';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './customers';
2+
export * from './customers-pn.state';

0 commit comments

Comments
 (0)