<commit_before>import { Component, DynamicComponentLoader, ViewContainerRef } from 'angular2/core';
import { Router, RouteParams, RouteConfig, ROUTER_DIRECTIVES,  } from 'angular2/router'
import { FORM_PROVIDERS, FORM_DIRECTIVES, FormBuilder, Validators, Control } from 'angular2/common';
import { Http } from 'angular2/http'
import 'rxjs/Rx'

import { AdminAuthService, AdminUtils } from './admin.auth'
import { Admin } from './admin'

//-----------------------------------------------------------AdminAccountProfile
@Component({
    selector      : 'profile',
    templateUrl: 'templates/account/profile.html',
    directives    : [FORM_DIRECTIVES],
})
export class AdminAccountProfile {
    errors = [];
    obj_errors = {};
    user = undefined;
    sessions = [];
    new_avatar = null;
    
    formChange = false;
    emailChange = false;
    passwordChange = false;
    confirmPassword = false;
    expireSessions = false;
    showSetAdmin = false;
    canDeactivate = undefined;
    
    isUser = false;    //currentUser is this user
    isAdmin = false;    //currentUser is admin

    static get parameters() {
        return [[Http], [AdminAuthService], [FormBuilder], 
            [RouteParams], [Admin], [AdminUtils] ];
    }
    constructor(http, authService, formbuilder, routeparams, admin, utils ) {
        this._http = http;
        this._admin = admin;
        this._routeParams = routeparams;
        this._auth = authService;
        this._utils = utils;
        
        this.lform = formbuilder.group({
            'first_name': ['', Validators.required],
            'last_name': ['', Validators.required],
            'phone': [''],
            'www_site': [''],
            'bio': [''],
            'avatar_image': [''],
            'is_admin': [''],
            'admin_password': [''],
            'password1': [''],
            'password2': [''],
        }); 
        
        let e = new Control('')
        this.lform.addControl('email',e);
    }

    ngOnInit() {
        let id = this._routeParams.get('id');
        this._http
            .get(`/api/admin/${id}/`)
            .subscribe( 
                (data) => this.onInit(data),
                (err) => {
                    this.obj_errors = err; 
                    this.errors = this._utils.to_array(err.json()); 
                }, 
            ); 
        this.getSessions(id);
        this._auth.getCurrentUser().then(data => this.currentUser = data);
    }

    onInit(data) {
        this.user = data;
        this.errors = [];
        this.obj_errors = {};
        this.new_avatar = null;
        this.formChange = false;
        this.emailChange = false;
        this.passwordChange = false;
        this.confirmPassword = false;
        this.expireSessions = false;
        this.showSetAdmin = false;
        this.canDeactivate = undefined;
        
        this.isUser = this.user.id == this._auth._currentUser.id;// no correct
        this.isAdmin = this._auth._currentUser.is_admin;

        this._admin.currentUrl({
            'url':'#', 'text': `${this.user.first_name} ${this.user.last_name}`
        });

        this._admin.headerButtons = [];
        if (this.isAdmin && !this.isUser) {
            if (!this.user.is_admin) {
                this._admin.headerButtons.push({
                    'text': 'Make this user the account owner', 
                    'class': 'btn mr10', 'click': this.setAdmin, 'self': this 
                });
            }
            else {
                this._admin.headerButtons.push({
                    'text': 'Take away this user the account owner permissions',
                    'class': 'btn mr10', 'click': this.setAdmin, 'self': this
                });
            }
        }

        this._admin.headerButtons.push({
            'text': 'Save', 'class': 'btn btn-blue', 
            'click': this.onSave, 'primary': true, 'self': this
        });
        
        for (let control in this.lform.controls) {
            if (control != 'avatar_image') {
                this.lform.controls[control].updateValue(undefined);
                this.lform.controls[control]
                    .updateValue(this.user[control], true, true);
            }
        }
    }
    
    cls() {
        this.confirmPassword = false;
        for (let control in this.lform.controls) {
            if (control != 'avatar_image') {
                this.lform.controls[control].updateValue(undefined);
                this.lform.controls[control]
                          .updateValue(this.user[control], true, true);
            }
        }
    }

    routerCanDeactivate() {
        if (!this.formChange)
            return true;
        this.close  = false;
        this.canDeactivate = new Promise(
            (resolve) => {
                this.unloadPage = resolve;
            }
        );
        return this.canDeactivate;
    }

    onSave(self) {
        if (!self) 
            self = this;
        if  (
                self.lform.controls['email'].value != self.user.email || 
                self.lform.controls['password1'].value  || 
                self.lform.controls['password2'].value 
            ) { 
            
            self.lform.controls['admin_password'].updateValue('');
            self.confirmPassword = true;
        }        
        else 
            self.onSaveAdmin();
    }
    
