-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
project.service.ts
87 lines (78 loc) · 2.15 KB
/
project.service.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
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { arrayRemove, arrayUpsert, setLoading } from '@datorama/akita';
import { JComment } from '@trungk18/interface/comment';
import { JIssue } from '@trungk18/interface/issue';
import { JProject } from '@trungk18/interface/project';
import { DateUtil } from '@trungk18/project/utils/date';
import { of } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
import { ProjectStore } from './project.store';
@Injectable({
providedIn: 'root'
})
export class ProjectService {
baseUrl: string;
constructor(private _http: HttpClient, private _store: ProjectStore) {
this.baseUrl = environment.apiUrl;
}
setLoading(isLoading: boolean) {
this._store.setLoading(isLoading);
}
getProject() {
this._http
.get<JProject>(`${this.baseUrl}/project.json`)
.pipe(
setLoading(this._store),
tap((project) => {
this._store.update((state) => ({
...state,
...project
}));
}),
catchError((error) => {
this._store.setError(error);
return of(error);
})
)
.subscribe();
}
updateProject(project: Partial<JProject>) {
this._store.update((state) => ({
...state,
...project
}));
}
updateIssue(issue: JIssue) {
issue.updatedAt = DateUtil.getNow();
this._store.update((state) => {
const issues = arrayUpsert(state.issues, issue.id, issue);
return {
...state,
issues
};
});
}
deleteIssue(issueId: string) {
this._store.update((state) => {
const issues = arrayRemove(state.issues, issueId);
return {
...state,
issues
};
});
}
updateIssueComment(issueId: string, comment: JComment) {
const allIssues = this._store.getValue().issues;
const issue = allIssues.find((x) => x.id === issueId);
if (!issue) {
return;
}
const comments = arrayUpsert(issue.comments ?? [], comment.id, comment);
this.updateIssue({
...issue,
comments
});
}
}