sector.fields.select.ts 6.66 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 7 8
import { Injectable } from "@angular/core";
import { SectorFieldsActions } from '../../actions/sectorfields.actions';
import { NgRedux, select } from 'ng2-redux';
import { ISectorFields } from '../../store/sectorfields/sectorfields.types';
import { IAppState } from '../../store/store';
9
import { SECTOR_FIELDS_INITIAL_STATE } from '../../store/sectorfields/sectorfields.initial-state';
10

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

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

@Component({
    selector: 'sector-fields-select',
    template: `
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    <div id="sectorFieldsNotice" (onHidden)="onHidden()" class="modal" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static" data-keyboard="false">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header {{modalHeader | async}}">
              <h3 class="modal-title pull-left"><i class="fa fa-check-square-o"></i>&nbsp;&nbsp;{{ modalTitle | async }}</h3>
            <button type="button" class="close pull-right" aria-label="Close" (click)="hideModal()">
              <span aria-hidden="true"><i class="fa fa-times"></i></span>
            </button>
          </div>
          <div class="modal-body">
              <p>{{ modalText | async }}</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal" (click)="hideModal()">Κλείσιμο</button>
          </div>
        </div>
      </div>
    </div>
Open Source Developer's avatar
trans  
Open Source Developer committed
42
    <div class="row">
43
             <breadcrumbs></breadcrumbs>
Open Source Developer's avatar
trans  
Open Source Developer committed
44
    </div>
Open Source Developer's avatar
trans  
Open Source Developer committed
45
    <h4> Επιλογή Τομέα </h4>
46
     <div class = "loading" *ngIf="(sectorFields$ | async).size === 0">
47
    </div>
48
       <form [formGroup]="formGroup">
49
       <p style="margin-top: 20px; line-height: 2em;"> Παρακαλώ επιλέξτε τον τομέα στον οποίο θα φοιτήσει ο μαθητής το νέο σχολικό έτος στην επαγγελματική εκπαίδευση. Έπειτα επιλέξτε <i>Συνέχεια</i>.</p>
50
        <div formArrayName="formArray">
51 52 53
            <ul class="list-group main-view">
            <div *ngFor="let sectorField$ of sectorFields$ | async; let i=index; let isOdd=odd; let isEven=even">
                <li class="list-group-item  isclickable" (click)="setActiveSectorAndSave(i)" [class.oddout]="isOdd" [class.evenout]="isEven" [class.selectedout]="sectorActive === i">
54 55 56
                    <h5>{{sectorField$.name}}</h5>
                </li>
            </div>
57
            </ul>
58

59
        </div>
60

61
        <div class="row" style="margin-top: 20px; margin-bottom: 20px;" *ngIf="(sectorFields$ | async).size > 0">
62
        <div class="col-md-6">
63
            <button type="button" class="btn-primary btn-lg pull-left" (click)="router.navigate(['/epal-class-select']);" >
64 65 66 67
          <i class="fa fa-backward"></i>
            </button>
        </div>
        <div class="col-md-6">
68
            <button type="button" class="btn-primary btn-lg pull-right isclickable" style="width: 9em;" (click)="navigateToSchools()" >
69
                <span style="font-size: 0.9em; font-weight: bold;">Συνέχεια&nbsp;&nbsp;&nbsp;</span><i class="fa fa-forward"></i>
70 71 72
            </button>
        </div>
        </div>
73

74 75 76 77
      </form>
  `

})
78
@Injectable() export default class SectorFieldsSelect implements OnInit, OnDestroy {
79 80
    private sectorFields$: BehaviorSubject<ISectorFields>;
    private sectorFieldsSub: Subscription;
81 82 83
    public formGroup: FormGroup;
    public cfs = new FormArray([]);
    private sectorActive = <number>-1;
84 85 86 87
    private modalTitle: BehaviorSubject<string>;
    private modalText: BehaviorSubject<string>;
    private modalHeader: BehaviorSubject<string>;
    public isModalShown: BehaviorSubject<boolean>;
88 89 90

    constructor(private fb: FormBuilder,
                private _cfa: SectorFieldsActions,
91
                private _rsa: RegionSchoolsActions,
92 93
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
94 95
        this.sectorFields$ = new BehaviorSubject(SECTOR_FIELDS_INITIAL_STATE);

96 97 98
        this.formGroup = this.fb.group({
            formArray: this.cfs
        });
99 100 101 102
        this.modalTitle = new BehaviorSubject("");
        this.modalText = new BehaviorSubject("");
        this.modalHeader = new BehaviorSubject("");
        this.isModalShown = new BehaviorSubject(false);
103 104 105
    };

    ngOnInit() {
106
        (<any>$('#sectorFieldsNotice')).appendTo("body");
107 108
        this._cfa.getSectorFields(false);
        this.sectorFieldsSub = this._ngRedux.select(state => {
109 110
            state.sectorFields.reduce(({}, sectorField) =>{
                this.cfs.push(new FormControl(sectorField.selected, []));
111
                //in case we want to retrieve last check when we return to the form
112

113 114 115
                if (sectorField.selected === true) {
                  this.sectorActive = sectorField.id - 1;
                }
116

117 118 119
                return sectorField;
            }, {});
            return state.sectorFields;
120 121 122
        }).subscribe(this.sectorFields$);

    }
123

124
    ngOnDestroy() {
125
        (<any>$('#sectorFieldsNotice')).remove();
126
        if (this.sectorFieldsSub) this.sectorFieldsSub.unsubscribe();
127
        this.sectorFields$.unsubscribe();
128 129
    }

130 131 132 133 134 135 136 137 138 139 140 141
    public showModal(): void {
        (<any>$('#sectorFieldsNotice')).modal('show');
    }

    public hideModal(): void {
        (<any>$('#sectorFieldsNotice')).modal('hide');
    }

    public onHidden(): void {
        this.isModalShown.next(false);
    }

142
    navigateToSchools() {
143 144 145 146 147 148 149 150 151
        if (this.sectorActive === -1) {
            this.modalTitle.next("Δεν επιλέχθηκε τομέας");
            this.modalText.next("Παρακαλούμε να επιλέξετε πρώτα έναν τομέα");
            this.modalHeader.next("modal-header-danger");
            this.showModal();
        }
        else {
            this.router.navigate(['/region-schools-select']);
        }
152 153
    }

154 155 156 157 158 159 160
    saveSelected() {
      for (let i = 0; i < this.formGroup.value.formArray.length; i++)
        this.formGroup.value.formArray[i] = false;
      if (this.sectorActive != -1)
        this.formGroup.value.formArray[this.sectorActive] = true;

      this._cfa.saveSectorFieldsSelected(this.formGroup.value.formArray);
161

162
      this._rsa.initRegionSchools();
163 164 165 166 167
    }

    setActiveSectorAndSave(ind) {
        if (ind === this.sectorActive)
          ind = -1;
168
        this.sectorActive = ind;
169
        this.saveSelected();
170 171 172
    }

}