Fix order behavior (#890)
* Fix order behavior * Compare variants by id
This commit is contained in:
parent
2d8fc85ac3
commit
82bbb93d94
1 changed files with 24 additions and 2 deletions
|
@ -9,7 +9,10 @@ import {
|
||||||
ProductChannelListingUpdate,
|
ProductChannelListingUpdate,
|
||||||
ProductChannelListingUpdateVariables
|
ProductChannelListingUpdateVariables
|
||||||
} from "@saleor/products/types/ProductChannelListingUpdate";
|
} from "@saleor/products/types/ProductChannelListingUpdate";
|
||||||
import { ProductDetails_product } from "@saleor/products/types/ProductDetails";
|
import {
|
||||||
|
ProductDetails_product,
|
||||||
|
ProductDetails_product_variants
|
||||||
|
} from "@saleor/products/types/ProductDetails";
|
||||||
import { ProductImageCreateVariables } from "@saleor/products/types/ProductImageCreate";
|
import { ProductImageCreateVariables } from "@saleor/products/types/ProductImageCreate";
|
||||||
import { ProductImageReorderVariables } from "@saleor/products/types/ProductImageReorder";
|
import { ProductImageReorderVariables } from "@saleor/products/types/ProductImageReorder";
|
||||||
import {
|
import {
|
||||||
|
@ -34,6 +37,7 @@ import {
|
||||||
import { mapFormsetStockToStockInput } from "@saleor/products/utils/data";
|
import { mapFormsetStockToStockInput } from "@saleor/products/utils/data";
|
||||||
import { getAvailabilityVariables } from "@saleor/products/utils/handlers";
|
import { getAvailabilityVariables } from "@saleor/products/utils/handlers";
|
||||||
import { ReorderEvent } from "@saleor/types";
|
import { ReorderEvent } from "@saleor/types";
|
||||||
|
import { move } from "@saleor/utils/lists";
|
||||||
import { diff } from "fast-array-diff";
|
import { diff } from "fast-array-diff";
|
||||||
import { MutationFetchResult } from "react-apollo";
|
import { MutationFetchResult } from "react-apollo";
|
||||||
import { arrayMove } from "react-sortable-hoc";
|
import { arrayMove } from "react-sortable-hoc";
|
||||||
|
@ -236,6 +240,13 @@ export function createImageReorderHandler(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function areVariantsEqual(
|
||||||
|
a: ProductDetails_product_variants,
|
||||||
|
b: ProductDetails_product_variants
|
||||||
|
) {
|
||||||
|
return a.id === b.id;
|
||||||
|
}
|
||||||
|
|
||||||
export function createVariantReorderHandler(
|
export function createVariantReorderHandler(
|
||||||
product:
|
product:
|
||||||
| ProductDetails_product
|
| ProductDetails_product
|
||||||
|
@ -244,9 +255,20 @@ export function createVariantReorderHandler(
|
||||||
reorderProductVariants: (variables: ProductVariantReorderVariables) => void
|
reorderProductVariants: (variables: ProductVariantReorderVariables) => void
|
||||||
) {
|
) {
|
||||||
return ({ newIndex, oldIndex }: ReorderEvent) => {
|
return ({ newIndex, oldIndex }: ReorderEvent) => {
|
||||||
|
const oldVariantOrder = [...product.variants];
|
||||||
|
|
||||||
|
product.variants = [
|
||||||
|
...(move(
|
||||||
|
product.variants[oldIndex],
|
||||||
|
product!.variants,
|
||||||
|
areVariantsEqual,
|
||||||
|
newIndex
|
||||||
|
) as ProductDetails_product_variants[])
|
||||||
|
];
|
||||||
|
|
||||||
reorderProductVariants({
|
reorderProductVariants({
|
||||||
move: {
|
move: {
|
||||||
id: product.variants[oldIndex].id,
|
id: oldVariantOrder[oldIndex].id,
|
||||||
sortOrder: newIndex - oldIndex
|
sortOrder: newIndex - oldIndex
|
||||||
},
|
},
|
||||||
productId: product.id
|
productId: product.id
|
||||||
|
|
Loading…
Reference in a new issue