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