application.preview.ts 8.15 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
import { Injectable } from "@angular/core";
5
import { NgRedux, select } from '@angular-redux/store';
6
7
8
9
import { IAppState } from '../../store/store';
import { SectorFieldsActions } from '../../actions/sectorfields.actions';
import { SectorCoursesActions } from '../../actions/sectorcourses.actions';
import { RegionSchoolsActions } from '../../actions/regionschools.actions';
Open Source Developer's avatar
Open Source Developer committed
10
import { EpalClassesActions } from '../../actions/epalclass.actions';
11
12
import { ISectorFields } from '../../store/sectorfields/sectorfields.types';
import { ISectors } from '../../store/sectorcourses/sectorcourses.types';
13
import { IRegionRecord, IRegionRecords, IRegionSchoolRecord } from '../../store/regionschools/regionschools.types';
Open Source Developer's avatar
Open Source Developer committed
14
import { IEpalClasses } from '../../store/epalclasses/epalclasses.types';
15
import {AppSettings} from '../../app.settings';
16
17
18
19
20
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';

21
22
23
@Component({
    selector: 'application-preview-select',
    template: `
24
        <div *ngFor="let epalclass$ of epalclasses$ | async;">
25
        <h4 style="margin-top: 20px; line-height: 2em; ">Οι επιλογές μου</h4>
26
27
        <ul class="list-group left-side-view" style="margin-bottom: 20px;">
                <li class="list-group-item active">
28
                    Τάξη φοίτησης στο νέο σχολικό έτος
29
                </li>
Open Source Developer's avatar
trans    
Open Source Developer committed
30
                <li class="list-group-item" *ngIf="epalclass$.name === '1'">
31
                    Α’ Λυκείου
Open Source Developer's avatar
trans    
Open Source Developer committed
32
33
                </li>
                <li class="list-group-item" *ngIf="epalclass$.name === '2'">
34
                    Β’ Λυκείου
Open Source Developer's avatar
trans    
Open Source Developer committed
35
36
                </li>
                <li class="list-group-item" *ngIf="epalclass$.name === '3'">
37
                    Γ’ Λυκείου
Open Source Developer's avatar
Open Source Developer committed
38
                </li>
39
40
41
                <li class="list-group-item" *ngIf="epalclass$.name === '4'">
                    Δ’ Λυκείου
                </li>
42

Open Source Developer's avatar
Open Source Developer committed
43
        </ul>
44
        </div>
Open Source Developer's avatar
Open Source Developer committed
45

46
47
48
49
50
51
        <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>
52
        </div>
53

54
55
56
57
58
    <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>
59
        <div *ngFor="let course$ of sector$.courses;" >
60

61
                <li class="list-group-item" *ngIf="course$.selected === true">
Open Source Developer's avatar
UI    
Open Source Developer committed
62
                    {{course$.course_name   }}
63
                </li>
64
65

        </div>
66
            </ul>
67
        </div>
68

69
        <ul *ngIf="(regions$ | async)" class="list-group left-side-view" style="margin-bottom: 20px;">
70

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

73
                <li class="list-group-item" [class.oddout]="isOdd" [class.evenout]="isEven">
74
                    <span class="roundedNumber">{{(i+1)}}</span>&nbsp;&nbsp;{{epal$.get("epal_name")}}
75
76
                </li>
              </div>
77
78
79
80
81
        </ul>
  `
})

@Injectable() export default class ApplicationPreview implements OnInit {
82
    private sectors$: BehaviorSubject<ISectors>;
83
    private regions$: BehaviorSubject<IRegionRecords>;
84
    private selectedSchools$: BehaviorSubject<Array<IRegionSchoolRecord>> = new BehaviorSubject(Array());
85
86
    private sectorFields$: BehaviorSubject<ISectorFields>;
    private epalclasses$: BehaviorSubject<IEpalClasses>;
87
    private epalclassesSub: Subscription;
88
89
90
    private sectorsSub: Subscription;
    private regionsSub: Subscription;
    private sectorFieldsSub: Subscription;
91
92
    private courseActive = "-1";
    private numSelectedSchools = <number>0;
93
94
    private numSelectedOrder = <number>0;
    private classSelected = 0;
Open Source Developer's avatar
preview    
Open Source Developer committed
95
    public currentUrl: string;
96
97

