application.preview.ts 7.78 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
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';
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 { IRegions, IRegionSchool } 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).size > 0" 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$.epal_name}}
75
76
                </li>
              </div>
77
78
79
80
81
        </ul>
  `
})

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

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

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

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

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

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

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

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

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

169
170
    }

171
172
173
174
175
176
177
178
    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;
    }

179
    ngOnDestroy() {
180
181
182
183
184
185
        if (this.regionsSub)
            this.regionsSub.unsubscribe();
        if (this.sectorsSub)
            this.sectorsSub.unsubscribe();
        if (this.sectorFieldsSub)
            this.sectorFieldsSub.unsubscribe();
186
187
188
189
        this.regions$.unsubscribe();
        this.epalclasses$.unsubscribe();
        this.sectors$.unsubscribe();
        this.sectorFields$.unsubscribe();
190
    }
191

192
}