From f6a21bf37ef56ecab88652b1449da63f73b9648a Mon Sep 17 00:00:00 2001
From: Mila <Mila@pollex.com.tw>
Date: 星期二, 16 十一月 2021 20:59:28 +0800
Subject: [PATCH] update 我的顧問清單: 使用 store 存放 consultantList 以及新增顧問、移除顧問的狀態控制

---
 PAMapp/pages/index.vue |   56 ++++++++++++++------------------------------------------
 1 files changed, 14 insertions(+), 42 deletions(-)

diff --git a/PAMapp/pages/index.vue b/PAMapp/pages/index.vue
index a77e07b..9ec3c7c 100644
--- a/PAMapp/pages/index.vue
+++ b/PAMapp/pages/index.vue
@@ -28,7 +28,6 @@
             </el-row>
             <ConsultantList
                 :agents="consultantList.slice(0, 3)"
-                @removeAgent="removeAgent"
             ></ConsultantList>
             <div class='pam-recommend pb-30 pt-30'>
                 <h5 class="mdTxt">��靽憿批��</h5>
@@ -40,11 +39,8 @@
 </template>
 
 <script lang="ts">
-import { Vue, Component, State, Action } from 'nuxt-property-decorator';
-import { getFavoriteFromStorage, setFavoriteToStorage } from '~/assets/ts/storageConsultant';
-import { addFavoriteConsultant, Consultants, deleteConsultant } from '~/assets/ts/api/consultant';
-import { login, getFavoriteConsultant } from '~/assets/ts/api/consultant';
-import { isLogin } from '~/assets/ts/auth';
+import { Vue, Component, State, Action, Watch } from 'nuxt-property-decorator';
+import { Consultants } from '~/assets/ts/api/consultant';
 
 @Component({
     layout: 'home'
@@ -55,52 +51,28 @@
     @State('recommendList') recommendList!: Consultants[];
     @Action storeRecommendList!: any;
 
+    @State('myConsultantList') myConsultantList!: Consultants[];
+    @Action storeConsultantList!: any;
+
+    @Watch('myConsultantList')
+    onMyConsultantListChange() {
+        this.consultantList = (this.myConsultantList || [])
+                .filter(item => item.contactStatus !== 'contacted')
+                .sort((a, b) => a.updateTime > b.updateTime ? -1 : 1)
+    }
+
     mounted() {
-        if (!this.recommendList) {
+        if (!this.recommendList?.length) {
             this.storeRecommendList();
         }
 
-        if (isLogin()) {
-            this.addFavoriteFromStorageToApi();
-        } else {
-            this.consultantList = getFavoriteFromStorage();
-        }
-    }
-
-    getMyConsutant() {
-        getFavoriteConsultant().then((response) => {
-            this.consultantList = response.data
-                .filter(item => item.contactStatus !== 'contacted')
-                .sort((a, b) => a.updateTime > b.updateTime ? -1 : 1)
-        });
-    }
-
-    addFavoriteFromStorageToApi() {
-        const agentNoList = getFavoriteFromStorage().map(i => i.agentNo)
-        if (agentNoList.length > 0) {
-            addFavoriteConsultant(agentNoList).then(res => this.getMyConsutant());
-            localStorage.removeItem('favoriteConsultant');
-            return;
-        }
-        this.getMyConsutant();
+        this.storeConsultantList();
     }
 
     routerPush(path: string) {
         this.$router.push(path);
     }
 
-    removeAgent(agentNo: string) {
-
-        if (!isLogin()) {
-            const findIndex = this.consultantList.findIndex((item, i) => {
-                return item.agentNo === agentNo;
-            })
-            this.consultantList.splice(findIndex, 1);
-            setFavoriteToStorage(this.consultantList)
-        } else {
-            deleteConsultant(agentNo).then(res => this.$router.go(0))
-        }
-    }
 }
 
 </script>

--
Gitblit v1.8.0