浏览代码

fix bug with AutocompleteAccesses on form submit

Olivier Massot 10 月之前
父节点
当前提交
8e891a3709

+ 9 - 17
components/Ui/Form.vue

@@ -37,7 +37,7 @@ de quitter si des données ont été modifiées.
       <div v-else class="mt-12" />
 
       <!-- Content -->
-      <slot v-bind="{ model, entity }" />
+      <slot v-bind="{ modelValue }" />
 
       <!-- Bottom action bar -->
       <v-container
@@ -105,16 +105,9 @@ import { useRefreshProfile } from '~/composables/data/useRefreshProfile'
 
 const props = defineProps({
   /**
-   * Classe de l'ApiModel (ex: Organization, Notification, ...)
+   * Instance de l'ApiModel
    */
-  model: {
-    type: Function as any as () => typeof ApiModel,
-    required: true,
-  },
-  /**
-   * Instance de l'objet
-   */
-  entity: {
+  modelValue: {
     type: Object as () => ApiModel,
     required: true,
   },
@@ -209,7 +202,7 @@ const closeConfirmationDialog = () => {
   formStore.setShowConfirmToLeave(false)
 }
 
-const emit = defineEmits(['update:entity'])
+const emit = defineEmits(['update:model-value'])
 
 
 // ### Actions du formulaire
@@ -234,10 +227,9 @@ const submit = async (next: string | null = null) => {
   try {
     usePageStore().loading = true
 
-    // TODO: est-ce qu'il faut re-fetch l'entité après le persist?
-    const updatedEntity = await em.persist(props.entity)
+    const updatedEntity = await em.persist(props.modelValue)
 
-    emit('update:entity', updatedEntity)
+    emit('update:model-value', updatedEntity)
 
     if (props.refreshProfile) {
       await refreshProfile()
@@ -348,7 +340,7 @@ const cancel = () => {
 
   formStore.setShowConfirmToLeave(false)
 
-  em.reset(props.model, props.entity)
+  em.reset(props.modelValue)
 
   if (requestedLeavingRoute.value !== null) {
     navigateTo(requestedLeavingRoute.value)
@@ -367,7 +359,7 @@ const actions = computed(() => {
  */
 const onFormChange = async () => {
   if (isValid.value) {
-    em.save(props.entity)
+    em.save(props.modelValue)
     setIsDirty(true)
 
     if (props.onChanged) {
@@ -390,7 +382,7 @@ const validate = async function () {
 }
 
 // #### Gestion de l'état dirty
-watch(props.entity, async (newEntity, oldEntity) => {
+watch(props.modelValue, async (newEntity, oldEntity) => {
   setIsDirty(true)
 })
 

+ 1 - 1
components/Ui/Form/Creation.vue

@@ -1,5 +1,5 @@
 <template>
-  <UiForm :model="model" :entity="entity" :submitActions="submitActions">
+  <UiForm v-model="entity" :submitActions="submitActions">
     <template #form.button>
       <v-btn v-if="goBackRoute" class="theme-neutral mr-3" @click="quit">
         {{ $t('cancel') }}

+ 1 - 2
components/Ui/Form/Edition.vue

@@ -3,8 +3,7 @@
     <UiLoadingPanel v-if="pending" />
     <UiForm
       v-else
-      :model="model"
-      :entity="entity"
+      v-model="entity"
       :submitActions="submitActions"
     >
       <template #form.button>

+ 1 - 2
pages/my-settings.vue

@@ -11,8 +11,7 @@ Page 'Mes préférences'
               <UiLoadingPanel v-if="pending" />
               <UiForm
                 v-else
-                :model="Preferences"
-                :entity="preferences"
+                v-model="preferences"
                 action-position="bottom"
               >
                 <v-row>

+ 1 - 3
pages/parameters/attendances.vue

@@ -4,9 +4,7 @@
       <UiLoadingPanel v-if="pending" />
       <UiForm
         v-else-if="parameters !== null"
-        :model="Parameters"
-        :entity="parameters"
-        action-position="bottom"
+        v-model="parameters"
       >
         <v-row>
           <v-col cols="12">

+ 1 - 3
pages/parameters/bulletin.vue

@@ -3,9 +3,7 @@
     <UiLoadingPanel v-if="pending" />
     <UiForm
       v-else
-      :model="Parameters"
-      :entity="parameters"
-      action-position="bottom"
+      v-model="parameters"
     >
       <v-row>
         <v-col cols="12">

+ 1 - 3
pages/parameters/education_notation.vue

@@ -3,9 +3,7 @@
     <UiLoadingPanel v-if="pending" />
     <UiForm
       v-else
-      :model="Parameters"
-      :entity="parameters"
-      action-position="bottom"
+      v-model="parameters"
     >
       <v-row>
         <v-col cols="12">

+ 1 - 3
pages/parameters/general_parameters.vue

@@ -3,9 +3,7 @@
     <UiLoadingPanel v-if="pending" />
     <UiForm
       v-else-if="parameters !== null"
-      :model="Parameters"
-      :entity="parameters"
-      action-position="bottom"
+      v-model="parameters"
     >
       <v-row>
         <v-col cols="12">

+ 1 - 3
pages/parameters/intranet.vue

@@ -3,9 +3,7 @@
     <UiLoadingPanel v-if="pending" />
     <UiForm
       v-else
-      :model="Parameters"
-      :entity="parameters"
-      action-position="bottom"
+      v-model="parameters"
     >
       <v-row>
         <v-col cols="12">

+ 1 - 3
pages/parameters/sms.vue

@@ -2,9 +2,7 @@
   <div>
     <UiForm
       v-if="parameters"
-      :model="Parameters"
-      :entity="parameters"
-      action-position="bottom"
+      v-model="parameters"
     >
       <v-row>
         <v-col cols="12">

+ 1 - 2
pages/parameters/subdomains/new.vue

@@ -3,8 +3,7 @@
     <LayoutContainer>
       <UiForm
         ref="form"
-        :model="Subdomain"
-        :entity="subdomain"
+        v-model="parameters"
         :submit-actions="submitActions"
         :validation-pending="validationPending"
         :refresh-profile="true"

+ 1 - 3
pages/parameters/super_admin.vue

@@ -13,10 +13,8 @@
     <UiForm
       v-else-if="adminAccess"
       ref="form"
-      :model="AdminAccess"
-      :entity="adminAccess"
+      v-model="adminAccess"
       class="w-100"
-      action-position="bottom"
     >
       <v-table class="mb-4">
         <tbody>

+ 1 - 3
pages/parameters/teaching.vue

@@ -3,9 +3,7 @@
     <UiLoadingPanel v-if="pending" />
     <UiForm
       v-else-if="parameters !== null"
-      :model="Parameters"
-      :entity="parameters"
-      action-position="bottom"
+      v-model="parameters"
     >
       <LayoutParametersTable
         :items="tableItems"

+ 2 - 3
pages/parameters/website.vue

@@ -4,8 +4,7 @@
     <UiForm
       v-else-if="parameters !== null"
       :model="Parameters"
-      :entity="parameters"
-      @update:entity="refresh"
+      v-model="parameters"
     >
       <v-row>
         <v-col cols="12">
@@ -158,7 +157,7 @@ if (organizationProfile.parametersId === null) {
   throw new Error('Missing organization parameters id')
 }
 
-const { data: parameters, pending, refresh } = fetch(
+const { data: parameters, pending } = fetch(
   Parameters,
   organizationProfile.parametersId,
 ) as AsyncData<ApiResource | null, Error | null>

+ 10 - 4
services/data/entityManager.ts

@@ -65,6 +65,10 @@ class EntityManager {
     return this.getRepository(model).where((val) => Number.isInteger(val.id))
   }
 
+  public getModel(instance: ApiResource): typeof ApiResource {
+    return instance.constructor as typeof ApiModel
+  }
+
   /**
    * Cast an object as an ApiResource
    * This in used internally to ensure the object is recognized as an ApiResource
@@ -140,7 +144,7 @@ class EntityManager {
    *                  record is also updated.
    */
   public save(instance: ApiResource, permanent: boolean = false): ApiResource {
-    const model = instance.constructor as typeof ApiResource
+    const model = this.getModel(instance)
 
     this.validateEntity(instance)
 
@@ -269,7 +273,7 @@ class EntityManager {
    * @param instance
    */
   public async persist(instance: ApiModel) {
-    const model = instance.constructor as typeof ApiModel
+    const model = this.getModel(instance)
 
     let url = UrlUtils.join('api', model.entity)
     let response
@@ -330,7 +334,7 @@ class EntityManager {
    * @param instance
    */
   public async delete(instance: ApiModel) {
-    const model = instance.constructor as typeof ApiModel
+    const model = this.getModel(instance)
     instance = this.cast(model, instance)
 
     console.log('delete', instance)
@@ -357,7 +361,9 @@ class EntityManager {
    * @param model
    * @param instance
    */
-  public reset(model: typeof ApiResource, instance: ApiResource) {
+  public reset(instance: ApiResource) {
+    const model = this.getModel(instance)
+
     const initialInstance = this.getInitialStateOf(model, instance.id)
     if (initialInstance === null) {
       throw new Error(