Repository navigation
Expand file tree
/
Copy pathcoffee-list.store.ts
More file actions
167 lines (149 loc) · 5.22 KB
/
Copy pathcoffee-list.store.ts
File metadata and controls
167 lines (149 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import {Injectable, OnDestroy} from '@angular/core';
import {Store} from 'rxjs-observable-store';
import {Subject, Observable} from 'rxjs';
import {takeUntil, tap, switchMap, retry} from 'rxjs/operators';
import {CoffeeListStoreState} from './coffee-list.store.state';
import {CoffeeListEndpoint} from './coffee-list.endpoint';
import {ModalComponent} from '../../../shared/components/modal/modal.component';
import {Candidate} from '../types/candidate';
import * as candidateHelpers from '../helpers/candidate.helpers';
import {UserStore} from '../../../core/user/services/user.store';
import {User} from '../../../core/user/types/user';
import {UserAction} from '../coffee-list.constants';
import * as endpointHelpers from '../../../shared/helpers/endpoint.helpers';
import {StoreRequestStateUpdater} from '../../../shared/types/store-request-state-updater';
import {CustomRequestStateUpdater} from '../../../shared/types/custom-request-state-updater';
import {Sort} from '../../../shared/types/sort';
@Injectable()
export class CoffeeListStore extends Store<CoffeeListStoreState>
implements OnDestroy {
private ngUnsubscribe$: Subject<undefined> = new Subject();
private reloadCandidates$: Subject<undefined> = new Subject();
private detailsModal: ModalComponent;
private storeRequestStateUpdater: StoreRequestStateUpdater;
constructor(
private endpoint: CoffeeListEndpoint,
private userStore: UserStore
) {
super(new CoffeeListStoreState());
}
init(): void {
this.initReloadCandidates$();
this.subscribeToUserUpdates();
this.storeRequestStateUpdater = endpointHelpers.getStoreRequestStateUpdater(
this
);
}
ngOnDestroy(): void {
this.ngUnsubscribe$.next();
this.ngUnsubscribe$.complete();
}
reloadCandidates(): void {
this.reloadCandidates$.next();
}
setDetailsModal(detailsModal: ModalComponent): void {
this.detailsModal = detailsModal;
}
openDetailsModal(candidate: Candidate): void {
this.setDetailsModalState(candidate);
this.detailsModal.open();
}
closeDetailsModal(): void {
this.setDetailsModalState(null);
this.detailsModal.close();
}
setDetailsModalState(candidate: Candidate): void {
this.setState({
...this.state,
detailsModal: {
candidate: candidate,
},
});
}
submitUserAction(candidate: Candidate, action: UserAction): void {
let request$: Observable<null>;
const requestStateUpdater = this.getUpdateCandidateRequestStateUpdater(
candidate
);
if (action === UserAction.AddVote) {
request$ = this.endpoint.addVote(candidate, requestStateUpdater);
}
if (action === UserAction.RemoveVote) {
request$ = this.endpoint.removeVote(candidate, requestStateUpdater);
}
request$
.pipe(
tap(() => {
this.reloadCandidates();
})
)
.subscribe();
}
sortCandidates(sort: Sort): void {
this.setState({
...this.state,
candidateList: {
...this.state.candidateList,
sort: sort,
},
});
this.reloadCandidates();
}
private initReloadCandidates$(): void {
this.reloadCandidates$
.pipe(
switchMap(() => {
return this.endpoint.listCandidates(
this.state.candidateList.sort,
this.storeRequestStateUpdater
);
}),
tap(candidates => {
this.updateCandidatesState(candidates, this.userStore.user);
}),
retry(),
takeUntil(this.ngUnsubscribe$)
)
.subscribe();
}
private subscribeToUserUpdates(): void {
this.userStore.user$
.pipe(takeUntil(this.ngUnsubscribe$))
.subscribe(user => {
this.updateCandidatesState(
this.state.candidateList.candidates,
user
);
});
}
private updateCandidatesState(candidates: Candidate[], user: User): void {
this.setState({
...this.state,
candidateList: {
...this.state.candidateList,
candidates: candidateHelpers.getCandidatesInStoreFormat(
candidates,
user
),
},
});
}
private getUpdateCandidateRequestStateUpdater(
candidate: Candidate
): CustomRequestStateUpdater {
return requestState => {
this.setState({
...this.state,
candidateList: {
...this.state.candidateList,
candidates: this.state.candidateList.candidates.map(c => {
if (c.id === candidate.id) {
return {...c, updateRequest: requestState};
}
return c;
}),
},
});
};
}
}