ali пре 2 година
родитељ
комит
16f92c27e7

+ 19 - 23
src/boot/axios.js

@@ -26,45 +26,40 @@ const AxiosHooks = (function () {
   function handleErrors (error, router, store) {
     const messages = []
     if (!error || !error.response) {
-      return
+      return error // Reject in case of network errors
     }
+
     const statusCode = parseInt(error.response.status)
+
     if (statusCode >= 500 && statusCode <= 599) {
       messages.push('مشکلی رخ داده است. مجدد تلاش کنید.')
     } else if (statusCode === 404) {
-      if (error.response.data.detail) {
-        messages.push(error.response.data.detail)
-      } else {
-        messages.push('موردی یافت نشد.')
-      }
+      messages.push(error.response.data.detail || 'موردی یافت نشد.')
     } else if (statusCode === 401) {
-      if (error.response.data.detail) {
-        messages.push(error.response.data.detail)
-      }
-      // const message = error.response.data.message
-      // messages.push(message)
+      messages.push(error.response.data.detail || 'Unauthorized access.')
       deAuthorizeUser(router, store)
     } else if (statusCode === 403) {
-      const message = error.response.data.detail
-      messages.push(message)
+      messages.push(error.response.data.detail || 'Forbidden access.')
     } else if (error.response.data) {
+      // Handle other errors by iterating over response data
       if (error.response.data.detail) {
         messages.push(error.response.data.detail)
       } else {
         for (const key of Object.keys(error.response.data)) {
-          if (typeof error.response.data[key] === 'string') {
-            messages.push(key + ': ' + error.response.data[key])
-          } else if (Array.isArray(error.response.data[key])) {
-            error.response.data[key].forEach(message => {
-              messages.push(key + ': ' + message)
-            })
+          const message = error.response.data[key]
+          if (typeof message === 'string') {
+            messages.push(`${key}: ${message}`)
+          } else if (Array.isArray(message)) {
+            message.forEach(msg => messages.push(`${key}: ${msg}`))
           }
         }
       }
     }
 
     toastMessages(messages)
-    return Promise.reject(error)
+
+    // Return the error to be handled by the calling function
+    return error
   }
 
   function handleFulfilled (response) {
@@ -158,9 +153,10 @@ export default boot(({ app, store, router, ssrContext }) => {
   if (appApi.interceptors) {
     appApi.interceptors.response.use(function (response) {
       AxiosHooks.handleFulfilled(response)
-      return Promise.resolve(response)
-    }, async function (error) {
-      return Promise.reject(await AxiosHooks.handleErrors(error, router, store))
+      return response
+    }, function (error) {
+      AxiosHooks.handleErrors(error, router, store)
+      return Promise.reject(error) // Ensure the error is rejected properly
     })
   }
 

+ 72 - 10
src/components/Widgets/ContentCategory/ContentCategoryShow/ContentCategoryShow.vue

@@ -66,7 +66,7 @@
 import { EntityIndex } from 'quasar-crud'
 import { Content } from 'src/models/Content.js'
 import { Product } from 'src/models/Product.js'
-import { mixinWidget } from 'src/mixin/Mixins.js'
+import { mixinWidget, mixinPrefetchServerData } from 'src/mixin/Mixins.js'
 import { APIGateway } from 'src/api/APIGateway.js'
 import { FormBuilderAssist } from 'quasar-form-builder'
 import { ContentCategory } from 'src/models/ContentCategory.js'
@@ -77,7 +77,7 @@ import ContentItem from './ContentItem.vue'
 export default {
   name: 'ContentCategoryShow',
   components: { EntityIndex, Breadcrumbs, productItem, ContentItem },
-  mixins: [mixinWidget],
+  mixins: [mixinWidget, mixinPrefetchServerData],
   data () {
     return {
       productApi: APIGateway.product.APIAdresses.base,
@@ -160,18 +160,49 @@ export default {
       return null
     }
   },
-  created() {
-    this.updateBreadcrumbs()
-    FormBuilderAssist.setAttributeByName(this.productInputs, 'content_category', 'value', this.contentCategoryId)
-    FormBuilderAssist.setAttributeByName(this.contentInputs, 'category', 'value', this.contentCategoryId)
-    if (this.contentCategoryId) {
-      this.loadContentCategory()
-    }
-  },
   mounted () {
+    this.updateBreadcrumbs()
     this.mounted = true
   },
   methods: {
+    prefetchServerDataPromise () {
+      FormBuilderAssist.setAttributeByName(this.productInputs, 'content_category', 'value', this.contentCategoryId)
+      FormBuilderAssist.setAttributeByName(this.contentInputs, 'category', 'value', this.contentCategoryId)
+      if (this.contentCategoryId) {
+        this.contentCategory.loading = true
+        return this.getContentCategoryInfo(this.contentCategoryId)
+      }
+
+      return Promise.resolve(null)
+    },
+    prefetchServerDataPromiseThen (contentCategory) {
+      this.contentCategory = new ContentCategory(contentCategory)
+      this.updateBreadcrumbs()
+      if (this.contentCategory.parent) {
+        this.getContentCategoryInfo(this.contentCategory.parent)
+          .then((parentContentCategory) => {
+            this.contentCategoryParent = new ContentCategory(parentContentCategory)
+            this.updateBreadcrumbs()
+            if (this.contentCategoryParent.parent) {
+              this.getContentCategoryInfo(this.contentCategoryParent.parent)
+                .then((parentParentContentCategory) => {
+                  this.contentCategoryParentParent = new ContentCategory(parentParentContentCategory)
+                  this.updateBreadcrumbs()
+                })
+                .finally(() => {
+                  this.contentCategory.loading = false
+                })
+            }
+          })
+          .finally(() => {
+            this.contentCategory.loading = false
+          })
+      }
+    },
+    prefetchServerDataPromiseCatch () {
+      this.contentCategory.loading = false
+    },
+
     getContent (data) {
       return new Content(data)
     },
@@ -212,6 +243,37 @@ export default {
           this.contentCategory.loading = false
         })
     },
+    loadContentCategory1 () {
+      this.contentCategory.loading = true
+      this.getContentCategoryInfo(this.contentCategoryId)
+        .then((contentCategory) => {
+          this.contentCategory = new ContentCategory(contentCategory)
+          this.updateBreadcrumbs()
+          if (this.contentCategory.parent) {
+            this.getContentCategoryInfo(this.contentCategory.parent)
+              .then((parentContentCategory) => {
+                this.contentCategoryParent = new ContentCategory(parentContentCategory)
+                this.updateBreadcrumbs()
+                if (this.contentCategoryParent.parent) {
+                  this.getContentCategoryInfo(this.contentCategoryParent.parent)
+                    .then((parentParentContentCategory) => {
+                      this.contentCategoryParentParent = new ContentCategory(parentParentContentCategory)
+                      this.updateBreadcrumbs()
+                    })
+                    .finally(() => {
+                      this.contentCategory.loading = false
+                    })
+                }
+              })
+              .finally(() => {
+                this.contentCategory.loading = false
+              })
+          }
+        })
+        .finally(() => {
+          this.contentCategory.loading = false
+        })
+    },
     getContentCategoryInfo (contentCategoryId) {
       return APIGateway.contentCategory.get({ data: { id: contentCategoryId } })
     },