Skip to content

Commit b7e5ac8

Browse files
Copilotrenemadsen
andcommitted
Remove Akita dependencies and update to use BehaviorSubject-based state management
Co-authored-by: renemadsen <[email protected]>
1 parent bfe4f8b commit b7e5ac8

File tree

6 files changed

+186
-140
lines changed

6 files changed

+186
-140
lines changed
Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,161 @@
11
import { Injectable } from '@angular/core';
2-
import { Observable } from 'rxjs';
2+
import { BehaviorSubject, Observable } from 'rxjs';
33
import {
44
OperationDataResult,
55
PaginationModel,
66
SortModel,
7+
FiltrationStateModel,
8+
CommonPaginationState,
79
} from 'src/app/common/models';
810
import { updateTableSort, getOffset } from 'src/app/common/helpers';
911
import { map } from 'rxjs/operators';
10-
import { CustomersQuery, CustomersStore } from './';
1112
import { CustomersPnService } from '../../services';
1213
import { CustomersPnModel } from '../../models';
1314

14-
@Injectable()
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' })
1537
export class CustomersStateService {
38+
private state$ = new BehaviorSubject<CustomersState>(createInitialState());
39+
1640
constructor(
17-
private store: CustomersStore,
18-
private service: CustomersPnService,
19-
private query: CustomersQuery
41+
private service: CustomersPnService
2042
) {}
2143

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 });
50+
}
51+
2252
getAllCustomers(): Observable<OperationDataResult<CustomersPnModel>> {
2353
return this.service
2454
.getAllCustomers({
25-
...this.query.pageSetting.pagination,
26-
...this.query.pageSetting.filters,
27-
sortColumnName: this.query.pageSetting.pagination.sort,
28-
name: this.query.pageSetting.filters.nameFilter,
55+
...this.state.pagination,
56+
...this.state.filters,
57+
sortColumnName: this.state.pagination.sort,
58+
name: this.state.filters.nameFilter,
2959
})
3060
.pipe(
3161
map((response) => {
3262
if (response && response.success && response.model) {
33-
this.store.update(() => ({
34-
total: response.model.total,
35-
}));
63+
this.updateState({ total: response.model.total });
3664
}
3765
return response;
3866
})
3967
);
4068
}
4169

4270
updateNameFilter(nameFilter: string) {
43-
this.store.update((state) => ({
71+
this.updateState({
4472
filters: {
45-
...state.filters,
73+
...this.state.filters,
4674
nameFilter: nameFilter,
4775
},
4876
pagination: {
49-
...state.pagination,
77+
...this.state.pagination,
5078
offset: 0,
5179
},
52-
}));
80+
});
5381
}
5482

5583
updatePageSize(pageSize: number) {
56-
this.store.update((state) => ({
84+
this.updateState({
5785
pagination: {
58-
...state.pagination,
86+
...this.state.pagination,
5987
pageSize: pageSize,
6088
},
61-
}));
89+
});
6290
this.checkOffset();
6391
}
6492

6593
getPageSize(): Observable<number> {
66-
return this.query.selectPageSize$;
94+
return this.state$.pipe(map(state => state.pagination.pageSize));
6795
}
6896

6997
getSort(): Observable<SortModel> {
70-
return this.query.selectSort$;
98+
return this.state$.pipe(
99+
map(state => new SortModel(state.pagination.sort, state.pagination.isSortDsc))
100+
);
71101
}
72102

73103
getNameFilter(): Observable<string> {
74-
return this.query.selectNameFilter$;
104+
return this.state$.pipe(map(state => state.filters.nameFilter));
75105
}
76106

77107
changePage(offset: number) {
78-
this.store.update((state) => ({
108+
this.updateState({
79109
pagination: {
80-
...state.pagination,
110+
...this.state.pagination,
81111
offset: offset,
82112
},
83-
}));
113+
});
84114
}
85115

86116
onDelete() {
87-
this.store.update((state) => ({
88-
total: state.total - 1,
89-
}));
117+
this.updateState({ total: this.state.total - 1 });
90118
this.checkOffset();
91119
}
92120

93121
onSortTable(sort: string) {
94122
const localPageSettings = updateTableSort(
95123
sort,
96-
this.query.pageSetting.pagination.sort,
97-
this.query.pageSetting.pagination.isSortDsc
124+
this.state.pagination.sort,
125+
this.state.pagination.isSortDsc
98126
);
99-
this.store.update((state) => ({
127+
this.updateState({
100128
pagination: {
101-
...state.pagination,
129+
...this.state.pagination,
102130
isSortDsc: localPageSettings.isSortDsc,
103131
sort: localPageSettings.sort,
104132
},
105-
}));
133+
});
106134
}
107135

