Files
my-links/src/utils/array.ts
Sonny ad682faa9e Reorder categories (#12)
* feat: add next category id column

* feat: reorder categories (front)

* refactor: remove dead code & some optimization

* feat(wip): add order column + sql request + trigger

* refactor: fix warnings

* fix: syntax error in migration

* feat: create sql query (category reorder)

* feat: use prisma generated types instead

* feat: create some react context with hooks

* refactor: move a lot of code from home page to
dedicated components

* refactor: extend generated prisma types

* refactor: use hooks and move links footer in dedicated component

* refactor: fix bad types used

* fix: warnings caused by setState loop

* feat: use nextid instead of order column

* fix: reorder categories (front)

* fix: sort categories by nextId

* feat: prevent send update request if cat.id equal target.id

* fix: reorganization applied even if there is no change

* chore: remove unused en var

* feat: check if nextid category exist

* chore: sql query for migration

* refactor: remove useless code and prevent sending request when category not moving

* fix: redorder categories when sending request
2023-12-14 00:15:58 +01:00

40 lines
936 B
TypeScript

import { i18n } from 'next-i18next';
export function groupItemBy(array: any[], property: string) {
const hash = {};
const props = property.split('.');
for (const item of array) {
const key = props.reduce((acc, prop) => acc && acc[prop], item);
const hashKey =
key !== undefined ? key : i18n.t('common:category.categories');
if (!hash[hashKey]) {
hash[hashKey] = [];
}
hash[hashKey].push(item);
}
return hash;
}
// Thanks S/O
export function arrayMove<T>(
arr: T[],
previousIndex: number,
nextIndex: number,
): T[] {
const arrayCopy = [...arr];
const [removedElement] = arrayCopy.splice(previousIndex, 1);
if (nextIndex >= arr.length) {
// Pad the array with undefined elements if needed
const padding = nextIndex - arr.length + 1;
arrayCopy.push(...new Array(padding).fill(undefined));
}
arrayCopy.splice(nextIndex, 0, removedElement);
return arrayCopy;
}