application.preview.ts 7.92 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 { 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
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<IRegionSchoolRecord>();
127
            if (state.regions.size === 0)
128
                return;
129
            state.regions.reduce((prevRegion, region) => {
130
131
                region.get("epals").reduce((prevEpal, epal) => {
                    if (epal.get("selected") === true) {
132
                        numsel++;
133
                        selectedSchools.push(epal);
134
                    }
135
                    if (epal.get("order_id") !== 0) {
136
137
                        numsel2++;
                    }
138
                    return epal;
139
140
141
142
143
                }, {});
                return region;
            }, {});
            this.numSelectedSchools = numsel;
            this.numSelectedOrder = numsel2;
144
            this.selectedSchools$.next(selectedSchools.sort(this.compareSchools));
145
//            this.selectedSchools$.next(selectedSchools);
146
            return state.regions;
147
        }).subscribe(this.regions$);
148

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

156
        this._ngRedux.select(state => {
157
158
159
160
161
162
            state.epalclasses.reduce(({}, epalclass) => {
                if (epalclass.name === "Α' Λυκείου")
                    this.classSelected = 1;
                else if (epalclass.name === "Β' Λυκείου")
                    this.classSelected = 2;
                else if (epalclass.name === "Γ' Λυκείου")
163
                    this.classSelected = 3;
164
165
                else if (epalclass.name === "Δ' Λυκείου")
                    this.classSelected = 4;
166
                return epalclass;
Open Source Developer's avatar
Open Source Developer committed
167
168
            }, {});
            return state.epalclasses;
169
        }).subscribe(this.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
183
184
185
186
187
        if (this.regionsSub)
            this.regionsSub.unsubscribe();
        if (this.sectorsSub)
            this.sectorsSub.unsubscribe();
        if (this.sectorFieldsSub)
            this.sectorFieldsSub.unsubscribe();
188
189
190
191
        this.regions$.unsubscribe();
        this.epalclasses$.unsubscribe();
        this.sectors$.unsubscribe();
        this.sectorFields$.unsubscribe();
192
    }
193

194
}