region.schools.select.ts 6.4 KB
Newer Older
1 2 3 4 5 6 7
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import { Injectable } from "@angular/core";
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
import { NgRedux, select } from 'ng2-redux';
import { IRegions } from '../../store/regionschools/regionschools.types';
8 9 10 11

import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';

12 13
import { IAppState } from '../../store/store';

14

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

@Component({
    selector: 'course-fields-select',
    template: `
26
     <div class="row equal">
Open Source Developer's avatar
UI  
Open Source Developer committed
27
      <div class="col-md-12">
28
       <form [formGroup]="formGroup">
29 30 31 32 33 34 35 36 37 38 39
        <div formArrayName="formArray">
            <ul class="list-group">
            <div *ngFor="let region$ of regions$ | async; let i=index">
                <li class="list-group-item" (click)="setActiveRegion(i)" [style.background-color]="toggleBackgroundColor(i)">
                    <h5>{{region$.region_name}}</h5>
                </li>

                <div *ngFor="let epal$ of region$.epals; let j=index;" [hidden]="i !== regionActive">
                    <li class="list-group-item" >
                        <div class="row">
                            <div class="col-md-2">
40
                                <input #cb type="checkbox" formControlName="{{ epal$.globalIndex }}"
41
                                (change)="saveSelected(cb,j)"
42 43
                                [hidden] = "numSelected === 3 && cb.checked === false"
                                >
44 45 46 47 48 49 50 51 52 53 54 55
                            </div>
                            <div class="col-md-10">
                                {{epal$.epal_name}}
                            </div>
                        </div>
                    </li>
                </div>
            </div>
            </ul>
        </div>
        <div class="row">
        <div class="col-md-2 col-md-offset-5">
56
            <button type="button" class="btn-primary btn-lg pull-center" (click)="navigateToApplication()" [disabled] = "numSelected === 0" >
57 58 59 60 61
            Συνέχεια<span class="glyphicon glyphicon-menu-right"></span>
            </button>
        </div>
        </div>
    </form>
62

63 64 65
   </div>

  </div>
66 67 68 69
  `
})
@Injectable() export default class RegionSchoolsSelect implements OnInit {
    private regions$: Observable<IRegions>;
70
    private sectors$: Observable<ISectors>;
71 72
    private formGroup: FormGroup;
    private rss = new FormArray([]);
73
    private classActive = "-1";
74
    private regionActive = <number>-1;
75
    private courseActive = -1;
76
    private numSelected = <number>0;
77
    //private schoolArray: Array<boolean> = new Array();
78

79 80 81

    constructor(private fb: FormBuilder,
                private _rsa: RegionSchoolsActions,
82
                private _rsb: SectorCoursesActions,
83 84 85 86 87 88 89 90 91
                private _ngRedux: NgRedux<IAppState>,
                private router: Router
            ) {
        this.formGroup = this.fb.group({
            formArray: this.rss
        });
    };

    ngOnInit() {
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        this.classActive = this.classActive = this.getClassActive();

        let class_id = -1;
        if (this.classActive === "Α' Λυκείου")  {
          //είναι Α' Λυκείου, οπότε courseActive = "-1" (είναι ήδη ορισμένο με αυτή την τιμή από την αρχικοποίηση)
          class_id = 1;
        }
        else if (this.classActive === "Β' Λυκείου") {
          class_id = 2;
          this.courseActive = this.getSectorActive();
        }
        else if (this.classActive === "Γ' Λυκείου")  {
          class_id = 3;
          this.courseActive = this.getCourseActive();
        }

        this._rsa.getRegionSchools(class_id,this.courseActive, false);
109 110

        this.regions$ = this._ngRedux.select(state => {
111
            let numsel = 0;
112 113 114
            state.regions.reduce((prevRegion, region) =>{
                region.epals.reduce((prevEpal, epal) =>{
                    this.rss.push( new FormControl(epal.selected, []));
115
                    if (epal.selected === true) {
116
                      numsel++;
117
                    }
118 119 120 121
                    return epal;
                }, {});
                return region;
            }, {});
122
            this.numSelected = numsel;
123 124 125 126 127 128
            return state.regions;
        });

    }

    setActiveRegion(ind) {
129 130 131
      if (ind === this.regionActive)
        ind = -1;
      this.regionActive = ind;
132 133 134
    }

    toggleBackgroundColor(ind) {
Open Source Developer's avatar
UI  
Open Source Developer committed
135
        return ((this.regionActive === ind) ? "#fd9665" : "white");
136 137
    }

138
    saveSelected(cb,j) {
139
        this._rsa.saveRegionSchoolsSelected(this.formGroup.value.formArray);
140 141 142 143 144
        //σε κάθε νέο check, αρχικοποίησε τη σειρά προτιμήσεων (σειρά προτίμησης:0)
        let schoolArrayOrders: Array<number> = new Array();
        for (let i=0; i < this.formGroup.value.formArray.length; i++)
          schoolArrayOrders.push(0);
        this._rsa.saveRegionSchoolsOrder(schoolArrayOrders);
145 146 147
    }

    navigateToApplication() {
148 149 150 151
      //if (this.numSelected > 1)
        this.router.navigate(['/schools-order-select']);
      //else
      //  this.router.navigate(['/student-application-form-main']);
152
    }
153 154 155 156 157 158 159 160 161

    getCourseActive() {
        const { sectors } = this._ngRedux.getState();
        let l,m;
        for ( l=0; l<sectors.size; l++)
          if (sectors["_tail"]["array"][l]["sector_selected"] === true)
            for ( m=0; m < sectors["_tail"]["array"][l]["courses"].length; m++)
              if (sectors["_tail"]["array"][l]["courses"][m]["selected"] === true)
                 return sectors["_tail"]["array"][l]["courses"][m]["course_id"];
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        return "-1";
    }

    getClassActive()  {
      const { epalclasses } = this._ngRedux.getState();
      let l,m;
      if (epalclasses.size !== 0 && epalclasses["_tail"]["array"][0]["name"].length !==0 )
         return epalclasses["_tail"]["array"][0]["name"];
      return "-1";
    }

    getSectorActive() {
      const { sectorFields } = this._ngRedux.getState();
      let l,m;
      for ( l=0; l<sectorFields.size; l++)  {
        if (sectorFields["_tail"]["array"][l]["selected"] === true) {
            return sectorFields["_tail"]["array"][l]["id"];
          }
      }
      return "-1";
182 183
    }

184
}