| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import {repositoryHelper} from "~/services/store/repository";
- import {QUERY_TYPE} from "~/types/enums";
- import {computed} from '@nuxtjs/composition-api'
- import {accessState, AnyJson, AnyStore} from "~/types/interfaces";
- import DataPersister from "~/services/dataPersister/dataPersister";
- import {MyProfile} from "~/models/Access/MyProfile";
- /**
- * @category Use/updater
- * @class UseMyProfileUpdater
- * Use Classe pour la gestion REST de MyProfile
- */
- export class UseMyProfileUpdater{
- private store!:AnyStore
- private $dataPersister!:DataPersister
- private myProfile!:MyProfile
- constructor(store:AnyStore, $dataPersister:DataPersister){
- this.store = store
- this.$dataPersister = $dataPersister
- }
- /**
- * Composition function
- */
- public invoke(): AnyJson{
- this.myProfile = this.createNewMyProfileInstance(this.store.state.profile.access)
- const activityYear = computed(()=>this.myProfile.activityYear)
- const historical = computed(()=>this.myProfile.historical)
- return {
- updateMyProfile: () => this.updateMyProfile(),
- setActivityYear: (activityYear:number) => this.setActivityYear(activityYear),
- setHistorical: (historicalChoices:Array<string>) => this.setHistorical(historicalChoices),
- activityYear,
- historical
- }
- }
- /**
- * Créer une nouvelle instance MyProfile
- * @param myProfile
- */
- private createNewMyProfileInstance(myProfile:accessState): MyProfile{
- const myProfileInstance = repositoryHelper.createNewModelInstance(MyProfile) as MyProfile
- myProfileInstance.id = myProfile.id
- myProfileInstance.activityYear = myProfile.activityYear
- myProfileInstance.historical = myProfile.historical
- repositoryHelper.persist(MyProfile, myProfileInstance)
- return myProfileInstance
- }
- /**
- * Mets à jour l'activity de my profile
- * @param activityYear
- */
- private setActivityYear(activityYear:number){
- if(activityYear <= 0)
- throw new Error('year must be positive')
- repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, activityYear, 'activityYear')
- }
- /**
- * Mets à jour l'historical de my profile
- * @param historicalChoices
- */
- private setHistorical(historicalChoices:Array<string>){
- repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, this.getHistoricalEntry(historicalChoices), 'historical')
- }
- /**
- * Transform les choix de l'historique en objet JSON reconnaissable coté API
- * @param historicalChoices
- */
- private getHistoricalEntry(historicalChoices:Array<string>){
- const historicalDefault:any = {'past':false, 'future':false, 'present':false}
- for(const historicalChoice of historicalChoices){
- historicalDefault[historicalChoice] = true
- }
- return historicalDefault;
- }
- /**
- * Effectue la mise à jour (coté API) de MyProfile
- */
- private async updateMyProfile(): Promise<any>{
- await this.$dataPersister.invoke({
- type: QUERY_TYPE.MODEL,
- model: MyProfile,
- id: this.myProfile.id
- })
- }
- }
- export const $useMyProfileUpdater = (store:AnyStore, $dataPersister:DataPersister) => new UseMyProfileUpdater(store, $dataPersister).invoke()
|