sector.courses.select.ts 6.39 KB
Newer Older
1
import { Component, OnInit, OnDestroy } from '@angular/core';
2
import { Router } from '@angular/router';
3
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
4 5 6
import { Injectable } from "@angular/core";
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
7
import { SECTOR_COURSES_INITIAL_STATE } from '../../store/sectorcourses/sectorcourses.initial-state';
8
import { NgRedux, select } from 'ng2-redux';
9
import { IAppState } from '../../store/store';
Open Source Developer's avatar
UI  
Open Source Developer committed
10
import {RemoveSpaces} from '../../pipes/removespaces';
11

12
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
13

14 15 16 17 18 19 20 21 22 23 24
import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'sectorcourses-fields-select',
    template: `
Open Source Developer's avatar
commit  
Open Source Developer committed
25
    <div class="row">
26
             <breadcrumbs></breadcrumbs>
Open Source Developer's avatar
commit  
Open Source Developer committed
27
    </div>
28
    <div class = "loading" *ngIf="(sectors$ | async).size === 0">
29
   </div>
Open Source Developer's avatar
trans  
Open Source Developer committed
30
       <h4> Επιλογή Ειδικότητας</h4>
31 32
      <form [formGroup]="formGroup">
        <div formArrayName="formArray">
Open Source Developer's avatar
trans  
Open Source Developer committed
33
        <p style="margin-top: 20px; line-height: 2em;"> Παρακαλώ επιλέξτε την ειδικότητα που θα παρακολουθήσει ο μαθητής στην Επαγγελματική Εκπαίδευση με βάση τον τομέα που έχει ολοκληρώσει και στη συνέχεια επιλέξτε <i>Συνέχεια</i>.</p>
34
            <ul class="list-group">
35 36
            <div *ngFor="let sector$ of sectors$ | async; let i=index; let isOdd=odd; let isEven=even">
                <li class="list-group-item isclickable" (click)="setActiveSector(i)"  [class.oddout]="isOdd" [class.evenout]="isEven" [class.selectedout]="sectorActive === i">
37 38
                    <h5>{{sector$.sector_name}}</h5>
                </li>
39
                <div *ngFor="let course$ of sector$.courses; let j=index; let isOdd2=odd; let isEven2=even" [class.oddin]="isOdd2" [class.evenin]="isEven2" [hidden]="i !== sectorActive">
Open Source Developer's avatar
Open Source Developer committed
40
                          <div class="row">
41
                           <div class="col-md-2 col-md-offset-1">
42 43
                                <input #cb type="checkbox" formControlName="{{ course$.globalIndex }}"
                                (change)="updateCheckedOptions(course$.globalIndex, cb)"
44 45
                                [checked] = " course$.globalIndex === idx "
                                >
46
                            </div>
47
                            <div class="col-md-8  col-md-offset-1 isclickable">
Open Source Developer's avatar
UI  
Open Source Developer committed
48
                                {{course$.course_name | removeSpaces}}
49
                            </div>
Open Source Developer's avatar
Open Source Developer committed
50 51
                            </div>
                    </div>
52 53 54 55
              </div>
            </ul>
        </div>

56
        <div class="row" style="margin-top: 20px; margin-bottom: 20px;" *ngIf="(sectors$ | async).size > 0">
57
        <div class="col-md-6">
58
            <button type="button" class="btn-primary btn-lg pull-left" (click)="router.navigate(['/epal-class-select']);" >
59 60 61 62
          <i class="fa fa-backward"></i>
            </button>
        </div>
        <div class="col-md-6">
63 64
            <button type="button" class="btn-primary btn-lg pull-right isclickable" style="width: 9em;" (click)="navigateToSchools()" (click)="navigateToSchools()" [disabled]="idx === -1" >
                <span style="font-size: 0.9em; font-weight: bold;">Συνέχεια&nbsp;&nbsp;&nbsp;</span><i class="fa fa-forward"></i>
65 66 67 68 69 70
            </button>
        </div>
        </div>
     </form>
  `
})
71 72 73
@Injectable() export default class SectorCoursesSelect implements OnInit, OnDestroy {
    private sectors$: BehaviorSubject<ISectors>;
    private sectorsSub: Subscription;
74 75 76 77
    private formGroup: FormGroup;
    private rss = new FormArray([]);
    private sectorActive = <number>-1;
    private idx = <number>-1;
78
    private sectorsList: Array<boolean> = new Array();
79 80

    constructor(private fb: FormBuilder,
81
                private _sca: SectorCoursesActions,
82
                private _rsa: RegionSchoolsActions,
83 84 85
                private _ngRedux: NgRedux<IAppState>,
                private router: Router
            ) {
86 87

        this.sectors$ = new BehaviorSubject(SECTOR_COURSES_INITIAL_STATE);
88 89 90 91 92 93
        this.formGroup = this.fb.group({
            formArray: this.rss
        });
    };

    ngOnInit() {
94
        this._sca.getSectorCourses(false);
95
        let ids = 0;
96
        this.sectorsSub = this._ngRedux.select(state => {
97
            state.sectors.reduce((prevSector, sector) =>{
98 99 100
                this.sectorsList[ids] = sector.sector_selected;
                ids++;
                //In case we want to preserve last checked option when we revisit the form
101 102
                if (sector.sector_selected === true)
                    this.sectorActive = ids-1;
103 104
                sector.courses.reduce((prevCourse, course) =>{
                    this.rss.push( new FormControl(course.selected, []));
105 106 107
                    //this.retrieveCheck();
                    if (course.selected === true) {
                      //In case we want to preserve last checked option when we revisit the form
108
                      this.idx = course.globalIndex;
109
                    }
110 111 112 113
                    return course;
                }, {});
                return sector;
            }, {});
114
            ids = 0;
115
            return state.sectors;
116 117 118
        }).subscribe(this.sectors$);

    }
119

120 121
    ngOnDestroy() {
        if (this.sectorsSub) this.sectorsSub.unsubscribe();
122
        this.sectors$.unsubscribe();
123 124 125
    }

    setActiveSector(ind) {
126 127 128
      if (ind === this.sectorActive)
        ind = -1;
      this.sectorActive = ind;
129 130 131
    }

    saveSelected() {
132
        this._sca.saveSectorCoursesSelected(this.formGroup.value.formArray, this.sectorsList);
133

134
        this._rsa.initRegionSchools();
135 136 137
    }

    navigateToSchools() {
138

139 140 141 142 143 144 145 146 147 148
        this.router.navigate(['/region-schools-select']);
    }

    updateCheckedOptions(globalIndex, cb){
      this.idx = globalIndex;
      for (let i = 0; i < this.formGroup.value.formArray.length; i++)
        this.formGroup.value.formArray[i] = false;
      this.formGroup.value.formArray[globalIndex] = cb.checked;
      if (cb.checked === false)
        this.idx = -1;
149 150 151 152 153 154

      for (let i = 0; i < this.sectorsList.length; i++)
          this.sectorsList[i] = false;
      this.sectorsList[this.sectorActive] = true;

      this.saveSelected();
155
  }
156 157

}