Populate sidedrawer user and email from server


#1

What is the best way to set sidedrawer user and email Label text? When I load home-view-model.ts I load all user infos inside a function to populate some Labels.

Should I get user and email from home-view-model.ts with localStorage or instantiate home-view-model.ts inside Mydrawer-view-model.ts?

home-view-model.ts

import { Observable } from "data/observable";
import * as pushPlugin from "nativescript-push-notifications";

require( "nativescript-localstorage" );

var http = require("http");
var LoadingIndicator = require("nativescript-loading-indicator").LoadingIndicator;
var loader = new LoadingIndicator();

export class HomeViewModel extends Observable {

    private serverUrl = "http://www.website.com/select-myinfos.php";
    user_id:string = "1";

    username:string;
    name: string;
    email:string;

    constructor() {
        super();

        this.getUserInfos();
    }

 public getUserInfos() {

        loader.show();
        console.log("get user infos executed");
         
        http.request({
            url: this.serverUrl,
            method: "POST",
            headers: { "Content-Type": "application/json" },
            content: JSON.stringify({ id: this.user_id })
        
        }).then((response) => {
    
            var resposta = response.content.toJSON();            
            //console.log("resposta: "+response.content);
    
            if(resposta === "user not found"){
                loader.hide();
                alert("Nao foram encontradas informações deste usuário.");
    
            }else if(resposta === "user id not found"){
                loader.hide();
                alert("Identificação de usuário incorreta.");
    
            }else {
                
                this.set("username", response.content.toJSON()[0].USER);
                this.set("name", response.content.toJSON()[0].NAME);
                this.set("cpf", response.content.toJSON()[0].CPF);
                this.set("birth", response.content.toJSON()[0].BIRTH_DATE);
                this.set("e_civil", response.content.toJSON()[0].E_CIVIL);
                this.set("genero", response.content.toJSON()[0].GENERO);
                this.set("address", response.content.toJSON()[0].ENDERECO);
                this.set("number", response.content.toJSON()[0].NUMERO);
                this.set("complement", response.content.toJSON()[0].COMPLEMENTO);
                this.set("neighborhood", response.content.toJSON()[0].BAIRRO);
                this.set("city", response.content.toJSON()[0].CIDADE);
                this.set("cep", response.content.toJSON()[0].CEP);
                this.set("state", response.content.toJSON()[0].ESTADO);
                this.set("country", response.content.toJSON()[0].PAIS);
                this.set("email", response.content.toJSON()[0].EMAIL);
                this.set("phone", response.content.toJSON()[0].TELEFONE);
                this.set("cellphone", response.content.toJSON()[0].CELULAR);
                this.set("pdf", response.content.toJSON()[0].PDF);

                localStorage.setItem("mail", this.get("email"));
                localStorage.setItem("user", this.get("username"));

                loader.hide();
            }
        

        }, function (e) {
            console.log("Error occurred " + e);
            //loader.hide();
            alert("Erro ao baixar informações. Verifique sua conexão.");
        });

    }

MyDrawer-view-model.ts

import { Observable } from "data/observable";
import { ObservableProperty } from "../../shared/observable-property-decorator";
import { HomeViewModel } from "../../home/home-view-model";
require( "nativescript-localstorage" );
/* ***********************************************************
* Keep data that is displayed in your app drawer in the MyDrawer custom component view model.
*************************************************************/
export class MyDrawerViewModel extends Observable {

    @ObservableProperty() selectedPage: string;
    @ObservableProperty() user: string;
    @ObservableProperty() email: string;

    /* ***********************************************************
    * Use the MyDrawer view model constructor to initialize the properties data values.
    *************************************************************/
    constructor(selectedPage: string) {
        super();

        this.selectedPage = selectedPage;
        
        //var user = new HomeViewModel();
        this.email = localStorage.getItem("mail");
        this.user = localStorage.getItem("user");
        
    }
}