region.schools.select.ts 7.09 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
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
10
import { IAppState } from '../../store/store';
Open Source Developer's avatar
Open Source Developer committed
11
import {RemoveSpaces} from '../../pipes/removespaces';
12

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: 'course-fields-select',
    template: `
25
    <div class = "loading" *ngIf="showLoader$ | async">
Open Source Developer's avatar
Open Source Developer committed
26
    </div>
27 28
<!--     <div class="row equal">
      <div class="col-md-12"> -->
29
       <form [formGroup]="formGroup">
30
        <div formArrayName="formArray">
31 32 33
            <ul class="list-group main-view">
            <div *ngFor="let region$ of regions$ | async; let i=index; let isOdd=odd; let isEven=even"  >
                <li class="list-group-item isclickable" (click)="setActiveRegion(i)" [class.oddout]="isOdd" [class.evenout]="isEven" [class.selectedout]="regionActive === i">
34 35
                    <h5>{{region$.region_name}}</h5>
                </li>
36

37
                <div *ngFor="let epal$ of region$.epals; let j=index; let isOdd2=odd; let isEven2=even" [class.oddin]="isOdd2" [class.evenin]="isEven2" [hidden]="i !== regionActive">
38

39
                        <div class="row">
40
                            <div class="col-md-2 col-md-offset-1">
41
                                <input #cb type="checkbox" formControlName="{{ epal$.globalIndex }}"
42
                                (change)="saveSelected(cb,j)"
43 44
                                [hidden] = "numSelected === 3 && cb.checked === false"
                                >
45
                             </div>
46
                            <div class="col-md-8  col-md-offset-1 isclickable">
Open Source Developer's avatar
Open Source Developer committed
47
                                {{epal$.epal_name | removeSpaces}}
48 49
                            </div>
                        </div>
50

51
                </div>
52

53 54 55
            </div>
            </ul>
        </div>
56 57 58 59 60 61 62
        <div class="row" style="margin-top: 20px;" *ngIf="!(showLoader$ | async)">
        <div class="col-md-6">
            <button [hidden] = "objLoaderStatus == true" type="button" class="btn-primary btn-lg pull-left" (click)="navigateBack()" >
          <i class="fa fa-backward"></i>
            </button>
        </div>
        <div class="col-md-6">
Open Source Developer's avatar
Open Source Developer committed
63 64
            <button [hidden] = "objLoaderStatus == true" type="button" class="btn-primary btn-lg pull-right" (click)="navigateToApplication()" [disabled] = "numSelected === 0"  >
          <i class="fa fa-forward"></i>
65 66 67 68
            </button>
        </div>
        </div>
    </form>
69

70
<!--   </div>
71

72
  </div>  -->
73 74 75 76
  `
})
@Injectable() export default class RegionSchoolsSelect implements OnInit {
    private regions$: Observable<IRegions>;
77
    private sectors$: Observable<ISectors>;
78
    private showLoader$: Observable<boolean>;
79 80
    private formGroup: FormGroup;
    private rss = new FormArray([]);
81
    private classActive = "-1";
82
    private regionActive = <number>-1;
83
    private courseActive = -1;
84
    private numSelected = <number>0;
Open Source Developer's avatar
Open Source Developer committed
85

86
    //private schoolArray: Array<boolean> = new Array();
87

88 89 90

    constructor(private fb: FormBuilder,
                private _rsa: RegionSchoolsActions,
91
                private _rsb: SectorCoursesActions,
92
                private _ngRedux: NgRedux<IAppState>,
93
                private router: Router
94

95 96 97
            ) {
        this.formGroup = this.fb.group({
            formArray: this.rss
98

99
        });
Open Source Developer's avatar
Open Source Developer committed
100

101 102 103
    };

    ngOnInit() {
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        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);
122
        this.regions$ = this._ngRedux.select(state => {
123
            let numsel = 0;
124 125 126
            state.regions.reduce((prevRegion, region) =>{
                region.epals.reduce((prevEpal, epal) =>{
                    this.rss.push( new FormControl(epal.selected, []));
127
                    if (epal.selected === true) {
128
                      numsel++;
129
                    }
130 131 132 133
                    return epal;
                }, {});
                return region;
            }, {});
134
            this.numSelected = numsel;
135 136
            return state.regions;
        });
137 138
        this.showLoader$ = this.regions$.map(regions => regions.size === 0);
    }
139

140 141 142 143 144 145 146 147 148 149 150
    navigateBack() {
//        this.router.navigate(['/epal-class-select']);
        if (this.classActive === "Α' Λυκείου")  {
            this.router.navigate(['/epal-class-select']);
        }
        else if (this.classActive === "Β' Λυκείου") {
            this.router.navigate(['/sector-fields-select']);
        }
        else if (this.classActive === "Γ' Λυκείου")  {
            this.router.navigate(['/sectorcourses-fields-select']);
        }
151 152 153
    }

    setActiveRegion(ind) {
154 155 156
      if (ind === this.regionActive)
        ind = -1;
      this.regionActive = ind;
157 158
    }

159
    saveSelected(cb,j) {
160
        this._rsa.saveRegionSchoolsSelected(this.formGroup.value.formArray);
161 162 163
    }

    navigateToApplication() {
164 165 166 167
      //if (this.numSelected > 1)
        this.router.navigate(['/schools-order-select']);
      //else
      //  this.router.navigate(['/student-application-form-main']);
168
    }
169 170 171 172 173 174 175 176 177

    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"];
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        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";
198 199
    }

200
}