application.preview.ts 8.59 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
9
10
import { Injectable } from "@angular/core";
import { NgRedux, select } from 'ng2-redux';
import { IAppState } from '../../store/store';
import { SectorFieldsActions } from '../../actions/sectorfields.actions';
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
import { StudentDataFieldsActions } from '../../actions/studentdatafields.actions';
Open Source Developer's avatar
Open Source Developer committed
11
import { EpalClassesActions } from '../../actions/epalclass.actions';
12
13
import { ISectorFields } from '../../store/sectorfields/sectorfields.types';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
14
import { IRegions, IRegionSchool } from '../../store/regionschools/regionschools.types';
15
import { IStudentDataFields } from '../../store/studentdatafields/studentdatafields.types';
Open Source Developer's avatar
Open Source Developer committed
16
import { IEpalClasses } from '../../store/epalclasses/epalclasses.types';
17
import {AppSettings} from '../../app.settings';
18
19
20
21
22
23
import { REGION_SCHOOLS_INITIAL_STATE } from '../../store/regionschools/regionschools.initial-state';
import { EPALCLASSES_INITIAL_STATE } from '../../store/epalclasses/epalclasses.initial-state';
import { SECTOR_COURSES_INITIAL_STATE } from '../../store/sectorcourses/sectorcourses.initial-state';
import { SECTOR_FIELDS_INITIAL_STATE } from '../../store/sectorfields/sectorfields.initial-state';
import { STUDENT_DATA_FIELDS_INITIAL_STATE } from '../../store/studentdatafields/studentdatafields.initial-state';

24
25
26
@Component({
    selector: 'application-preview-select',
    template: `
27
        <div *ngFor="let epalclass$ of epalclasses$ | async;">
28
        <h4 style="margin-top: 20px; line-height: 2em; ">Οι επιλογές μου</h4>
29
30
31
32
        <ul class="list-group left-side-view" style="margin-bottom: 20px;">
                <li class="list-group-item active">
                    Τάξη εισαγωγής
                </li>
Open Source Developer's avatar
Open Source Developer committed
33
                <li class="list-group-item">
34
                    {{epalclass$.name  }}
Open Source Developer's avatar
Open Source Developer committed
35
                </li>
36

Open Source Developer's avatar
Open Source Developer committed
37
        </ul>
38
        </div>
Open Source Developer's avatar
Open Source Developer committed
39

40
41
42
43
44
45
        <div *ngFor="let sectorField$ of sectorFields$ | async">
        <ul class="list-group left-side-view">
            <li class="list-group-item active" *ngIf="sectorField$.selected === true" >
                {{sectorField$.name}}
            </li>
            </ul>
46
        </div>
47

48
49
50
51
52
    <div *ngFor="let sector$ of sectors$  | async;">
            <ul class="list-group left-side-view" style="margin-bottom: 20px;" *ngIf="sector$.sector_selected === true">
                <li class="list-group-item active" *ngIf="sector$.sector_selected === true" >
                    {{sector$.sector_name }}
                </li>
53
        <div *ngFor="let course$ of sector$.courses;" >
54

55
                <li class="list-group-item" *ngIf="course$.selected === true">
Open Source Developer's avatar
UI    
Open Source Developer committed
56
                    {{course$.course_name   }}
57
                </li>
58
59

        </div>
60
            </ul>
61
        </div>
62

63
64


65
        <ul *ngIf="(regions$ | async).size > 0" class="list-group left-side-view" style="margin-bottom: 20px;">
66

67
                <div *ngFor="let epal$ of selectedSchools$ | async; let i=index; let isOdd=odd; let isEven=even" >
68

69
                <li class="list-group-item" [class.oddout]="isOdd" [class.evenout]="isEven">
70
                    <span class="roundedNumber">{{(i+1)}}</span>&nbsp;&nbsp;{{epal$.epal_name}}
71
72
                </li>
              </div>
73
        </ul>
74

75
              <div *ngFor="let studentDataField$ of studentDataFields$ | async;">
76
77
78
79
              <ul class="list-group left-side-view" style="margin-bottom: 20px;">
              <li class="list-group-item active">
                  Στοιχεία μαθητή
              </li>
80
                <li class="list-group-item">
81
                    {{studentDataField$.name  }}
82
83
                </li>
                <li class="list-group-item">
84
                    {{studentDataField$.studentsurname  }}
85
                </li>
86
                </ul>
87
            </div>
88
89
90
91
  `
})

