application.preview.ts 8.39 KB
Newer Older
1
2
3
import { Component, OnInit, OnDestroy } from "@angular/core";
import { Router } from "@angular/router";
import { BehaviorSubject, Subscription } from "rxjs/Rx";
4
import { Injectable } from "@angular/core";
5
6
7
8
9
10
import { NgRedux, select } from "@angular-redux/store";
import { IAppState } from "../../store/store";
import { SectorFieldsActions } from "../../actions/sectorfields.actions";
import { SectorCoursesActions } from "../../actions/sectorcourses.actions";
import { RegionSchoolsActions } from "../../actions/regionschools.actions";
import { EpalClassesActions } from "../../actions/epalclass.actions";
11
import { ISectorFieldRecords } from "../../store/sectorfields/sectorfields.types";
12
13
14
15
16
17
18
19
import { ISectors } from "../../store/sectorcourses/sectorcourses.types";
import { IRegionRecord, IRegionRecords, IRegionSchoolRecord } from "../../store/regionschools/regionschools.types";
import { IEpalClasses } from "../../store/epalclasses/epalclasses.types";
import {AppSettings} from "../../app.settings";
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";
20

21
@Component({
22
    selector: "application-preview-select",
23
    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="(selectedSchools$ | 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
    private sectorFields$: BehaviorSubject<ISectorFieldRecords>;
86
    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;
95
    private 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("sectors")
111
112
113
114
115
116
117
118
119
120
121
            .subscribe(sectors => {
                let scs = <ISectors>sectors;
                scs.reduce((prevSector, sector) => {
                    sector.courses.reduce((prevCourse, course) => {
                        if (course.selected === true) {
                            this.courseActive = course.course_id;
                        }

                        return course;
                    }, {});
                    return sector;
122
                }, {});
123
124
125
                this.sectors$.next(scs);
            });

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

149
        this.sectorFieldsSub = this._ngRedux.select("sectorFields")
150
            .subscribe(sectorFields => {
151
                this.sectorFields$.next(<ISectorFieldRecords>sectorFields);
152
            }, error => { console.log("error selecting sectorFields"); });
153

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

171
172
    }

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

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

199
    }
200

201
}