Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move optimistic response logic to providers
dominik-zeglen85fd6db3b012
diff --git a/saleor/static/dashboard-next/products/containers/ProductImagesReorder.tsx b/saleor/static/dashboard-next/products/containers/ProductImagesReorder.tsxindex 0ff70c8ae7..d455205c66 100644--- a/saleor/static/dashboard-next/products/containers/ProductImagesReorder.tsx+++ b/saleor/static/dashboard-next/products/containers/ProductImagesReorder.tsx@@ -16,6 +16,10 @@ import { productDetailsQuery } from "../queries";interface ProductImagesReorderProviderPropsextends PartialMutationProviderProps<ProductImageReorderMutation> {productId: string;+ productImages: Array<{+ id: string;+ url: string;+ }>;children: PartialMutationProviderRenderProps<ProductImageReorderMutation,ProductImageReorderMutationVariables@@ -24,15 +28,15 @@ interface ProductImagesReorderProviderPropsconst ProductImagesReorderProvider: React.StatelessComponent<ProductImagesReorderProviderProps-> = ({ productId, children, onError, onSuccess }) => (+> = props => (<TypedProductImagesReordermutation={productImagesReorder}- onCompleted={onSuccess}- onError={onError}+ onCompleted={props.onSuccess}+ onError={props.onError}update={(cache, { data: { productImageReorder } }) => {const data: ProductDetailsQuery = cache.readQuery({query: productDetailsQuery,- variables: { id: productId }+ variables: { id: props.productId }});data.product.images.edges.forEach((item, index, array) => {array[index].node = productImageReorder.productImages[index];@@ -41,11 +45,32 @@ const ProductImagesReorderProvider: React.StatelessComponent<}}>{(mutate, { data, error, loading }) =>- children({+ props.children({data,error,loading,- mutate+ mutate: opts => {+ const productImagesMap = props.productImages.reduce((prev, curr) => {+ prev[curr.id] = curr;+ return prev;+ }, {});+ const productImages = opts.variables.imagesIds.map((id, index) => ({+ __typename: "ProductImage",+ ...productImagesMap[id],+ sortOrder: index+ }));+ const optimisticResponse = {+ productImageReorder: {+ __typename: "ProductImageReorder",+ errors: null,+ productImages+ }+ };+ return mutate({+ optimisticResponse,+ variables: opts.variables+ });+ }})}</TypedProductImagesReorder>diff --git a/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx b/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsxindex 1dfa1960c2..a636a78eb3 100644--- a/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx+++ b/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx@@ -133,7 +133,21 @@ const ProductImageDeleteProvider: React.StatelessComponent<data,error,loading,- mutate+ mutate: opts => {+ const optimisticResponse = {+ productImageDelete: {+ __typename: "ProductImageDelete",+ productImage: {+ __typename: "ProductImage",+ id: opts.variables.id+ }+ }+ };+ return mutate({+ optimisticResponse,+ variables: opts.variables+ });+ }})}</TypedProductImageDeleteMutation>@@ -189,6 +203,11 @@ const ProductUpdateOperations: React.StatelessComponent<{updateProduct => (<ProductImagesReorderProviderproductId={productId}+ productImages={+ product && product.images && product.images.edges+ ? product.images.edges.map(edge => edge.node)+ : []+ }onError={onError}onSuccess={onImageReorder}>@@ -226,21 +245,8 @@ const ProductUpdateOperations: React.StatelessComponent<deleteProductImage: {data: deleteProductImage.data,loading: deleteProductImage.loading,- mutate: variables => {- const optimisticResponse = {- productImageDelete: {- __typename: "ProductImageDelete",- productImage: {- __typename: "ProductImage",- id: variables.id- }- }- };- deleteProductImage.mutate({- optimisticResponse,- variables- });- }+ mutate: variables =>+ deleteProductImage.mutate({ variables })},errors:updateProduct &&@@ -251,31 +257,8 @@ const ProductUpdateOperations: React.StatelessComponent<reorderProductImages: {data: reorderProductImages.data,loading: reorderProductImages.loading,- mutate: variables => {- const imagesMap = {};- product.images.edges.forEach(edge => {- const image = edge.node;- imagesMap[image.id] = image;- });- const productImages = variables.imagesIds.map(- (id, index) => ({- __typename: "ProductImage",- ...imagesMap[id],- sortOrder: index- })- );- const optimisticResponse = {- productImageReorder: {- __typename: "ProductImageReorder",- errors: null,- productImages- }- };- reorderProductImages.mutate({- optimisticResponse,- variables- });- }+ mutate: variables =>+ reorderProductImages.mutate({ variables })},updateProduct: {data: updateProduct.data,