useMyProfile.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {computed, ComputedRef} from '@nuxtjs/composition-api'
  2. import { Item, Model } from '@vuex-orm/core'
  3. import { repositoryHelper } from '~/services/store/repository'
  4. import { QUERY_TYPE } from '~/types/enums'
  5. import { Historical } from '~/types/interfaces'
  6. import { MyProfile } from '~/models/Access/MyProfile'
  7. import { $accessProfile } from '@/services/profile/accessProfile'
  8. import { useContext } from '@nuxtjs/composition-api'
  9. /**
  10. * Composable function
  11. */
  12. export function useMyProfile(){
  13. const {$dataPersister, store} = useContext()
  14. $accessProfile.setStore(store)
  15. const currentAccessId = $accessProfile.getCurrentAccessId()
  16. const myProfile = getMyProfileInstance(currentAccessId) as MyProfile
  17. const activityYear:ComputedRef<number> = computed(() => myProfile.activityYear)
  18. const historical:ComputedRef<Historical> = computed(() => myProfile.historical)
  19. /**
  20. * Effectue la mise à jour (coté API) de MyProfile
  21. */
  22. async function updateMyProfile (): Promise<any> {
  23. await $dataPersister.invoke({
  24. type: QUERY_TYPE.MODEL,
  25. model: MyProfile,
  26. id: myProfile.id
  27. })
  28. }
  29. /**
  30. * Mets à jour l'activity de my profile
  31. * @param activityYear
  32. */
  33. function setActivityYear (activityYear:number) {
  34. if (activityYear <= 0) { throw new Error('year must be positive') }
  35. repositoryHelper.updateStoreFromField(MyProfile, myProfile, activityYear, 'activityYear')
  36. }
  37. /**
  38. * Mets à jour l'historical de my profile
  39. * @param historicalChoices
  40. */
  41. function setHistorical (historicalChoices:Array<string>) {
  42. repositoryHelper.updateStoreFromField(MyProfile, myProfile, getHistoricalEntry(historicalChoices), 'historical')
  43. }
  44. /**
  45. * Mets à jour l'historical de my profile
  46. * @param dates
  47. */
  48. function setHistoricalRange (dates:Array<string>) {
  49. repositoryHelper.updateStoreFromField(MyProfile, myProfile, getHistoricalRangeEntry(dates), 'historical')
  50. }
  51. return {
  52. currentAccessId,
  53. activityYear,
  54. historical,
  55. updateMyProfile,
  56. setActivityYear,
  57. setHistorical,
  58. setHistoricalRange
  59. }
  60. }
  61. /**
  62. * récupère l'instance MyProfile
  63. * @param myProfileId
  64. */
  65. function getMyProfileInstance (myProfileId:any): Item<Model> {
  66. return repositoryHelper.findItemFromModel(MyProfile, parseInt(myProfileId))
  67. }
  68. /**
  69. * Transform les choix de l'historique en objet JSON reconnaissable coté API
  70. * @param historicalChoices
  71. */
  72. function getHistoricalEntry (historicalChoices:Array<string>) {
  73. const historicalDefault:any = { past: false, future: false, present: false }
  74. for (const historicalChoice of historicalChoices) {
  75. historicalDefault[historicalChoice] = true
  76. }
  77. return historicalDefault
  78. }
  79. /**
  80. * Transforme le choix des période en Objet JSON reconnaissable coté API
  81. * @param dates
  82. */
  83. function getHistoricalRangeEntry (dates:Array<string>) {
  84. return { past: false, future: false, present: false, dateStart: dates[0], dateEnd: dates[1] }
  85. }
  86. /**
  87. * Const servant à assurer les tests des fonctions non exportées
  88. */
  89. export const _exportedForTesting = {
  90. getMyProfileInstance,
  91. getHistoricalEntry,
  92. getHistoricalRangeEntry
  93. }