    constructor(private _ngRedux: NgRedux<IAppState>,
98
99
        private router: Router
    ) {
100
101
102

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

104
105
        this.sectors$ = new BehaviorSubject(SECTOR_COURSES_INITIAL_STATE);
        this.sectorFields$ = new BehaviorSubject(SECTOR_FIELDS_INITIAL_STATE);
106
    };
107
108

    ngOnInit() {
Open Source Developer's avatar
preview    
Open Source Developer committed
109
        this.currentUrl = this.router.url;
110
        this.sectorsSub = this._ngRedux.select(state => {
111
112
            state.sectors.reduce((prevSector, sector) => {
                sector.courses.reduce((prevCourse, course) => {
113
114
115
116
                    if (course.selected === true) {
                        this.courseActive = course.course_id;
                    }

117
118
119
120
                    return course;
                }, {});
                return sector;
            }, {});
121
            //this.numSelectedCourses = numsel;
122
            return state.sectors;
123
        }).subscribe(this.sectors$);
124

125
        this.regionsSub = this._ngRedux.select(state => {
126
            let numsel = 0, numsel2 = 0;
127
            let selectedSchools = Array<IRegionSchoolRecord>();
128
            if (state.regions.size === 0)
129
                return;
130
            state.regions.reduce((prevRegion, region) => {
131
132
                region.get("epals").reduce((prevEpal, epal) => {
                    if (epal.get("selected") === true) {
133
                        numsel++;
134
                        selectedSchools.push(epal);
135
                    }
136
                    if (epal.get("order_id") !== 0) {
137
138
                        numsel2++;
                    }
139
                    return epal;
140
141
142
143
144
                }, {});
                return region;
            }, {});
            this.numSelectedSchools = numsel;
            this.numSelectedOrder = numsel2;
145
            this.selectedSchools$.next(selectedSchools.sort(this.compareSchools));
146
//            this.selectedSchools$.next(selectedSchools);
147
            return state.regions;
148
        }).subscribe(this.regions$);
149

150
        this.sectorFieldsSub = this._ngRedux.select(state => {
151
            state.sectorFields.reduce(({}, sectorField) => {
152
153
154
                return sectorField;
            }, {});
            return state.sectorFields;
155
        }).subscribe(this.sectorFields$);
156

157
        this.epalclassesSub = this._ngRedux.select(state => {
158
159
160
161
162
163
            state.epalclasses.reduce(({}, epalclass) => {
                if (epalclass.name === "Α' Λυκείου")
                    this.classSelected = 1;
                else if (epalclass.name === "Β' Λυκείου")
                    this.classSelected = 2;
                else if (epalclass.name === "Γ' Λυκείου")
164
                    this.classSelected = 3;
165
166
                else if (epalclass.name === "Δ' Λυκείου")
                    this.classSelected = 4;
167
                return epalclass;
Open Source Developer's avatar
Open Source Developer committed
168
169
            }, {});
            return state.epalclasses;
170
        }).subscribe(this.epalclasses$);
Open Source Developer's avatar
Open Source Developer committed
171

172
173
    }

174
    compareSchools(a: IRegionSchoolRecord,b: IRegionSchoolRecord) {
175
176
177
178
179
180
181
        if (a.order_id < b.order_id)
            return -1;
        if (a.order_id > b.order_id)
            return 1;
        return 0;
    }

182
    ngOnDestroy() {
183
        if (this.regionsSub) {
184
            this.regionsSub.unsubscribe();
185
186
187
//            this.regions$.unsubscribe();
        }
        if (this.sectorsSub) {
188
            this.sectorsSub.unsubscribe();
189
190
191
//            this.sectors$.unsubscribe();
        }
        if (this.sectorFieldsSub) {
192
            this.sectorFieldsSub.unsubscribe();
193
194
195
196
197
198
199
//            this.sectorFields$.unsubscribe();
        }
        if (this.epalclassesSub) {
            this.epalclassesSub.unsubscribe();
//            this.epalclasses$.unsubscribe();
        }

200
    }
201

202
}