Skip to content

Commit

Permalink
fix: prevent same index drag to start request and restore state on error
Browse files Browse the repository at this point in the history
  • Loading branch information
steff456 committed Dec 8, 2023
1 parent 4bb85ca commit 26a9cba
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/course-outline/CourseOutline.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const CourseOutline = ({ courseId }) => {
document.title = getPageHeadTitle(courseName, intl.formatMessage(messages.headingTitle));
const [sections, setSections] = useState(sectionsList);

const initialSections = [...sections];

const {
isShow: isShowProcessingNotification,
title: processingNotificationTitle,
Expand All @@ -98,8 +100,10 @@ const CourseOutline = ({ courseId }) => {
}, []);

const finalizeSectionOrder = () => {
handleDragNDrop(sections.map((section) => section.id));
}
handleDragNDrop(sections.map((section) => section.id), () => {
setSections(() => initialSections);
});
};

useEffect(() => {
if (sectionsList) {
Expand Down
3 changes: 2 additions & 1 deletion src/course-outline/data/thunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export function addNewCourseSectionQuery(courseBlockId) {
};
}

export function setSectionOrderListQuery(courseId, newListId) {
export function setSectionOrderListQuery(courseId, newListId, restoreCallback) {
return async (dispatch) => {
dispatch(updateSavingStatus({ status: RequestStatus.PENDING }));
dispatch(showProcessingNotification(NOTIFICATION_MESSAGES.saving));
Expand All @@ -277,6 +277,7 @@ export function setSectionOrderListQuery(courseId, newListId) {
} catch (error) {
dispatch(hideProcessingNotification());
dispatch(updateSavingStatus({ status: RequestStatus.FAILED }));
restoreCallback();
}
};
}
4 changes: 2 additions & 2 deletions src/course-outline/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ const useCourseOutline = ({ courseId }) => {
dispatch(duplicateCourseSectionQuery(currentSection.id, courseStructure.id));
};

const handleDragNDrop = (newListId) => {
dispatch(setSectionOrderListQuery(courseId, newListId));
const handleDragNDrop = (newListId, restoreCallback) => {
dispatch(setSectionOrderListQuery(courseId, newListId, restoreCallback));
};

useEffect(() => {
Expand Down
11 changes: 8 additions & 3 deletions src/course-outline/section-card/SectionCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,19 @@ const SectionCard = ({
item.index = hoverIndex;
},
});

const indexCopy = index;
const [{ isDragging }, drag] = useDrag({
type: ItemTypes.SECTION,
item: () => ({ id, index }),
item: () => ({ id, index, startingIndex: indexCopy }),
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),
end: () => {
finalizeSectionOrder();
end: (item) => {
const { startingIndex } = item;
if (index !== startingIndex) {
finalizeSectionOrder();
}
},
});
const opacity = isDragging ? 0 : 1;
Expand Down

0 comments on commit 26a9cba

Please sign in to comment.