fixed dragging and dropping (#2219)

This commit is contained in:
Erik Jan de Wit 2022-03-10 16:16:53 +01:00 committed by GitHub
parent c72a9bbf06
commit bc365b03b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -161,9 +161,12 @@ export const UserCredentials = ({ user }: UserCredentialsProps) => {
const itemOrder = useMemo(
() =>
groupedUserCredentials.map(({ value }) =>
value.map(({ id }) => id).toString()
),
groupedUserCredentials.flatMap((groupedCredential) => [
groupedCredential.value.map(({ id }) => id).toString(),
...(groupedCredential.isExpanded
? groupedCredential.value.map((c) => c.id!)
: []),
]),
[groupedUserCredentials]
);
@ -286,26 +289,25 @@ export const UserCredentials = ({ user }: UserCredentialsProps) => {
};
const onDragFinish = async (dragged: string, newOrder: string[]) => {
dragged = dragged.split(",")[0];
const keys = groupedUserCredentials.map(({ value }) =>
value.map(({ id }) => id)
);
const oldIndex = keys.findIndex((el) => el.join().includes(dragged));
const newIndex = newOrder.findIndex((el) => el.includes(dragged));
const oldIndex = itemOrder.findIndex((key) => key === dragged);
const newIndex = newOrder.findIndex((key) => key === dragged);
const times = newIndex - oldIndex;
const ids = dragged.split(",");
try {
for (const id of ids)
for (let index = 0; index < Math.abs(times); index++) {
if (times > 0) {
await adminClient.users.moveCredentialPositionDown({
id: user.id!,
credentialId: dragged,
newPreviousCredentialId: `${keys[newIndex][0]}`,
credentialId: id,
newPreviousCredentialId: itemOrder[newIndex],
});
} else {
await adminClient.users.moveCredentialPositionUp({
id: user.id!,
credentialId: dragged,
credentialId: id,
});
}
}