瀏覽代碼

refactoring

Vincent GUFFON 4 年之前
父節點
當前提交
ed80b7cdfb
共有 4 個文件被更改,包括 23 次插入19 次删除
  1. 7 13
      components/Ui/Button/Delete.vue
  2. 10 2
      plugins/Data/axios.js
  3. 3 4
      plugins/route.ts
  4. 3 0
      services/connection/urlBuilder.ts

+ 7 - 13
components/Ui/Button/Delete.vue

@@ -33,9 +33,10 @@ Bouton Delete avec modale de confirmation de la suppression
 </template>
 
 <script lang="ts">
-import { defineComponent, useContext, ref, Ref } from '@nuxtjs/composition-api'
-import { DataDeleterArgs, alert } from '~/types/interfaces'
-import { TYPE_ALERT } from '~/types/enums'
+import {defineComponent, ref, Ref, useContext} from '@nuxtjs/composition-api'
+import {alert, DataDeleterArgs} from '~/types/interfaces'
+import {TYPE_ALERT} from '~/types/enums'
+import Page from "~/services/store/page";
 
 export default defineComponent({
   props: {
@@ -47,21 +48,14 @@ export default defineComponent({
   setup (props) {
     const { $dataDeleter, store, app: { i18n } } = useContext()
     const showDialog: Ref<boolean> = ref(false)
+    const page = new Page(store)
 
     const deleteItem = async () => {
       try {
         await $dataDeleter.invoke(props.deleteArgs)
-        const alert: alert = {
-          type: TYPE_ALERT.SUCCESS,
-          message: i18n.t('deleteSuccess') as string
-        }
-        store.commit('page/setAlert', alert)
+        page.addAlerts(TYPE_ALERT.SUCCESS, [i18n.t('deleteSuccess') as string])
       } catch (error) {
-        const alert: alert = {
-          type: TYPE_ALERT.ALERT,
-          message: error.message
-        }
-        store.commit('page/setAlert', alert)
+        page.addAlerts(TYPE_ALERT.ALERT, [error.message])
       }
       showDialog.value = false
     }

+ 10 - 2
plugins/Data/axios.js

@@ -1,3 +1,6 @@
+import {TYPE_ALERT} from "~/types/enums";
+import Page from "~/services/store/page";
+
 export default function ({ $axios, redirect, store }) {
   $axios.onRequest(config => {
     if(!config.params.noXaccessId){
@@ -20,12 +23,17 @@ export default function ({ $axios, redirect, store }) {
   })
 
   $axios.onError((error) => {
+
     // In case of unauthorized, redirect to a specific page
-    if (error.statusCode === 401) {
+    if (error.response.status === 401) {
       redirect('/login')
     }
-    if (error.statusCode === 403) {
+    if (error.response.status === 403) {
       console.debug('forbidden')
     }
+
+    if (error.response.status === 500) {
+      new Page(store).addAlerts(TYPE_ALERT.ALERT, [error.response.data['hydra:description']])
+    }
   })
 }

+ 3 - 4
plugins/route.ts

@@ -1,12 +1,11 @@
 import { Plugin } from '@nuxt/types'
+import Form from "~/services/store/form";
 
 const routePlugin: Plugin = (ctx) => {
   if (ctx.app.router) {
     ctx.app.router.beforeEach((to, from, next) => {
-      if (ctx.store.state.form.dirty) {
-        ctx.store.commit('form/setShowConfirmToLeave', true)
-        ctx.store.commit('form/setGoAfterLeave', to)
-      } else {
+      new Form(ctx.store).handleActionsAfterLeavePage(to)
+      if (!ctx.store.state.form.dirty) {
         next()
       }
     })

+ 3 - 0
services/connection/urlBuilder.ts

@@ -20,6 +20,9 @@ class UrlBuilder {
     let url: string = ''
     switch (args.type) {
       case QUERY_TYPE.DEFAULT:
+        if (!args.url) {
+          throw new Error('*args* has no url')
+        }
         url = args.url
         break;