@Injectable() export default class ApplicationPreview implements OnInit {
92
93
    private sectors$: BehaviorSubject<ISectors>;
    private regions$: BehaviorSubject<IRegions>;
94
    private selectedSchools$: BehaviorSubject<Array<IRegionSchool>> = new BehaviorSubject(Array());
95
96
97
98
99
100
101
    private sectorFields$: BehaviorSubject<ISectorFields>;
    private studentDataFields$: BehaviorSubject<IStudentDataFields>;
    private epalclasses$: BehaviorSubject<IEpalClasses>;
    private sectorsSub: Subscription;
    private regionsSub: Subscription;
    private sectorFieldsSub: Subscription;
    private studentDataFieldsSub: Subscription;
102
103
    private courseActive = "-1";
    private numSelectedSchools = <number>0;
104
105
    private numSelectedOrder = <number>0;
    private classSelected = 0;
106
107

    constructor(private _ngRedux: NgRedux<IAppState>,
108
109
        private router: Router
    ) {
110
111
112

        this.regions$ = new BehaviorSubject(REGION_SCHOOLS_INITIAL_STATE);
        this.epalclasses$ = new BehaviorSubject(EPALCLASSES_INITIAL_STATE);
113

114
115
116
        this.sectors$ = new BehaviorSubject(SECTOR_COURSES_INITIAL_STATE);
        this.sectorFields$ = new BehaviorSubject(SECTOR_FIELDS_INITIAL_STATE);
        this.studentDataFields$ = new BehaviorSubject(STUDENT_DATA_FIELDS_INITIAL_STATE);
117
    };
118
119

    ngOnInit() {
120
        this.sectorsSub = this._ngRedux.select(state => {
121
122
            state.sectors.reduce((prevSector, sector) => {
                sector.courses.reduce((prevCourse, course) => {
123
124
125
126
                    if (course.selected === true) {
                        this.courseActive = course.course_id;
                    }

127
128
129
130
                    return course;
                }, {});
                return sector;
            }, {});
131
            //this.numSelectedCourses = numsel;
132
            return state.sectors;
133
        }).subscribe(this.sectors$);
134

135
        this.regionsSub = this._ngRedux.select(state => {
136
            let numsel = 0, numsel2 = 0;
137
            let selectedSchools = Array<IRegionSchool>();
138
139
            state.regions.reduce((prevRegion, region) => {
                region.epals.reduce((prevEpal, epal) => {
140
                    if (epal.selected === true) {
141
                        numsel++;
142
                        selectedSchools.push(epal);
143
144
145
146
                    }
                    if (epal.order_id !== 0) {
                        numsel2++;
                    }
147
                    return epal;
148
149
150
151
152
                }, {});
                return region;
            }, {});
            this.numSelectedSchools = numsel;
            this.numSelectedOrder = numsel2;
153
            this.selectedSchools$.next(selectedSchools.sort(this.compareSchools));
154
//            this.selectedSchools$.next(selectedSchools);
155
            return state.regions;
156
        }).subscribe(this.regions$);
157

158
        this.sectorFieldsSub = this._ngRedux.select(state => {
159
            state.sectorFields.reduce(({}, sectorField) => {
160
161
162
                return sectorField;
            }, {});
            return state.sectorFields;
163
        }).subscribe(this.sectorFields$);
164

165
        this.studentDataFieldsSub = this._ngRedux.select(state => {
166
            state.studentDataFields.reduce(({}, studentDataField) => {
167
168
169
                return studentDataField;
            }, {});
            return state.studentDataFields;
170
        }).subscribe(this.studentDataFields$);
171

172
        this._ngRedux.select(state => {
173
174
175
176
177
178
            state.epalclasses.reduce(({}, epalclass) => {
                if (epalclass.name === "Α' Λυκείου")
                    this.classSelected = 1;
                else if (epalclass.name === "Β' Λυκείου")
                    this.classSelected = 2;
                else if (epalclass.name === "Γ' Λυκείου")
179
                    this.classSelected = 3;
180
                return epalclass;
Open Source Developer's avatar
Open Source Developer committed
181
182
            }, {});
            return state.epalclasses;
183
        }).subscribe(this.epalclasses$);
Open Source Developer's avatar
Open Source Developer committed
184

185
186
    }

187
188
189
190
191
192
193
194
    compareSchools(a: IRegionSchool,b: IRegionSchool) {
        if (a.order_id < b.order_id)
            return -1;
        if (a.order_id > b.order_id)
            return 1;
        return 0;
    }

195
    ngOnDestroy() {
196
197
198
199
200
201
202
203
        if (this.regionsSub)
            this.regionsSub.unsubscribe();
        if (this.sectorsSub)
            this.sectorsSub.unsubscribe();
        if (this.sectorFieldsSub)
            this.sectorFieldsSub.unsubscribe();
        if (this.studentDataFieldsSub)
            this.studentDataFieldsSub.unsubscribe();
204
205
206
207
208
        this.regions$.unsubscribe();
        this.epalclasses$.unsubscribe();
        this.sectors$.unsubscribe();
        this.sectorFields$.unsubscribe();
        this.studentDataFields$.unsubscribe();
209
    }
210

211
}