-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcoffee-list.endpoint.ts
115 lines (109 loc) · 4.32 KB
/
coffee-list.endpoint.ts
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
import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {Observable, throwError} from 'rxjs';
import {map, catchError, delay} from 'rxjs/operators';
import {ApiResponse} from '../../../shared/types/api-response';
import {COFFEE_LIST_CONFIG} from '../coffee-list.config';
import {Candidate} from '../types/candidate';
import * as endpointHelpers from '../../../shared/helpers/endpoint.helpers';
import * as sortHelpers from '../../../shared/helpers/sort.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';
import {SortOrder} from '../../../app.constants';
@Injectable()
export class CoffeeListEndpoint {
constructor(private http: HttpClient) {}
listCandidates(
sort: Sort,
requestStateUpdater: StoreRequestStateUpdater
): Observable<Candidate[]> {
const request = COFFEE_LIST_CONFIG.requests.listCandidates;
const options = {
params: {
...sortHelpers.convertSortToRequestParams(sort),
},
};
requestStateUpdater(request.name, {inProgress: true});
return this.http
.get<ApiResponse<Candidate[]>>(request.url, options)
.pipe(
delay(2000), // Simulate request delay
map(response => {
requestStateUpdater(request.name, {inProgress: false});
// Simulate sorting on server
const candidates = response.data.sort(
(c1: Candidate, c2: Candidate): number => {
let field1 = c1[sort.field];
let field2 = c2[sort.field];
if (Array.isArray(field1)) {
field1 = field1.length;
field2 = field2.length;
}
if (field1 < field2) {
return sort.order === SortOrder.Asc ? -1 : 1;
}
if (field1 > field2) {
return sort.order === SortOrder.Asc ? 1 : -1;
}
return 0;
}
);
return candidates;
}),
catchError((error: HttpErrorResponse) => {
requestStateUpdater(request.name, {
inProgress: false,
error: true,
});
return throwError(error);
})
);
}
addVote(
candidate: Candidate,
requestStateUpdater: CustomRequestStateUpdater
): Observable<null> {
const url = endpointHelpers.getUrlWithParams(
COFFEE_LIST_CONFIG.requests.addVote.url,
{id: candidate.id}
);
requestStateUpdater({inProgress: true});
return this.http.post<ApiResponse<null>>(url, null).pipe(
map(response => {
requestStateUpdater({inProgress: false});
return response.data;
}),
catchError((error: HttpErrorResponse) => {
requestStateUpdater({
inProgress: false,
error: true,
});
return throwError(error);
})
);
}
removeVote(
candidate: Candidate,
requestStateUpdater: CustomRequestStateUpdater
): Observable<null> {
const url = endpointHelpers.getUrlWithParams(
COFFEE_LIST_CONFIG.requests.removeVote.url,
{id: candidate.id}
);
requestStateUpdater({inProgress: true});
return this.http.delete<ApiResponse<null>>(url).pipe(
map(response => {
requestStateUpdater({inProgress: false});
return response.data;
}),
catchError((error: HttpErrorResponse) => {
requestStateUpdater({
inProgress: false,
error: true,
});
return throwError(error);
})
);
}
}