-
-
Notifications
You must be signed in to change notification settings - Fork 746
/
tag.service.ts
89 lines (73 loc) · 2.14 KB
/
tag.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
88
89
import {Tag} from './tag.model';
import {AddTag, DeleteTag, LoadTagState, UpdateTag} from './store/tag.actions';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {select, Store} from '@ngrx/store';
import {PersistenceService} from '../../core/persistence/persistence.service';
import {take} from 'rxjs/operators';
import {
initialTagState,
selectAllTags,
selectTagById,
selectTagByName,
selectTagsByIds,
TagState
} from './store/tag.reducer';
import shortid from 'shortid';
@Injectable({
providedIn: 'root'
})
export class TagService {
public tags$: Observable<Tag[]> = this._store$.pipe(select(selectAllTags));
constructor(
private _store$: Store<TagState>,
private _persistenceService: PersistenceService
) {
}
getById$(id: string): Observable<Tag> {
return this._store$.pipe(select(selectTagById, {id}), take(1));
}
getByIds$(ids: string[]): Observable<Tag[]> {
return this._store$.pipe(select(selectTagsByIds, {ids}));
}
getByName$(name: string): Observable<Tag> {
return this._store$.pipe(select(selectTagByName, {name}));
}
async load() {
const lsTagState = await this._persistenceService.taskTag.loadState() || initialTagState;
this.loadState(lsTagState || initialTagState);
}
public loadState(state: TagState) {
Object.keys(state.entities).forEach((k, i) => {
if (!k) {
delete state.entities[i];
state.ids.splice(state.ids.indexOf(k as string & number), 1);
}
});
this._store$.dispatch(new LoadTagState({state}));
}
public addTag(tag: Partial<Tag>): string {
if (!tag.name) {
console.error('Can\'t add an empty tag!');
return;
}
const id = shortid();
this._store$.dispatch(new AddTag({
tag: {
id,
name: tag.name,
created: Date.now(),
modified: Date.now(),
color: tag.color || '#FFDAB9',
...tag,
}
}));
return id;
}
public removeTag(id: string) {
this._store$.dispatch(new DeleteTag({id}));
}
public updateColor(id: string, color: string) {
this._store$.dispatch(new UpdateTag({id, changes: {color}}));
}
}