Sfoglia il codice sorgente

tools for delete account

robert2206 8 anni fa
parent
commit
425b34c3dd

+ 3 - 0
src/app/app.module.ts

@@ -8,6 +8,7 @@ import { LoginPage } from "../pages/login/login";
 import { HomePage } from "../pages/home/home";
 import { Page1 } from '../pages/page1/page1';
 import { Page2 } from '../pages/page2/page2';
+import { ToolsPage } from "../pages/tools/tools";
 
 // Services
 import { OdooRPCService } from "angular2-odoo-jsonrpc";
@@ -27,6 +28,7 @@ import { MenuPipe } from "../pipes/menu";
         HomePage,
         Page1,
         Page2,
+        ToolsPage,
         // Pipes
         MenuPipe
     ],
@@ -39,6 +41,7 @@ import { MenuPipe } from "../pipes/menu";
         MyApp,
         LoginPage,
         HomePage,
+        ToolsPage,
         Page1,
         Page2
     ],

+ 1 - 16
src/pages/home/home.ts

@@ -1,8 +1,5 @@
 import { Component } from '@angular/core';
 import { NavController, NavParams, MenuController } from 'ionic-angular';
-import { SyncService } from "../../services/sync-service";
-
-import "rxjs/add/operator/count";
 
 @Component({
 	selector: 'page-home',
@@ -13,22 +10,10 @@ export class HomePage {
 	constructor(
 		public navCtrl: NavController,
 		public navParams: NavParams,
-		public menuCtrl: MenuController,
-		public sync: SyncService
+		public menuCtrl: MenuController
 	) { }
 
   	ionViewDidLoad() {
-		console.log('ionViewDidLoad HomePage');
 		this.menuCtrl.enable(true);	
-
-		this.sync.do().subscribe(result => { 
-			// result.count().subscribe(x => {
-			// 	console.log(x);
-				
-			// });
-			console.log(result);
-			
-			
-		}, error => console.error(error));	
   	}
 }

+ 23 - 10
src/pages/tools/tools.html

@@ -1,18 +1,31 @@
-<!--
-  Generated template for the Tools page.
-
-  See http://ionicframework.com/docs/v2/components/#navigation for more info on
-  Ionic pages and navigation.
--->
 <ion-header>
 
-  <ion-navbar>
-    <ion-title>tools</ion-title>
-  </ion-navbar>
+     <ion-navbar>
+        <button ion-button menuToggle>
+            <ion-icon name="menu"></ion-icon>
+        </button>
 
-</ion-header>
+        <ion-title>Herramientas</ion-title>
+    </ion-navbar>
 
+</ion-header>
 
 <ion-content padding>
+    
+    <ion-card>
+        <button ion-item (click)="askIfSyncData()">
+            <ion-icon name="sync" item-left></ion-icon>
+            <h2>Sincronizar datos</h2>
+            <p>Sincronice sus datos con su cuenta de Odoo</p>
+        </button>
+    </ion-card>
+
+    <ion-card>
+        <button ion-item (click)="askIfDeleteAccount()">
+            <ion-icon name="walk" item-left></ion-icon>
+            <h2>Eliminar cuenta</h2>
+            <p>Elimine su cuenta de Odoo guardada en la aplicación incluyendo sus datos</p>
+        </button>
+    </ion-card>
 
 </ion-content>

+ 8 - 0
src/pages/tools/tools.scss

@@ -1,3 +1,11 @@
 page-tools {
+    
+    h2 {
+        padding-top: 10px;
+    }
 
+    p {
+        word-break: keep-all;
+        white-space: normal;
+    }
 }

+ 112 - 12
src/pages/tools/tools.ts

@@ -1,22 +1,122 @@
 import { Component } from '@angular/core';
-import { NavController, NavParams } from 'ionic-angular';
+import { NavController, NavParams, AlertController, LoadingController, ToastController } from 'ionic-angular';
 
-/*
-  Generated class for the Tools page.
+import { LoginPage } from "../login/login";
+
+import { SyncService } from "../../services/sync-service";
+import { PouchService } from "../../services/pouch-service";
 
-  See http://ionicframework.com/docs/v2/components/#navigation for more info on
-  Ionic pages and navigation.
-*/
 @Component({
-  selector: 'page-tools',
-  templateUrl: 'tools.html'
+    selector: 'page-tools',
+    templateUrl: 'tools.html'
 })
 export class ToolsPage {
 
-  constructor(public navCtrl: NavController, public navParams: NavParams) {}
+    constructor(
+        public navCtrl: NavController,
+        public navParams: NavParams,
+        public alertCtrl: AlertController,
+        public loadingCtrl: LoadingController,
+        public toastCtrl: ToastController,
+        public sync: SyncService,
+        public pouch: PouchService
+    ) { }
+
+    /**
+     *
+     */
+    ionViewDidLoad() {
+        console.log('ionViewDidLoad ToolsPage');
+    }
+
+    /**
+     *
+     */
+    askIfSyncData(): void {
+        this.alertCtrl.create({
+            title: "Confirmar",
+            message: "Desea sincronizar sus datos ahora?",
+            buttons: [
+                {
+                    text: "Cancelar"
+                },
+                {
+                    text: "Aceptar",
+                    handler: () => {
+                        this.syncData();
+                    }
+                }
+            ]
+        }).present();
+    }
+
+    /**
+     *
+     */
+    syncData(): void {
+        let loader = this.loadingCtrl.create({
+            content: "Actualizando datos, espere..."
+        });
+        loader.present();
+
+        let toast = this.toastCtrl.create({
+            duration: 3000
+        });
+
+        this.sync.do().subscribe(s => {
+            toast.setMessage("Datos actualizados correctamente");
+        }, e => {
+            console.log(e);
+
+            toast.setMessage("No se pudo actualizar los datos");
+        }, () => {
+            loader.dismiss();
+            toast.present();
+        });
+    }
+
+    /**
+     *
+     */
+    askIfDeleteAccount() {
+         this.alertCtrl.create({
+            title: "Confirmar",
+            message: "Desea eliminar su cuenta?",
+            buttons: [
+                {
+                    text: "Cancelar"
+                },
+                {
+                    text: "Aceptar",
+                    handler: () => {
+                        this.deleteAccount();
+                    }
+                }
+            ]
+        }).present();
+    }
 
-  ionViewDidLoad() {
-    console.log('ionViewDidLoad ToolsPage');
-  }
+     /**
+     *
+     */
+    deleteAccount() {
+        let loader = this.loadingCtrl.create({
+            content: "Eliminando cuenta, espere..."
+        });
+        loader.present();
 
+        let toast = this.toastCtrl.create({
+            duration: 3000
+        });
+        
+        this.pouch.destroyAndInitialize().subscribe(s => { 
+            localStorage.clear();
+            this.navCtrl.setRoot(LoginPage);
+        }, e => {
+            toast.setMessage("No se pudo eliminar su cuenta");
+        }, () => {
+            loader.dismiss();
+            toast.present();
+        });
+    }
 }

+ 3 - 2
src/services/pouch-service.ts

@@ -8,6 +8,7 @@ import * as PouchFind from "pouchdb-find";
 import "rxjs/add/observable/throw";
 import "rxjs/add/observable/fromPromise";
 import "rxjs/add/observable/from";
+import "rxjs/add/operator/concat";
 import "rxjs/add/operator/concatMap";
 
 PouchDB.plugin(PouchFind);
@@ -24,7 +25,7 @@ export class PouchService {
     /**
      *
      */
-    private initialize(): Observable<any> {
+    private initialize(destroy?: boolean): Observable<any> {
         this.pouchDB = new PouchDB("odoo", {
             auto_compaction: true
         });
@@ -103,6 +104,6 @@ export class PouchService {
      *
      */
     destroyAndInitialize() {
-        return this.destroy().concat(this.initialize());
+        return this.destroy().concatMap(response => this.initialize(response.ok));
     }
 }

+ 2 - 2
src/services/sync-service.ts

@@ -116,11 +116,11 @@ export class SyncService {
         delete data.type;
         delete data.remote_id;
 
-        if (data.doc_state === "created") {
+        if (data.doc_state === DocStatus.Created) {
             return this.odoo.create(model, data);
         }
         
-        if (data.doc_state === "updated") {
+        if (data.doc_state === DocStatus.Updated) {
             return this.odoo.write(model, [[id], data]);
         }