report-users.ts 5.11 KB
Newer Older
1
import { NgRedux } from "@angular-redux/store";
2
import { Injectable } from "@angular/core";
3 4 5
import { Component, Input, OnDestroy, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { LocalDataSource } from "ng2-smart-table";
6 7 8
import { BehaviorSubject, Subscription } from "rxjs/Rx";

import { API_ENDPOINT } from "../../app.settings";
9 10 11 12 13 14
import { HelperDataService } from "../../services/helper-data-service";
import { LOGININFO_INITIAL_STATE } from "../../store/logininfo/logininfo.initial-state";
import { ILoginInfoRecords } from "../../store/logininfo/logininfo.types";
import { IAppState } from "../../store/store";
import { CsvCreator } from "./csv-creator";
import { ReportsSchema, TableColumn } from "./reports-schema";
15 16

@Component({
17
    selector: "report-users",
18 19
    template: `

20 21
    <div class="reports-container">
        <div class = "loading" *ngIf="validCreator == 0"></div>
22

23 24
        <h5>Αριθμός Αιτήσεων / Εγγεγραμμένων Χρηστών</h5>
        <h6>Επιλογή Φίλτρων: Δεν υπάρχουν διαθέσιμα φίλτρα</h6>
25

26 27
        <button type="submit" class="btn btn-alert" (click)="createReport()" [hidden]="minedu_userName == ''"><i class="fa fa-file-text"></i> Δημιουργία Αναφοράς</button>
        <button type="submit" class="btn btn-alert pull-right"  (click)="navigateBack()" [hidden]="minedu_userName == ''" >Επιστροφή</button>
28 29 30

        <div *ngIf="validCreator == 1 ">
          <input #search class="search" type="text" placeholder="Αναζήτηση..." (keydown.enter)="onSearch(search.value)">
31
          <div class="smart-table-container table table-hover table-striped" reportScroll>
32 33 34 35 36
            <ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table>
          </div>
        </div>

        <button type="button" class="alert alert-info pull-right" (click)="export2Csv()" [hidden]="validCreator != 1">
37
        <i class="fa fa-download"></i> Εξαγωγή σε csv</button>
38 39 40 41 42 43
    </div>
   `
})

@Injectable() export default class ReportUsers implements OnInit, OnDestroy {

44
    loginInfo$: BehaviorSubject<ILoginInfoRecords>;
45 46 47 48 49 50 51 52 53 54 55 56
    loginInfoSub: Subscription;
    private generalReport$: BehaviorSubject<any>;
    private generalReportSub: Subscription;
    private apiEndPoint = API_ENDPOINT;
    private minedu_userName: string;
    private minedu_userPassword: string;
    private distStatus = "READY";
    private data;
    private validCreator: number;
    private routerSub: any;

    private source: LocalDataSource;
57
    columnMap: Map<string, TableColumn> = new Map<string, TableColumn>();
58
    @Input() settings: any;
59 60
    private reportSchema = new ReportsSchema();
    private csvObj = new CsvCreator();
61

62 63
    constructor(
        private _ngRedux: NgRedux<IAppState>,
64 65
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
66 67
        private router: Router
    ) {
68 69
        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.generalReport$ = new BehaviorSubject([{}]);
70
        this.minedu_userName = "";
71
        this.validCreator = -1;
72 73 74 75
    }

    ngOnInit() {

76
        this.loginInfoSub = this._ngRedux.select("loginInfo")
77
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
78 79
            .subscribe(loginInfo => {
                if (loginInfo.size > 0) {
80 81 82 83
                    loginInfo.reduce(({}, loginInfoObj) => {
                        this.minedu_userName = loginInfoObj.minedu_username;
                        this.minedu_userPassword = loginInfoObj.minedu_userpassword;
                        return loginInfoObj;
84 85 86 87
                    }, {});
                }
                this.loginInfo$.next(loginInfo);
            }, error => console.log("error selecting loginInfo"));
88 89 90
    }

    ngOnDestroy() {
91 92 93 94
        if (this.loginInfoSub)
            this.loginInfoSub.unsubscribe();
        if (this.generalReportSub)
            this.generalReportSub.unsubscribe();
95 96
    }

97 98
    createReport() {
        this.validCreator = 0;
99
        let route = "/ministry/report-users/";
100
        this.settings = this.reportSchema.ReportUsersSchema;
101

102 103 104 105 106 107 108 109 110 111 112 113 114
        this.generalReportSub = this._hds.makeReport(this.minedu_userName, this.minedu_userPassword, route, 0, 0, 0, 0, 0, 0, 0)
            .subscribe(data => {
                this.generalReport$.next(data);
                this.data = data;
                this.validCreator = 1;
                this.source = new LocalDataSource(this.data);
                this.columnMap = new Map<string, TableColumn>();

                this.csvObj.columnMap = this.columnMap;
                this.csvObj.source = this.source;
                this.csvObj.settings = this.settings;
                this.csvObj.prepareColumnMap();
            },
115 116 117 118 119 120
            error => {
                this.generalReport$.next([{}]);
                this.validCreator = -1;
                console.log("Error Getting ReportUsers");
            });
    }
121

122
    navigateBack() {
123
        this.router.navigate(["/ministry/minister-reports"]);
124
    }
125

126
    onSearch(query: string = "") {
127 128
        this.csvObj.onSearch(query);
    }
129

130 131 132
    export2Csv() {
        this.csvObj.export2Csv();
    }
133 134

}