108136
checkOffset() {
109137
const newOffset = getOffset(
110-
this.query.pageSetting.pagination.pageSize,
111-
this.query.pageSetting.pagination.offset,
112-
this.query.pageSetting.total
138+
this.state.pagination.pageSize,
139+
this.state.pagination.offset,
140+
this.state.total
113141
);
114-
if (newOffset !== this.query.pageSetting.pagination.offset) {
115-
this.store.update((state) => ({
142+
if (newOffset !== this.state.pagination.offset) {
143+
this.updateState({
116144
pagination: {
117-
...state.pagination,
145+
...this.state.pagination,
118146
offset: newOffset,
119147
},
120-
}));
148+
});
121149
}
122150
}
123151

124152
getPagination(): Observable<PaginationModel> {
125-
return this.query.selectPagination$;
153+
return this.state$.pipe(
154+
map(state => new PaginationModel(
155+
state.total,
156+
state.pagination.pageSize,
157+
state.pagination.offset
158+
))
159+
);
126160
}
127161
}
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
import { Injectable } from '@angular/core';
2-
import { Query } from '@datorama/akita';
3-
import { CustomersState, CustomersStore } from './';
4-
import { PaginationModel, SortModel } from 'src/app/common/models';
5-
6-
@Injectable({ providedIn: 'root' })
7-
export class CustomersQuery extends Query<CustomersState> {
8-
constructor(protected store: CustomersStore) {
9-
super(store);
10-
}
11-
12-
get pageSetting() {
13-
return this.getValue();
14-
}
15-
16-
selectNameFilter$ = this.select((state) => state.filters.nameFilter);
17-
selectPageSize$ = this.select((state) => state.pagination.pageSize);
18-
selectPagination$ = this.select(
19-
(state) =>
20-
new PaginationModel(
21-
state.total,
22-
state.pagination.pageSize,
23-
state.pagination.offset
24-
)
25-
);
26-
27-
selectSort$ = this.select(
28-
(state) => new SortModel(state.pagination.sort, state.pagination.isSortDsc)
29-
);
30-
}
1+
// import { Injectable } from '@angular/core';
2+
// import { Query } from '@datorama/akita';
3+
// import { CustomersState, CustomersStore } from './';
4+
// import { PaginationModel, SortModel } from 'src/app/common/models';
5+
//
6+
// @Injectable({ providedIn: 'root' })
7+
// export class CustomersQuery extends Query<CustomersState> {
8+
// constructor(protected store: CustomersStore) {
9+
// super(store);
10+
// }
11+
//
12+
// get pageSetting() {
13+
// return this.getValue();
14+
// }
15+
//
16+
// selectNameFilter$ = this.select((state) => state.filters.nameFilter);
17+
// selectPageSize$ = this.select((state) => state.pagination.pageSize);
18+
// selectPagination$ = this.select(
19+
// (state) =>
20+
// new PaginationModel(
21+
// state.total,
22+
// state.pagination.pageSize,
23+
// state.pagination.offset
24+
// )
25+
// );
26+
//
27+
// selectSort$ = this.select(
28+
// (state) => new SortModel(state.pagination.sort, state.pagination.isSortDsc)
29+
// );
30+
// }
Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1-
import { Injectable } from '@angular/core';
2-
import { persistState, Store, StoreConfig } from '@datorama/akita';
3-
import {
4-
FiltrationStateModel,
5-
CommonPaginationState,
6-
} from 'src/app/common/models';
7-
8-
export interface CustomersState {
9-
pagination: CommonPaginationState;
10-
filters: FiltrationStateModel;
11-
total: number;
12-
}
13-
14-
export function createInitialState(): CustomersState {
15-
return <CustomersState>{
16-
pagination: {
17-
pageSize: 10,
18-
sort: 'Id',
19-
isSortDsc: false,
20-
offset: 0,
21-
},
22-
filters: {
23-
nameFilter: '',
24-
},
25-
total: 0,
26-
};
27-
}
28-
29-
const customersPersistStorage = persistState({
30-
include: ['customers'],
31-
key: 'customersPn',
32-
preStorageUpdate(storeName, state) {
33-
return {
34-
pagination: state.pagination,
35-
filters: state.filters,
36-
};
37-
},
38-
});
39-
40-
@Injectable({ providedIn: 'root' })
41-
@StoreConfig({ name: 'customers', resettable: true })
42-
export class CustomersStore extends Store<CustomersState> {
43-
constructor() {
44-
super(createInitialState());
45-
}
46-
}
47-
48-
export const customersPersistProvider = {
49-
provide: 'persistStorage',
50-
useValue: customersPersistStorage,
51-
multi: true,
52-
};
1+
// import { Injectable } from '@angular/core';
2+
// import { persistState, Store, StoreConfig } from '@datorama/akita';
3+
// import {
4+
// FiltrationStateModel,
5+
// CommonPaginationState,
6+
// } from 'src/app/common/models';
7+
//
8+
// export interface CustomersState {
9+
// pagination: CommonPaginationState;
10+
// filters: FiltrationStateModel;
11+
// total: number;
12+
// }
13+
//
14+
// export function createInitialState(): CustomersState {
15+
// return <CustomersState>{
16+
// pagination: {
17+
// pageSize: 10,
18+
// sort: 'Id',
19+
// isSortDsc: false,
20+
// offset: 0,
21+
// },
22+
// filters: {
23+
// nameFilter: '',
24+
// },
25+
// total: 0,
26+
// };
27+
// }
28+
//
29+
// const customersPersistStorage = persistState({
30+
// include: ['customers'],
31+
// key: 'customersPn',
32+
// preStorageUpdate(storeName, state) {
33+
// return {
34+
// pagination: state.pagination,
35+
// filters: state.filters,
36+
// };
37+
// },
38+
// });
39+
//
40+
// @Injectable({ providedIn: 'root' })
41+
// @StoreConfig({ name: 'customers', resettable: true })
42+
// export class CustomersStore extends Store<CustomersState> {
43+
// constructor() {
44+
// super(createInitialState());
45+
// }
46+
// }
47+
//
48+
// export const customersPersistProvider = {
49+
// provide: 'persistStorage',
50+
// useValue: customersPersistStorage,
51+
// multi: true,
52+
// };
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './customers.query';
2-
export * from './customers.store';
1+
// export * from './customers.query';
2+
// export * from './customers.store';
33
export * from './customers-state.service';

0 commit comments

Comments
 (0)