Skip to content

Commit

Permalink
Make Graphql and GraphqlMutation errorCallback the same (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Sep 10, 2024
1 parent 097c851 commit 5fa3e63
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
8 changes: 3 additions & 5 deletions resources/js/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Vue.prototype.getCheckoutStep = (stepName) => {
return (config.checkout_steps[config.store_code] ?? config.checkout_steps['default'])?.indexOf(stepName)
}

Vue.prototype.updateCart = async function (data, response) {
Vue.prototype.updateCart = async function (variables, response) {
if (!response?.data) {
return response?.data
}
Expand All @@ -22,11 +22,9 @@ Vue.prototype.updateCart = async function (data, response) {
return response.data
}

Vue.prototype.checkResponseForExpiredCart = async function (error) {
let responseData = await error.response?.json()

Vue.prototype.checkResponseForExpiredCart = async function (variables, response) {
if (
responseData?.errors?.some(
response?.errors?.some(
(error) =>
error.extensions.category === 'graphql-no-such-entity' &&
error.path.some((path) =>
Expand Down
6 changes: 3 additions & 3 deletions resources/js/components/Graphql.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
},
errorCallback: {
type: Function,
default: (error) => Notify(window.config.translations.errors.wrong, 'warning'),
default: (variables, error) => Notify(window.config.translations.errors.wrong, 'warning'),
},
store: {
type: String,
Expand Down Expand Up @@ -82,14 +82,14 @@ export default {
}
}
this.data = this.callback ? await this.callback(this.data, response) : response.data
this.data = this.callback ? await this.callback(this.variables, response) : response.data
if (this.cache) {
useLocalStorage(this.cachePrefix + this.cache, null, { serializer: StorageSerializers.object }).value = this.data
}
} catch (error) {
console.error(error)
this.errorCallback(error)
this.errorCallback(this.variables, await error?.response?.json())
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/GraphqlMutation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default {
throw error
}
const errorResponse = error.response.json()
const errorResponse = await error.response.json()
if (this.errorCallback) {
await this.errorCallback(this.data, errorResponse)
}
Expand Down
5 changes: 3 additions & 2 deletions resources/js/components/Product/AddToCart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ export default {
}
if (error?.response) {
if (!(await this.checkResponseForExpiredCart(error.response)) && GraphQLError.prototype.isPrototypeOf(error)) {
const responseData = await error.response.json()
if (GraphQLError.prototype.isPrototypeOf(error) && !(await this.checkResponseForExpiredCart({}, responseData))) {
// If there are errors we may still get a newly updated cart back.
await this.updateCart({}, await error.response.json())
await this.updateCart({}, responseData)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion resources/js/stores/useCart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StorageSerializers, asyncComputed, useLocalStorage, useMemoize } from '@vueuse/core'
import { computed, watch } from 'vue'
import { GraphQLError } from '../fetch'
import { mask, clearMask } from './useMask'

const cartStorage = useLocalStorage('cart', {}, { serializer: StorageSerializers.object })
Expand Down Expand Up @@ -27,7 +28,7 @@ export const refresh = async function (force = false) {
cart.value = Object.values(response.data)[0]
} catch (error) {
console.error(error)
Vue.prototype.checkResponseForExpiredCart(error)
GraphQLError.prototype.isPrototypeOf(error) && Vue.prototype.checkResponseForExpiredCart({}, await error?.response?.json())
}
}

Expand Down

0 comments on commit 5fa3e63

Please sign in to comment.