Edition.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <LayoutContainer>
  3. <UiLoadingPanel v-if="pending" />
  4. <UiForm v-else v-model="entity" :submit-actions="submitActions">
  5. <template #form.button>
  6. <v-btn v-if="goBackRoute" class="theme-neutral mr-3" @click="quit">
  7. {{ $t('cancel') }}
  8. </v-btn>
  9. </template>
  10. <slot v-bind="{ model, entity }" />
  11. </UiForm>
  12. </LayoutContainer>
  13. </template>
  14. <script setup lang="ts" generic="T extends typeof ApiModel">
  15. import { useRoute } from 'vue-router'
  16. import type { RouteLocationRaw } from 'vue-router'
  17. import type ApiModel from '~/models/ApiModel'
  18. import type { AnyJson } from '~/types/data'
  19. import { SUBMIT_TYPE } from '~/types/enum/enums'
  20. import { useEntityFetch } from '~/composables/data/useEntityFetch'
  21. const props = withDefaults(defineProps<{
  22. model: T
  23. /**
  24. * Id de l'objet
  25. * Si non renseigné, le composant essaiera de l'extraire de la route actuelle
  26. */
  27. id?: number | null
  28. /**
  29. * Route de retour
  30. */
  31. goBackRoute?: RouteLocationRaw | null
  32. /**
  33. * La validation est en cours
  34. */
  35. validationPending?: boolean
  36. /**
  37. * Faut-il rafraîchir le profil à la soumission du formulaire ?
  38. */
  39. refreshProfile?: boolean
  40. }>(), {
  41. id: null,
  42. goBackRoute: null,
  43. validationPending: false,
  44. refreshProfile: false
  45. })
  46. const { fetch } = useEntityFetch()
  47. const route = useRoute()
  48. const router = useRouter()
  49. const entityId =
  50. props.id !== null ? props.id : parseInt(route.params.id as string)
  51. const { data: entity, pending } = fetch(props.model, entityId)
  52. const submitActions = computed(() => {
  53. const actions: AnyJson = {}
  54. if (props.goBackRoute !== null) {
  55. actions[SUBMIT_TYPE.SAVE_AND_BACK] = props.goBackRoute
  56. } else {
  57. actions[SUBMIT_TYPE.SAVE] = null
  58. }
  59. return actions
  60. })
  61. const quit = () => {
  62. if (!props.goBackRoute) {
  63. throw new Error('no go back route defined')
  64. }
  65. router.push(props.goBackRoute)
  66. }
  67. </script>
  68. <style scoped lang="scss"></style>