Skip to content

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

Add updateAtIndex and removeAtIndex

dominik-zeglen443c8073cf06
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 f13400ca69..998d4c2e46 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
@@ -33,6 +33,13 @@ Array [
]
`;
+exports[`Properly calculates output arrays Removes at index 1`] = `
+Array [
+ "lorem",
+ "dolor",
+]
+`;
+
exports[`Properly calculates output arrays Toggles 1`] = `
Array [
"ipsum",
@@ -48,3 +55,11 @@ Array [
"sit",
]
`;
+
+exports[`Properly calculates output arrays Updates at index 1`] = `
+Array [
+ "lorem",
+ "amet",
+ "dolor",
+]
+`;
diff --git a/saleor/static/dashboard-next/utils/lists/lists.test.ts b/saleor/static/dashboard-next/utils/lists/lists.test.ts
index c15e70690e..dc6976b1c9 100644
--- a/saleor/static/dashboard-next/utils/lists/lists.test.ts
+++ b/saleor/static/dashboard-next/utils/lists/lists.test.ts
@@ -1,4 +1,13 @@
-import { add, addAtIndex, isSelected, move, remove, toggle } from "./lists";
+import {
+ add,
+ addAtIndex,
+ isSelected,
+ move,
+ remove,
+ removeAtIndex,
+ toggle,
+ updateAtIndex
+} from "./lists";
const initialArray = ["lorem", "ipsum", "dolor"];
@@ -11,10 +20,18 @@ describe("Properly calculates output arrays", () => {
expect(addAtIndex("sit", initialArray, 2)).toMatchSnapshot();
});
+ it("Updates at index", () => {
+ expect(updateAtIndex("amet", initialArray, 1)).toMatchSnapshot();
+ });
+
it("Removes", () => {
expect(remove("ipsum", initialArray, (a, b) => a === b)).toMatchSnapshot();
});
+ it("Removes at index", () => {
+ expect(removeAtIndex(initialArray, 1)).toMatchSnapshot();
+ });
+
it("Matches", () => {
expect(isSelected("lorem", initialArray, (a, b) => a === b)).toBe(true);
expect(isSelected("sit", initialArray, (a, b) => a === b)).toBe(false);
diff --git a/saleor/static/dashboard-next/utils/lists/lists.ts b/saleor/static/dashboard-next/utils/lists/lists.ts
index 4986136aa2..21005baccf 100644
--- a/saleor/static/dashboard-next/utils/lists/lists.ts
+++ b/saleor/static/dashboard-next/utils/lists/lists.ts
@@ -30,6 +30,14 @@ export function move<TData>(
return addAtIndex(data, remove(data, list, compare), index);
}
+export function updateAtIndex<TData>(
+ data: TData,
+ list: List<TData>,
+ index: number
+) {
+ return addAtIndex(data, removeAtIndex(list, index), index);
+}
+
export function remove<TData>(
data: TData,
list: List<TData>,
@@ -38,6 +46,10 @@ export function remove<TData>(
return list.filter(listElement => !compare(listElement, data));
}
+export function removeAtIndex<TData>(list: List<TData>, index: number) {
+ return [...list.slice(0, index), ...list.slice(index + 1)];
+}
+
export function toggle<TData>(
data: TData,
list: List<TData>,