Edition.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <LayoutContainer>
  3. <UiLoadingPanel v-if="pending" />
  4. <UiForm
  5. v-else
  6. :model="model"
  7. :entity="entity"
  8. :submitActions="submitActions"
  9. >
  10. <template #form.button>
  11. <v-btn v-if="goBackRoute" class="theme-neutral mr-3" @click="quit">
  12. {{ $t('cancel') }}
  13. </v-btn>
  14. </template>
  15. <slot v-bind="{ model, entity }" />
  16. </UiForm>
  17. </LayoutContainer>
  18. </template>
  19. <script setup lang="ts">
  20. import type { PropType } from '@vue/runtime-core'
  21. import ApiModel from '~/models/ApiModel'
  22. import type { AnyJson } from '~/types/data'
  23. import { SUBMIT_TYPE } from '~/types/enum/enums'
  24. import { useRoute } from 'vue-router'
  25. import type { RouteLocationRaw} from 'vue-router'
  26. import { useEntityFetch } from '~/composables/data/useEntityFetch'
  27. import type {AsyncData} from "#app";
  28. const props = defineProps({
  29. /**
  30. * Classe de l'ApiModel (ex: Organization, Notification, ...)
  31. */
  32. model: {
  33. type: Function as any as () => typeof ApiModel,
  34. required: true,
  35. },
  36. /**
  37. * Id de l'objet
  38. * Si non renseigné, le component essaiera de l'extraire de la route actuelle
  39. */
  40. id: {
  41. type: Number as PropType<number | null>,
  42. required: false,
  43. default: null,
  44. },
  45. /**
  46. * Route de retour
  47. */
  48. goBackRoute: {
  49. type: Object as PropType<RouteLocationRaw | null>,
  50. required: false,
  51. default: null,
  52. },
  53. /**
  54. * La validation est en cours
  55. */
  56. validationPending: {
  57. type: Boolean,
  58. required: false,
  59. default: false,
  60. },
  61. /**
  62. * Faut-il rafraichir le profil à la soumission du formulaire ?
  63. */
  64. refreshProfile: {
  65. type: Boolean,
  66. required: false,
  67. default: false,
  68. },
  69. })
  70. const { fetch } = useEntityFetch()
  71. const route = useRoute()
  72. const router = useRouter()
  73. const entityId =
  74. props.id !== null ? props.id : parseInt(route.params.id as string)
  75. const { data: entity, pending } = fetch(props.model, entityId) as AsyncData<ApiModel, Error | null>
  76. const submitActions = computed(() => {
  77. let actions: AnyJson = {}
  78. if (props.goBackRoute !== null) {
  79. actions[SUBMIT_TYPE.SAVE_AND_BACK] = props.goBackRoute
  80. } else {
  81. actions[SUBMIT_TYPE.SAVE] = null
  82. }
  83. return actions
  84. })
  85. const quit = () => {
  86. if (!props.goBackRoute) {
  87. throw Error('no go back route defined')
  88. }
  89. router.push(props.goBackRoute)
  90. }
  91. </script>
  92. <style scoped lang="scss"></style>