-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathdynamic.component.ts
More file actions
58 lines (49 loc) · 1.74 KB
/
dynamic.component.ts
File metadata and controls
58 lines (49 loc) · 1.74 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
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { AddArrayControlAction, FormGroupState, RemoveArrayControlAction } from 'ngrx-forms';
import { Observable } from 'rxjs';
import { map, take } from 'rxjs/operators';
import { CreateGroupElementAction, FormValue, RemoveGroupElementAction, State } from './dynamic.reducer';
@Component({
selector: 'ngf-dynamic',
templateUrl: './dynamic.component.html',
styleUrls: ['./dynamic.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DynamicPageComponent {
formState$: Observable<FormGroupState<FormValue>>;
arrayOptions$: Observable<number[]>;
groupOptions$: Observable<string[]>;
constructor(private store: Store<State>) {
this.formState$ = store.pipe(select(s => s.dynamic.formState));
this.arrayOptions$ = store.pipe(select(s => s.dynamic.array.options));
this.groupOptions$ = store.pipe(select(s => s.dynamic.groupOptions));
}
addGroupOption() {
const name = Math.random().toString(36).substr(2, 3);
this.store.dispatch(new CreateGroupElementAction(name));
}
removeGroupOption(name: string) {
this.store.dispatch(new RemoveGroupElementAction(name));
}
addArrayOption(index: number) {
this.formState$.pipe(
take(1),
map(s => s.controls.array.id),
map(id => new AddArrayControlAction(id, false, index)),
).subscribe(this.store);
}
removeArrayOption(index: number) {
this.formState$.pipe(
take(1),
map(s => s.controls.array.id),
map(id => RemoveArrayControlAction(id, index)),
).subscribe(this.store);
}
trackByIndex(index: number) {
return index;
}
trackById(_: number, id: string) {
return id;
}
}