Browse Source

simplify the em.delete method and add validate to delete and save

Olivier Massot 1 year ago
parent
commit
37a7ef49b3

+ 5 - 10
components/Ui/Button/Delete.vue

@@ -29,20 +29,15 @@ Bouton Delete avec modale de confirmation de la suppression
 </template>
 
 <script setup lang="ts">
+import type { Ref, PropType } from 'vue'
 import { TYPE_ALERT } from '~/types/enum/enums'
-import type { Ref } from '@vue/reactivity'
 import { useEntityManager } from '~/composables/data/useEntityManager'
 import ApiResource from '~/models/ApiResource'
 import { usePageStore } from '~/stores/page'
-import ApiModel from '~/models/ApiModel'
 
 const props = defineProps({
-  model: {
-    type: Function as any as () => typeof ApiModel,
-    required: true,
-  },
   entity: {
-    type: Object as () => ApiResource,
+    type: Object as PropType<ApiResource>,
     required: true,
   },
   flat: {
@@ -58,10 +53,10 @@ const { em } = useEntityManager()
 
 const deleteItem = async () => {
   try {
-    //@ts-ignore
-    await em.delete(props.model, props.entity)
+    await em.delete(props.entity)
     usePageStore().addAlert(TYPE_ALERT.SUCCESS, ['deleteSuccess'])
-  } catch (error: any) {
+  } catch (error) {
+    // @ts-expect-error error is supposed to have a message prop
     usePageStore().addAlert(TYPE_ALERT.ALERT, [error.message])
     throw error
   }

+ 1 - 1
components/Ui/Card.vue

@@ -24,7 +24,7 @@ Container de type Card
         </NuxtLink>
       </v-btn>
 
-      <UiButtonDelete v-if="withDeleteAction" :model="model" :entity="entity" />
+      <UiButtonDelete v-if="withDeleteAction" :entity="entity" />
 
       <slot name="card.action" />
     </v-card-actions>

+ 0 - 1
pages/parameters/attendances.vue

@@ -63,7 +63,6 @@
                 @click="goToEditPage(reason.id as number)"
               />
               <UiButtonDelete
-                :model="AttendanceBookingReason"
                 :entity="reason"
                 :flat="true"
                 class="cycle-edit-icon"

+ 0 - 1
pages/parameters/education_timings/index.vue

@@ -22,7 +22,6 @@
                 @click="goToEditPage(timing.id as number)"
               />
               <UiButtonDelete
-                :model="EducationTiming"
                 :entity="timing"
                 :flat="true"
                 class="cycle-edit-icon"

+ 0 - 1
pages/parameters/residence_areas/index.vue

@@ -25,7 +25,6 @@
                 @click="goToEditPage(residenceArea.id as number)"
               />
               <UiButtonDelete
-                :model="ResidenceArea"
                 :entity="residenceArea"
                 :flat="true"
                 class="cycle-edit-icon"

+ 9 - 1
services/data/entityManager.ts

@@ -143,6 +143,8 @@ class EntityManager {
   public save(instance: ApiResource, permanent: boolean = false): ApiResource {
     const model = instance.constructor as typeof ApiResource
 
+    this.validateEntity(instance)
+
     if (permanent) {
       this.saveInitialState(model, instance)
     }
@@ -323,9 +325,15 @@ class EntityManager {
    * @param model
    * @param instance
    */
-  public async delete(model: typeof ApiModel, instance: ApiResource) {
+  public async delete(instance: ApiResource) {
+    const model = instance.constructor as typeof ApiModel
+
+    this.validateEntity(instance)
+
     const repository = this.getRepository(model)
 
+    this.validateEntity(instance)
+
     // If object has been persisted to the datasource, send a delete request
     if (!instance.isNew()) {
       const url = UrlUtils.join('api', model.entity, String(instance.id))