Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful exception handler when variant create mutation fails #5257

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-knives-tell.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions locale/defaultMessages.json
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion src/hooks/makeMutation.ts
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ export function useMutation<TData, TVariables>(
},
onError: (err: ApolloError) => {
if (!disableErrorHandling) {
if (err?.graphQLErrors.length > 0) {
if (err?.graphQLErrors?.length > 0) {
if (hasError(err, GqlErrors.ReadOnlyException)) {
notify({
status: "error",
17 changes: 16 additions & 1 deletion src/products/views/ProductVariantCreate.tsx
Original file line number Diff line number Diff line change
@@ -75,6 +75,15 @@ export const ProductVariant: React.FC<ProductVariantCreateProps> = ({ 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<ProductVariantCreateProps> = ({ productId,
formData.attributesWithNewFileValue,
uploadFilesResult,
);

const variantCreateResult = await variantCreate({
variables: {
input: {
@@ -136,7 +146,12 @@ export const ProductVariant: React.FC<ProductVariantCreateProps> = ({ 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,
5 changes: 5 additions & 0 deletions src/products/views/messages.ts
Original file line number Diff line number Diff line change
@@ -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",
},
});