소스 검색

refactor hooks, add refreshProfile hook

Olivier Massot 3 년 전
부모
커밋
e8a1829276

+ 3 - 2
services/data/baseDataManager.ts

@@ -40,9 +40,10 @@ abstract class BaseDataManager extends Hookable implements DataManager {
     const queryArguments = { ...this.defaultArguments, ...args }
     BaseDataManager.startLoading(queryArguments)
 
-    await this.triggerHooks(queryArguments)
+    const response = await this._invoke(queryArguments)
 
-    return await this._invoke(queryArguments)
+    await this.triggerHooks(queryArguments)
+    return response
   }
 
   /**

+ 5 - 0
services/data/dataPersister.ts

@@ -61,6 +61,11 @@ class DataPersister extends BaseDataManager {
     }
     return await dataProvider.process(deserializedResponse, dataProviderArgs)
   }
+
+  public async fetchProfile() {
+    console.log('refresh the current profile with updated data')
+    await this.ctx.store.dispatch('refreshUserProfile')
+  }
 }
 
 export default DataPersister

+ 1 - 1
services/data/hookable.ts

@@ -14,7 +14,7 @@ abstract class Hookable {
   protected async triggerHooks (args: UrlArgs) {
     for (const Hook of this.sortedHooks()) {
       if (Hook.support(args)) {
-        await new Hook().invoke(args)
+        await new Hook(this).invoke(args)
       }
     }
   }

+ 6 - 0
services/data/hooks/baseHook.ts

@@ -1,7 +1,13 @@
 import { DataProviderArgs } from '~/types/interfaces'
+import Hookable from "~/services/data/hookable";
 
 abstract class BaseHook {
   public static priority = 255
+  protected parent: Hookable;
+
+  constructor(parent: Hookable) {
+    this.parent = parent
+  }
 
   // eslint-disable-next-line require-await
   async invoke (_args: DataProviderArgs): Promise<any> {

+ 1 - 0
services/data/hooks/hookDeleter/hookDeleterExample.ts

@@ -8,6 +8,7 @@ class HookDeleterExample extends BaseHook implements HookDeleter {
   async invoke (args: DataDeleterArgs): Promise<any> {
     // eslint-disable-next-line no-console
     await console.log('This is a deleter hook')
+    // don't forget to include this class ins the _import.ts file
   }
 
   static support (_args: DataDeleterArgs): boolean {

+ 3 - 1
services/data/hooks/hookPersister/_import.ts

@@ -1,5 +1,7 @@
 import HookPersisterExample from '~/services/data/hooks/hookPersister/hookPersisterExample'
+import PostPersistProfileRefresh from "~/services/data/hooks/hookPersister/postPersistProfileRefresh";
 
 export const hooksPersister = [
-  HookPersisterExample
+  HookPersisterExample,
+  PostPersistProfileRefresh
 ]

+ 1 - 0
services/data/hooks/hookPersister/hookPersisterExample.ts

@@ -7,6 +7,7 @@ class HookPersisterExample extends BaseHook implements HookPersister {
   async invoke (_args: DataPersisterArgs): Promise<any> {
     // eslint-disable-next-line no-console
     await console.log('This is a persister hook')
+    // don't forget to include this class ins the _import.ts file
   }
 
   static support (_args: DataPersisterArgs): boolean {

+ 8 - 3
services/data/hooks/hookPersister/postPersistProfileRefresh.ts

@@ -3,7 +3,11 @@ import BaseHook from '~/services/data/hooks/baseHook'
 import {Parameters} from "~/models/Organization/Parameters";
 import DataPersister from "~/services/data/dataPersister";
 
-class ParametersPostPersist extends BaseHook implements HookPersister {
+/**
+ * Access profile (and its embed organization profile) shall be re-fetched after some
+ * data updates
+ */
+class PostPersistProfileRefresh extends BaseHook implements HookPersister {
   public static  priority = 10
 
   async invoke (_args: DataPersisterArgs): Promise<any> {
@@ -11,8 +15,9 @@ class ParametersPostPersist extends BaseHook implements HookPersister {
   }
 
   static support (_args: DataPersisterArgs): boolean {
-    return (typeof _args.model !== 'undefined') && _args.model.entity === 'parameters'
+    return (typeof _args.model !== 'undefined') &&
+      ['accesses', 'organizations', 'parameters', 'subdomains'].includes(_args.model.entity)
   }
 }
 
-export default ParametersPostPersist
+export default PostPersistProfileRefresh

+ 1 - 0
services/data/hooks/hookProvider/hookProviderExample.ts

@@ -7,6 +7,7 @@ class HookProviderExample extends BaseHook implements HookProvider {
   async invoke (_args: DataProviderArgs): Promise<any> {
     // eslint-disable-next-line no-console
     await console.log('This is a provider hook')
+    // don't forget to include this class ins the _import.ts file
   }
 
   static support (_args: DataProviderArgs): boolean {

+ 14 - 0
store/index.js

@@ -51,5 +51,19 @@ export const actions = {
       url: '/api/my_profile'
     })
     await dispatch('profile/access/setProfile', myProfile.data)
+  },
+
+  /**
+   * Met à jour les informations du profil connecté, par exemple après une mise à jour des paramètres
+   * @param dispatch
+   * @param state
+   * @return {Promise<void>}
+   */
+  async refreshUserProfile ({ dispatch }) {
+    const myProfile = await this.app.context.$dataProvider.invoke({
+      type: QUERY_TYPE.DEFAULT,
+      url: '/api/my_profile'
+    })
+    await dispatch('profile/access/refreshProfile', myProfile.data)
   }
 }

+ 9 - 0
store/profile/access.ts

@@ -174,6 +174,15 @@ export const actions = {
 
     context.dispatch('createNewMyProfileVUexOrmInstance', profile)
   },
+  refreshProfile (context: any, profile: any) {
+    context.commit('setName', profile.name)
+    context.commit('setGivenName', profile.givenName)
+    context.commit('setGender', profile.gender)
+    context.commit('setAvatarId', profile.avatarId)
+    context.commit('setActivityYear', profile.activityYear)
+
+    context.dispatch('profile/organization/refreshProfile', profile.organization, { root: true })
+  },
   setMultiAccesses (context: any, organizations: any) {
     _.each(organizations, (organization:baseOrganizationState) => {
       const o: baseOrganizationState = {

+ 6 - 0
store/profile/organization.ts

@@ -80,5 +80,11 @@ export const actions = {
       }
       context.commit('addParent', p)
     })
+  },
+  refreshProfile (context: any, profile: any) {
+    context.commit('setName', profile.name)
+    context.commit('setCurrentActivityYear', profile.currentYear)
+    context.commit('setWebsite', profile.website)
+    context.commit('setLegalStatus', profile.legalStatus)
   }
 }