Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add optimistic response to attribute reorder

dominik-zeglenf35c42662192
diff --git a/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx b/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx
index a84814042e..6289d416be 100644
--- a/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx
+++ b/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx
@@ -1,5 +1,10 @@
import React from "react";
+import { MutationFn } from "react-apollo";
+import {
+ AttributeReorderInput,
+ AttributeTypeEnum
+} from "@saleor/types/globalTypes";
import { getMutationProviderData } from "../../misc";
import { PartialMutationProviderOutput } from "../../types";
import {
@@ -21,6 +26,10 @@ import {
ProductTypeDelete,
ProductTypeDeleteVariables
} from "../types/ProductTypeDelete";
+import {
+ ProductTypeDetailsFragment,
+ ProductTypeDetailsFragment_productAttributes
+} from "../types/ProductTypeDetailsFragment";
import {
ProductTypeUpdate,
ProductTypeUpdateVariables
@@ -30,6 +39,29 @@ import {
UnassignAttributeVariables
} from "../types/UnassignAttribute";
+function moveAttribute(
+ attributes:
+ | ProductTypeDetailsFragment_productAttributes[]
+ | ProductTypeDetailsFragment_productAttributes[],
+ move: AttributeReorderInput
+) {
+ const attributeIndex = attributes.findIndex(
+ attribute => attribute.id === move.id
+ );
+ const newIndex = attributeIndex + move.sortOrder;
+
+ const attributesWithoutMovedOne = [
+ ...attributes.slice(0, attributeIndex),
+ ...attributes.slice(attributeIndex + 1)
+ ];
+
+ return [
+ ...attributesWithoutMovedOne.slice(0, newIndex),
+ attributes[attributeIndex],
+ ...attributesWithoutMovedOne.slice(newIndex)
+ ];
+}
+
interface ProductTypeOperationsProps {
children: (props: {
assignAttribute: PartialMutationProviderOutput<
@@ -53,6 +85,7 @@ interface ProductTypeOperationsProps {
ProductTypeUpdateVariables
>;
}) => React.ReactNode;
+ productType: ProductTypeDetailsFragment;
onAssignAttribute: (data: AssignAttribute) => void;
onUnassignAttribute: (data: UnassignAttribute) => void;
onProductTypeAttributeReorder: (data: ProductTypeAttributeReorder) => void;
@@ -64,6 +97,7 @@ const ProductTypeOperations: React.StatelessComponent<
ProductTypeOperationsProps
> = ({
children,
+ productType,
onAssignAttribute,
onUnassignAttribute,
onProductTypeAttributeReorder,
@@ -84,8 +118,46 @@ const ProductTypeOperations: React.StatelessComponent<
<ProductTypeAttributeReorderMutation
onCompleted={onProductTypeAttributeReorder}
>
- {(...reorderAttribute) =>
- children({
+ {(
+ reorderAttributeMutation,
+ reorderAttributeMutationResult
+ ) => {
+ const reorderAttributeMutationFn: MutationFn<
+ ProductTypeAttributeReorder,
+ ProductTypeAttributeReorderVariables
+ > = opts => {
+ const optimisticResponse = {
+ productTypeReorderAttributes: {
+ __typename: "ProductTypeReorderAttributes" as "ProductTypeReorderAttributes",
+ errors: [],
+ productType: {
+ ...productType,
+ productAttributes:
+ opts.variables.type ===
+ AttributeTypeEnum.PRODUCT
+ ? moveAttribute(
+ productType.productAttributes,
+ opts.variables.move
+ )
+ : productType.productAttributes,
+ variantAttributes:
+ opts.variables.type ===
+ AttributeTypeEnum.VARIANT
+ ? moveAttribute(
+ productType.variantAttributes,
+ opts.variables.move
+ )
+ : productType.variantAttributes
+ }
+ }
+ };
+ return reorderAttributeMutation({
+ ...opts,
+ optimisticResponse
+ });
+ };
+
+ return children({
assignAttribute: getMutationProviderData(
...assignAttribute
),
@@ -93,7 +165,8 @@ const ProductTypeOperations: React.StatelessComponent<
...deleteProductType
),
reorderAttribute: getMutationProviderData(
- ...reorderAttribute
+ reorderAttributeMutationFn,
+ reorderAttributeMutationResult
),
unassignAttribute: getMutationProviderData(
...unassignAttribute
@@ -101,8 +174,8 @@ const ProductTypeOperations: React.StatelessComponent<
updateProductType: getMutationProviderData(
...updateProductType
)
- })
- }
+ });
+ }}
</ProductTypeAttributeReorderMutation>
)}
</TypedUnassignAttributeMutation>
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx
index 1f8e4b9ef5..16d63ff81c 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx
@@ -120,6 +120,7 @@ export const ProductTypeUpdate: React.FC<ProductTypeUpdateProps> = ({
return (
<ProductTypeOperations
+ productType={maybe(() => data.productType)}
onAssignAttribute={handleAttributeAssignSuccess}
onUnassignAttribute={handleAttributeUnassignSuccess}
onProductTypeDelete={handleProductTypeDeleteSuccess}