prevent lag effect when changing order

This commit is contained in:
grimhilt 2023-07-31 01:57:00 +02:00
parent 6697844239
commit ca2262403b

View File

@ -39,6 +39,7 @@ const Content = ({ form, playlistId }) => {
}; };
const changePositionValue = (from, to) => { const changePositionValue = (from, to) => {
return new Promise((resolve, reject) => {
const formFiles = form.values.files; const formFiles = form.values.files;
let below_position = to === 0 ? 0 : formFiles[to].position; let below_position = to === 0 ? 0 : formFiles[to].position;
let above_position = formFiles[to].position; let above_position = formFiles[to].position;
@ -56,17 +57,18 @@ const Content = ({ form, playlistId }) => {
// sending modification to server // sending modification to server
API.playlistChangeOrder(playlistId, { file_id: formFiles[from].id, position: newPosition }) API.playlistChangeOrder(playlistId, { file_id: formFiles[from].id, position: newPosition })
.then((res) => { .then((res) => {
if (res.status !== 200) { if (res.status == 200) {
form.values.files[from].position = newPosition; form.values.files[from].position = newPosition;
return true; resolve(true);
} else { } else {
setNotification(true, `Error when changing order (${res.status})`); setNotification(true, `Error when changing order (${res.status})`);
return false; resolve(false);
} }
}) })
.catch((err) => { .catch((err) => {
setNotification(true, err.message); setNotification(true, err.message);
return false; resolve(false);
});
}); });
}; };
@ -115,9 +117,10 @@ const Content = ({ form, playlistId }) => {
<Box mx="auto"> <Box mx="auto">
<DragDropContext <DragDropContext
onDragEnd={({ destination, source }) => { onDragEnd={({ destination, source }) => {
changePositionValue(source.index, destination.index).then((success) => {
if (success) {
form.reorderListItem('files', { from: source.index, to: destination.index }); form.reorderListItem('files', { from: source.index, to: destination.index });
changePositionValue(source.index, destination.index).then((success) => {
if (!success) {
form.reorderListItem('files', { from: destination.index, to: source.index });
} }
}); });
}} }}