amka-fill.ts 3.07 KB
Newer Older
Open Source Developer's avatar
Open Source Developer committed
1 2 3 4 5 6 7 8 9
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Rx';
import { Injectable } from "@angular/core";
import { AmkaFillsActions } from '../../actions/amkafill.actions';
import { NgRedux, select } from 'ng2-redux';
import { IAmkaFills } from '../../store/amkafill/amkafills.types';
import { IAppState } from '../../store/store';
import { AmkaCheckService} from '../../services/amkacheck-service';
10
import { VALID_DIGITS_PATTERN } from '../../constants';
11

Open Source Developer's avatar
Open Source Developer committed
12 13 14 15
import {
    FormBuilder,
    FormGroup,
    FormControl,
16 17
    FormArray,
    Validators,
Open Source Developer's avatar
Open Source Developer committed
18 19
} from '@angular/forms';
import {AppSettings} from '../../app.settings';
20

Open Source Developer's avatar
Open Source Developer committed
21 22 23 24

@Component({
    selector: 'amka-fill',
    template: `
25

Open Source Developer's avatar
Open Source Developer committed
26
    <form [formGroup]="formGroup">
27 28

        <div class="form-group">
Open Source Developer's avatar
Open Source Developer committed
29
              <label for="studentAmka">ΑΜΚΑ μαθητή</label><input class="form-control" type="text" formControlName="name">
30 31 32 33 34 35 36 37
        </div>
        <div class="alert alert-danger" *ngIf="formGroup.get('name').touched && formGroup.get('name').hasError('required') ">
                Το πεδίο δεν μπορεί να αφεθεί κενό!
        </div>
        <div class="alert alert-danger" *ngIf="formGroup.get('name').dirty && formGroup.get('name').hasError('pattern')">
                Επιτρέπονται μόνο ψηφία σε αυτό το πεδίο!
        </div>

Open Source Developer's avatar
Open Source Developer committed
38 39
        <div class="row">
            <div class="col-md-2 col-md-offset-5">
Open Source Developer's avatar
UI  
Open Source Developer committed
40
                <button type="button" class="btn-primary btn-lg pull-right" (click)="saveSelected()">
Open Source Developer's avatar
Open Source Developer committed
41 42 43
                Συνέχεια<span class="glyphicon glyphicon-menu-right"></span>
                </button>
            </div>
Open Source Developer's avatar
commit  
Open Source Developer committed
44 45 46
            <div *ngIf="emptyselection==true">
                 Παρακαλώ συμπληρώστε το ΑΜΚΑ του μαθητή
            </div>
Open Source Developer's avatar
Open Source Developer committed
47
        </div>
Open Source Developer's avatar
a  
Open Source Developer committed
48
     </form>
Open Source Developer's avatar
Open Source Developer committed
49 50 51 52 53 54 55 56
   `
})

@Injectable() export default class AmkaFill implements OnInit {
    private amkafills$: Observable<IAmkaFills>;

    public formGroup: FormGroup;
    private respond: any;
Open Source Developer's avatar
commit  
Open Source Developer committed
57
    emptyselection = false ;
Open Source Developer's avatar
Open Source Developer committed
58 59

       constructor(private fb: FormBuilder,
60
                private _cas: AmkaCheckService,
Open Source Developer's avatar
Open Source Developer committed
61 62 63 64
                private _cfa: AmkaFillsActions,
                private _ngRedux: NgRedux<IAppState>,
                private router: Router) {
       this.formGroup = this.fb.group({
65
            name: ['', [Validators.pattern(VALID_DIGITS_PATTERN),Validators.required]],
Open Source Developer's avatar
Open Source Developer committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            });
        };

    ngOnInit() {
          this.amkafills$ = this._ngRedux.select(state => {
            if (state.amkafills.size > 0) {
                state.amkafills.reduce(({}, amkafill) => {
                    this.formGroup.setValue(amkafill);
                    return amkafill;
                }, {});
            }
            return state.amkafills;
        });

    }


    saveSelected() {
84
     if (this.formGroup.value.name == undefined) {
Open Source Developer's avatar
commit  
Open Source Developer committed
85
              this.emptyselection = true;
86
       }
Open Source Developer's avatar
commit  
Open Source Developer committed
87
      else
88 89
      {
        this._cfa.saveAmkaFills(this.formGroup.value);
Open Source Developer's avatar
Open Source Developer committed
90
        this.router.navigate(['/epal-class-select']);
Open Source Developer's avatar
commit  
Open Source Developer committed
91
      }
Open Source Developer's avatar
Open Source Developer committed
92 93
    }

94
}