login.component.ts 3.31 KB
Newer Older
1
2
3
4
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { SessionService } from '../shared/services/session.service';
import { AuthenticationService } from '../shared/services/authentication.service';
5
import { TranslateService } from '@ngx-translate/core';
6
7
8
9
10
11
12
13
14
15


@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.scss']

})

export class LoginComponent implements OnInit {
16
    languages = [
17
18
        {id: 0, name: "Español", code: 'es', flagCode: 'es'},
        {id: 1, name: "English", code: 'en', flagCode: 'us'}
19
20
21
22
23
24
    ];

	model: any = {
        language: this.languages[0]
    };

25
26
27
28
29
    loading = false;
    error = false;
    errorText = "";
    returnUrl: string;

30
  
31
32
33
34
35
    constructor(
        private route: ActivatedRoute,
        private router: Router,
        private sessionService: SessionService,
        private authenticationService: AuthenticationService,
36
        public translate: TranslateService,
37
38
39
        ) { }

    ngOnInit() {
40
41
42
43
44
45
46
47
48
49
50
        console.log('this.route', this.route.snapshot.data['language']);
        console.log('return', this.route.snapshot.queryParams['returnUrl']);
        
        // let currentSession = sessionStorage.getItem("currentUser"); 
        // let langCode = currentSession ? JSON.parse(currentSession).language : 'es';
        // if (langCode) {
        //     this.model.language = this.getLanguageElementByCode(langCode);
        // }

        this.model.language = this.getLanguageElementByCode(this.route.snapshot.data['language']);
        this.translate.use(this.model.language.code);
Diego Rey's avatar
Diego Rey committed
51

52
53
54
55
56
57
58
59
60
61
62
63
        // reset login status
        this.authenticationService.logout();

        // get return url from route parameters or default to '/'
        this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/matefun';
    }

    login() {
    	// this.router.navigate([this.returnUrl]);
        this.loading = true;

        var that = this;
64

65
        this.translate.use(this.model.language.code);
66
        this.authenticationService.login(this.model.cedula, this.model.password, this.model.language.code)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
            .subscribe(
                data => {
                    //resetSession = true;
                    this.router.navigate([this.returnUrl]);
                    that.sessionService.reset();
                },
                error => {
                    this.loading = false;
                    this.error = true;
                    this.errorText = error.text();
                });
    }

    invitado(){
        this.loading = true;
82
83

        this.authenticationService.login("invitado", "invitado", this.model.language.code)
84
85
86
87
88
89
90
91
92
93
            .subscribe(
                data => {
                    this.router.navigate([this.returnUrl]);
                    this.sessionService.reset();
                },
                error => {
                    this.loading = false;
                });
    }

94
95
96
97
98
    onChangeLanguage(lang) {
        this.model.language = lang;
        this.translate.use(this.model.language.code);
    }

Diego Rey's avatar
Diego Rey committed
99
100
101
102
103
104
105
106
107
108
109
    getLanguageElementByCode(code) {
        let langElement = null;
        for(let lang of this.languages) {
            if (lang.code === code) {
                langElement = lang;
                break;
            }
        };
        return langElement;
    }

110
}