course-fields-select.ts 2.1 KB
Newer Older
1 2 3 4
import {Component, OnInit} from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import {Injectable} from "@angular/core";
5 6 7 8
import {CourseFieldsActions} from '../../actions/coursefields.actions';
import { DevToolsExtension, NgRedux, select } from 'ng2-redux';
import { ICourseField, ICourseFields } from '../../store/coursefields/coursefields.types';
import { IAppState } from '../../store/store';
9 10 11 12 13 14 15 16 17 18 19 20 21 22

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'course-fields-select',
    template: `
     <form [formGroup]="formGroup">
        <div formArrayName="formArray">
23 24
            <div *ngIf="courseFields$.length === 0" class="loading">Loading&#8230;</div>
            <div *ngFor="let courseField$ of courseFields$ | async; let i=index">
25 26 27 28 29
            <div class="row">
            <div class="col-md-2 pull-right">
                <input type="checkbox" formControlName="{{i}}">
            </div>
            <div class="col-md-10 pull-left">
30
                {{courseField$.name}}
31 32 33 34 35 36 37 38 39 40
            </div>
            </div>
            </div>
        </div>
<!--        <button (click)="addInput()">Add Input</button>  -->
    </form>
    <pre>{{formGroup.value | json}}</pre>
  `
})
@Injectable() export default class CourseFieldsSelect implements OnInit {
41 42
    private courseFields$: Observable<ICourseFields>;

43 44 45
    public formGroup: FormGroup;
    public cfs = new FormArray([]);

46
    constructor(private http: Http, private fb: FormBuilder, private _cfa: CourseFieldsActions, private _ngRedux: NgRedux<IAppState>) {
47 48 49 50 51 52
        this.formGroup = this.fb.group({
            formArray: this.cfs
        });
    };

    ngOnInit() {
53
        this._cfa.getCourseFields({});
54

55 56 57
        this.courseFields$ = this._ngRedux.select(state => {
            console.log("in select");
            for (let courseField in state.courseFields) {
58
                this.cfs.push(new FormControl('', []));
59 60 61 62
            }
            return state.courseFields;
        })
    }
63
}