From a77cb579f9b12be9003af1b811f5b77ea45c66be Mon Sep 17 00:00:00 2001 From: andrzejewsky Date: Fri, 15 Nov 2024 15:18:00 +0100 Subject: [PATCH] Gracefull handler --- .changeset/giant-knives-tell.md | 5 +++++ locale/defaultMessages.json | 4 ++++ src/hooks/makeMutation.ts | 2 +- src/products/views/ProductVariantCreate.tsx | 17 ++++++++++++++++- src/products/views/messages.ts | 5 +++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/giant-knives-tell.md diff --git a/.changeset/giant-knives-tell.md b/.changeset/giant-knives-tell.md new file mode 100644 index 00000000000..7401a579b36 --- /dev/null +++ b/.changeset/giant-knives-tell.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Variant creation no longer reports an error when API call fails, this means this scenario is now handled gracefully. diff --git a/locale/defaultMessages.json b/locale/defaultMessages.json index 18aea1d296e..8cf67d81405 100644 --- a/locale/defaultMessages.json +++ b/locale/defaultMessages.json @@ -2264,6 +2264,10 @@ "DGCzal": { "string": "This token gives you access to your shop's API, which you'll find here: {url}" }, + "DGWVA9": { + "context": "variant created error message", + "string": "Variant creation failed" + }, "DHBlFi": { "context": "navigator customer mode description", "string": "Search Customers" diff --git a/src/hooks/makeMutation.ts b/src/hooks/makeMutation.ts index 242244493ea..259fd389efb 100644 --- a/src/hooks/makeMutation.ts +++ b/src/hooks/makeMutation.ts @@ -55,7 +55,7 @@ export function useMutation( }, onError: (err: ApolloError) => { if (!disableErrorHandling) { - if (err?.graphQLErrors.length > 0) { + if (err?.graphQLErrors?.length > 0) { if (hasError(err, GqlErrors.ReadOnlyException)) { notify({ status: "error", diff --git a/src/products/views/ProductVariantCreate.tsx b/src/products/views/ProductVariantCreate.tsx index 905f9ebe0b5..7ff341d4a08 100644 --- a/src/products/views/ProductVariantCreate.tsx +++ b/src/products/views/ProductVariantCreate.tsx @@ -75,6 +75,15 @@ export const ProductVariant: React.FC = ({ productId, onCompleted: data => { const variantId = data.productVariantCreate.productVariant.id; + if (!variantId) { + notify({ + status: "error", + text: intl.formatMessage(messages.variantCreatedError), + }); + + return; + } + notify({ status: "success", text: intl.formatMessage(messages.variantCreatedSuccess), @@ -98,6 +107,7 @@ export const ProductVariant: React.FC = ({ productId, formData.attributesWithNewFileValue, uploadFilesResult, ); + const variantCreateResult = await variantCreate({ variables: { input: { @@ -136,7 +146,12 @@ export const ProductVariant: React.FC = ({ productId, return { id: null, errors: variantCreateResultErrors }; } - const id = variantCreateResult.data.productVariantCreate.productVariant.id; + const id = variantCreateResult.data?.productVariantCreate?.productVariant?.id; + + if (!id) { + return { id: null, errors: [] }; + } + const updateChannelsResult = await updateChannels({ variables: { id, diff --git a/src/products/views/messages.ts b/src/products/views/messages.ts index 2d73096916c..515d6047040 100644 --- a/src/products/views/messages.ts +++ b/src/products/views/messages.ts @@ -6,4 +6,9 @@ export const variantCreateMessages = defineMessages({ defaultMessage: "Variant created", description: "variant created success message", }, + variantCreatedError: { + id: "DGWVA9", + defaultMessage: "Variant creation failed", + description: "variant created error message", + }, });