| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import { computed, ComputedRef } from '@nuxtjs/composition-api'
- import { Item, Model } from '@vuex-orm/core'
- import { repositoryHelper } from '~/services/store/repository'
- import { QUERY_TYPE } from '~/types/enums'
- import { AnyJson, AnyStore, Historical } from '~/types/interfaces'
- import DataPersister from '~/services/data/dataPersister'
- import { MyProfile } from '~/models/Access/MyProfile'
- import { $accessProfile } from '@/services/profile/accessProfile'
- import { useContext } from '@nuxtjs/composition-api'
- /**
- * @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 {
- const {$ability} = useContext()
- const currentAccessId = $accessProfile(this.store).getCurrentAccessId()
- this.myProfile = this.getMyProfileInstance(currentAccessId) as MyProfile
- 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
- }
- }
- /**
- * récupère l'instance MyProfile
- * @param myProfileId
- */
- private getMyProfileInstance (myProfileId:any): Item<Model> {
- return repositoryHelper.findItemFromModel(MyProfile, parseInt(myProfileId))
- }
- /**
- * 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()
|