| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import {repositoryHelper} from "~/services/store/repository";
- import {QUERY_TYPE} from "~/types/enums";
- import {computed, ComputedRef} from '@nuxtjs/composition-api'
- import {accessState, AnyJson, AnyStore, Historical} 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:ComputedRef<number> = computed(()=>this.myProfile.activityYear)
- const historical:ComputedRef<Historical> = computed(()=>this.myProfile.historical)
- return {
- updateMyProfile: () => this.updateMyProfile(),
- setActivityYear: (activityYear:number) => this.setActivityYear(activityYear),
- setHistorical: (historicalChoices:Array<string>) => this.setHistorical(historicalChoices),
- setHistoricalRange: (dates:Array<string>) => this.setHistoricalRange(dates),
- 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')
- }
- /**
- * Mets à jour l'historical de my profile
- * @param dates
- */
- private setHistoricalRange(dates:Array<string>){
- repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, this.getHistoricalRangeEntry(dates), '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;
- }
- /**
- * Trasnforme le choix des période en Objet JSON reconnaissable coté API
- * @param dates
- */
- private getHistoricalRangeEntry(dates:Array<string>){
- return {'past':false, 'future':false, 'present':false, dateStart:dates[0], dateEnd:dates[1]}
- }
- /**
- * 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()
|