Skip to content

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

Add addAtIndex and move functions

dominik-zeglenbe21b1b56229
diff --git a/saleor/static/dashboard-next/utils/lists/__snapshots__/lists.test.ts.snap b/saleor/static/dashboard-next/utils/lists/__snapshots__/lists.test.ts.snap
index 93c5d4c066..f13400ca69 100644
--- a/saleor/static/dashboard-next/utils/lists/__snapshots__/lists.test.ts.snap
+++ b/saleor/static/dashboard-next/utils/lists/__snapshots__/lists.test.ts.snap
@@ -9,6 +9,23 @@ Array [
]
`;
+exports[`Properly calculates output arrays Adds at index 1`] = `
+Array [
+ "lorem",
+ "ipsum",
+ "sit",
+ "dolor",
+]
+`;
+
+exports[`Properly calculates output arrays Moves 1`] = `
+Array [
+ "ipsum",
+ "lorem",
+ "dolor",
+]
+`;
+
exports[`Properly calculates output arrays Removes 1`] = `
Array [
"lorem",
diff --git a/saleor/static/dashboard-next/utils/lists/lists.test.ts b/saleor/static/dashboard-next/utils/lists/lists.test.ts
index 32caf71319..c15e70690e 100644
--- a/saleor/static/dashboard-next/utils/lists/lists.test.ts
+++ b/saleor/static/dashboard-next/utils/lists/lists.test.ts
@@ -1,4 +1,4 @@
-import { add, isSelected, remove, toggle } from "./lists";
+import { add, addAtIndex, isSelected, move, remove, toggle } from "./lists";
const initialArray = ["lorem", "ipsum", "dolor"];
@@ -7,6 +7,10 @@ describe("Properly calculates output arrays", () => {
expect(add("sit", initialArray)).toMatchSnapshot();
});
+ it("Adds at index", () => {
+ expect(addAtIndex("sit", initialArray, 2)).toMatchSnapshot();
+ });
+
it("Removes", () => {
expect(remove("ipsum", initialArray, (a, b) => a === b)).toMatchSnapshot();
});
@@ -20,4 +24,8 @@ describe("Properly calculates output arrays", () => {
expect(toggle("lorem", initialArray, (a, b) => a === b)).toMatchSnapshot();
expect(toggle("sit", initialArray, (a, b) => a === b)).toMatchSnapshot();
});
+
+ it("Moves", () => {
+ expect(move("lorem", initialArray, (a, b) => a === b, 1)).toMatchSnapshot();
+ });
});
diff --git a/saleor/static/dashboard-next/utils/lists/lists.ts b/saleor/static/dashboard-next/utils/lists/lists.ts
index b1c2517932..4986136aa2 100644
--- a/saleor/static/dashboard-next/utils/lists/lists.ts
+++ b/saleor/static/dashboard-next/utils/lists/lists.ts
@@ -13,6 +13,23 @@ export function add<TData>(data: TData, list: List<TData>) {
return [...list, data];
}
+export function addAtIndex<TData>(
+ data: TData,
+ list: List<TData>,
+ index: number
+) {
+ return [...list.slice(0, index), data, ...list.slice(index)];
+}
+
+export function move<TData>(
+ data: TData,
+ list: List<TData>,
+ compare: Compare<TData>,
+ index: number
+) {
+ return addAtIndex(data, remove(data, list, compare), index);
+}
+
export function remove<TData>(
data: TData,
list: List<TData>,