application.preview.ts 8.38 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
Open Source Developer committed
30
                <li class="list-group-item" *ngIf="epalclass$.name === '1'">
31
                    Α’ Λυκείου
Open Source Developer's avatar
Open Source Developer committed
32 33
                </li>
                <li class="list-group-item" *ngIf="epalclass$.name === '2'">
34
                    Β’ Λυκείου
Open Source Developer's avatar
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
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 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
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
Open Source Developer committed
109
        this.currentUrl = this.router.url;
110 111 112 113 114 115 116 117 118 119 120 121
        this.sectorsSub = this._ngRedux.select('sectors')
            .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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
                this.sectors$.next(scs);
            });

        this.regionsSub = this._ngRedux.select('regions')
            .subscribe(regions => {
                let rgns = <IRegionRecords>regions;
                let numsel = 0, numsel2 = 0;
                let selectedSchools = Array<IRegionSchoolRecord>();
                if (rgns.size === 0)
                    return;
                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;
145
                }, {});
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
                this.numSelectedSchools = numsel;
                this.numSelectedOrder = numsel2;
                this.selectedSchools$.next(selectedSchools.sort(this.compareSchools));
            });

        this.sectorFieldsSub = this._ngRedux.select('sectorFields')
            .subscribe(sectorFields => {
                this.sectorFields$.next(<ISectorFields>sectorFields);
            }, error => {console.log("error selecting sectorFields");});

        this.epalclassesSub = this._ngRedux.select('epalclasses')
            .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);
            }, error => {console.log("error selecting epalclasses");});
Open Source Developer's avatar
Open Source Developer committed
172

173 174
    }

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

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

201
    }
202

203
}