    onSaveAdmin() { // admin permissions
        if (this.new_avatar)
            this.lform.controls['avatar_image'].updateValue(this.new_avatar);

        this._http
            .patch(`/api/admin/${this.user.id}/`, this.lform.value )
            .subscribe(<commit_msg>Profile uses AdminLeavePage Component<commit_after>import { Component, DynamicComponentLoader, ViewContainerRef } from 'angular2/core';
import { Router, RouteParams, RouteConfig, ROUTER_DIRECTIVES,  } from 'angular2/router'
import { FORM_PROVIDERS, FORM_DIRECTIVES, FormBuilder, Validators, Control } from 'angular2/common';
import { Http } from 'angular2/http'
import 'rxjs/Rx'

import { AdminAuthService, AdminUtils } from './admin.auth'
import { Admin } from './admin'

import { AdminLeavePage } from './components';


//-----------------------------------------------------------AdminAccountProfile
@Component({
    selector      : 'profile',
    templateUrl: 'templates/account/profile.html',
    directives    : [FORM_DIRECTIVES, AdminLeavePage],
})
export class AdminAccountProfile {
    errors = [];
    obj_errors = {};
    user = undefined;
    sessions = [];
    new_avatar = null;
    
    formChange = false;
    emailChange = false;
    passwordChange = false;
    confirmPassword = false;
    expireSessions = false;
    showSetAdmin = false;
    canDeactivate = undefined;
    
    isUser = false;    //currentUser is this user
    isAdmin = false;    //currentUser is admin

    static get parameters() {
        return [[Http], [AdminAuthService], [FormBuilder], 
            [RouteParams], [Admin], [AdminUtils] ];
    }
    constructor(http, authService, formbuilder, routeparams, admin, utils ) {
        this._http = http;
        this._admin = admin;
        this._routeParams = routeparams;
        this._auth = authService;
        this._utils = utils;
        
        this.lform = formbuilder.group({
            'first_name': ['', Validators.required],
            'last_name': ['', Validators.required],
            'phone': [''],
            'www_site': [''],
            'bio': [''],
            'avatar_image': [''],
            'is_admin': [''],
            'admin_password': [''],
            'password1': [''],
            'password2': [''],
        }); 
        
        let e = new Control('')
        this.lform.addControl('email',e);
    }

    ngOnInit() {
        this.self = this; // for child components
        let id = this._routeParams.get('id');
        this._http
            .get(`/api/admin/${id}/`)
            .subscribe( 
                (data) => this.onInit(data),
                (err) => {
                    this.obj_errors = err; 
                    this.errors = this._utils.to_array(err.json()); 
                }, 
            ); 
        this.getSessions(id);
        this._auth.getCurrentUser().then(data => this.currentUser = data);
    }

    onInit(data) {
        this.user = data;
        this.errors = [];
        this.obj_errors = {};
        this.new_avatar = null;
        this.formChange = false;
        this.emailChange = false;
        this.passwordChange = false;
        this.confirmPassword = false;
        this.expireSessions = false;
        this.showSetAdmin = false;
        this.canDeactivate = undefined;
        
        this.isUser = this.user.id == this._auth._currentUser.id;// no correct
        this.isAdmin = this._auth._currentUser.is_admin;

        this._admin.currentUrl({
            'url':'#', 'text': `${this.user.first_name} ${this.user.last_name}`
        });

        this._admin.headerButtons = [];
        if (this.isAdmin && !this.isUser) {
            if (!this.user.is_admin) {
                this._admin.headerButtons.push({
                    'text': 'Make this user the account owner', 
                    'class': 'btn mr10', 'click': this.setAdmin, 'self': this 
                });
            }
            else {
                this._admin.headerButtons.push({
                    'text': 'Take away this user the account owner permissions',
                    'class': 'btn mr10', 'click': this.setAdmin, 'self': this
                });
            }
        }

        this._admin.headerButtons.push({
            'text': 'Save', 'class': 'btn btn-blue', 
            'click': this.onSave, 'primary': true, 'self': this
        });
        
        for (let control in this.lform.controls) {
            if (control != 'avatar_image') {
                this.lform.controls[control].updateValue(undefined);
                this.lform.controls[control]
                    .updateValue(this.user[control], true, true);
            }
        }
    }
    
    cls() {
        this.confirmPassword = false;
        for (let control in this.lform.controls) {
            if (control != 'avatar_image') {
                this.lform.controls[control].updateValue(undefined);
                this.lform.controls[control]
                          .updateValue(this.user[control], true, true);
            }
        }
    }

    routerCanDeactivate() {
        if (!this.formChange)
            return true;
        this.leavePage = true;
        this.canDeactivate = new Promise(
            (resolve) => {
                this.unloadPage = resolve;
            }
        );
        return this.canDeactivate;
    }

    onSave(self) {
        if (!self) 
            self = this;
        if  (
                self.lform.controls['email'].value != self.user.email || 
                self.lform.controls['password1'].value  || 
                self.lform.controls['password2'].value 
            ) { 
            
            self.lform.controls['admin_password'].updateValue('');
            self.confirmPassword = true;
        }        
        else 
            self.onSaveAdmin();
    }
    
    onSaveAdmin() { // admin permissions
        if (this.new_avatar)
            this.lform.controls['avatar_image'].updateValue(this.new_avatar);

        this._http
            .patch(`/api/admin/${this.user.id}/`, this.lform.value )
            .subscribe(
{'fraction_non_alphanumeric': 0.14203101726101813, 'fraction_numerical': 0.0021028651537720144, 'mean_word_length': 1.6556537924616102, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4663053', 'n_tokens_mistral': 3233, 'n_tokens_neox': 3144, 'n_words': 602}