Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reordering of lessons via drag-and-drop on the script edit gui page #37440

Merged
merged 12 commits into from Oct 27, 2020
Merged
3 changes: 0 additions & 3 deletions apps/src/lib/levelbuilder/script-editor/LessonGroupCard.jsx
Expand Up @@ -6,7 +6,6 @@ import ReactDOM from 'react-dom';
import {borderRadius, tokenMargin} from '@cdo/apps/lib/levelbuilder/constants';
import OrderControls from '@cdo/apps/lib/levelbuilder/OrderControls';
import {
moveLesson,
moveGroup,
removeLesson,
addLesson,
Expand Down Expand Up @@ -75,7 +74,6 @@ class LessonGroupCard extends Component {
addLesson: PropTypes.func.isRequired,
moveGroup: PropTypes.func.isRequired,
removeGroup: PropTypes.func.isRequired,
moveLesson: PropTypes.func.isRequired,
removeLesson: PropTypes.func.isRequired,
setLessonGroup: PropTypes.func.isRequired,
reorderLesson: PropTypes.func.isRequired,
Expand Down Expand Up @@ -355,7 +353,6 @@ export const UnconnectedLessonGroupCard = LessonGroupCard;
export default connect(
state => ({}),
{
moveLesson,
moveGroup,
removeLesson,
removeGroup,
Expand Down
59 changes: 4 additions & 55 deletions apps/src/lib/levelbuilder/script-editor/scriptEditorRedux.js
Expand Up @@ -6,7 +6,6 @@ const INIT = 'scriptEditor/INIT';
const ADD_GROUP = 'scriptEditor/ADD_GROUP';
const ADD_LESSON = 'scriptEditor/ADD_LESSON';
const MOVE_GROUP = 'scriptEditor/MOVE_GROUP';
const MOVE_LESSON = 'scriptEditor/MOVE_LESSON';
const REMOVE_GROUP = 'scriptEditor/REMOVE_GROUP';
const REMOVE_LESSON = 'scriptEditor/REMOVE_LESSON';
const SET_LESSON_GROUP = 'scriptEditor/SET_LESSON_GROUP';
Expand Down Expand Up @@ -44,13 +43,6 @@ export const moveGroup = (groupPosition, direction) => ({
direction
});

export const moveLesson = (groupPosition, lessonPosition, direction) => ({
type: MOVE_LESSON,
groupPosition,
lessonPosition,
direction
});

export const removeGroup = groupPosition => ({
type: REMOVE_GROUP,
groupPosition
Expand Down Expand Up @@ -114,18 +106,9 @@ function updateGroupPositions(lessonGroups) {
}

function updateLessonPositions(lessonGroups) {
let relativePosition = 1;
let absolutePosition = 1;
lessonGroups.forEach(lessonGroup => {
lessonGroup.lessons.forEach(lesson => {
lesson.position = absolutePosition;
if (lesson.lockable) {
lesson.relativePosition = undefined;
} else {
lesson.relativePosition = relativePosition;
relativePosition++;
}
absolutePosition++;
lessonGroup.lessons.forEach((lesson, lessonIndex) => {
lesson.position = lessonIndex + 1;
});
});
}
Expand Down Expand Up @@ -170,7 +153,7 @@ function lessonGroups(state = [], action) {
}
case REMOVE_LESSON: {
const lessons = newState[action.groupPosition - 1].lessons;
lessons.splice(action.lessonPosition - lessons[0].position, 1);
lessons.splice(action.lessonPosition - 1, 1);
updateLessonPositions(newState);
break;
}
Expand All @@ -190,44 +173,10 @@ function lessonGroups(state = [], action) {
updateLessonPositions(newState);
break;
}
case MOVE_LESSON: {
const groupIndex = action.groupPosition - 1;

const lessons = newState[groupIndex].lessons;

const lessonIndex = action.lessonPosition - lessons[0].position;
const lessonSwapIndex =
action.direction === 'up' ? lessonIndex - 1 : lessonIndex + 1;

if (lessonSwapIndex >= 0 && lessonSwapIndex <= lessons.length - 1) {
//if lesson is staying in the same lesson group
const tempLesson = lessons[lessonIndex];
lessons[lessonIndex] = lessons[lessonSwapIndex];
lessons[lessonSwapIndex] = tempLesson;
} else {
const groupSwapIndex =
action.direction === 'up' ? groupIndex - 1 : groupIndex + 1;

// Remove the lesson from the old lesson group
const oldLessons = newState[groupIndex].lessons;
const curLesson = oldLessons.splice(lessonIndex, 1)[0];

// add lesson to the new lesson group
const newLessons = newState[groupSwapIndex].lessons;
action.direction === 'up'
? newLessons.push(curLesson)
: newLessons.unshift(curLesson);
}
updateLessonPositions(newState);
break;
}
case SET_LESSON_GROUP: {
// Remove the lesson from the old lesson group
const oldLessons = newState[action.oldGroupPosition - 1].lessons;
const curLesson = oldLessons.splice(
action.lessonPosition - oldLessons[0].position,
1
)[0];
const curLesson = oldLessons.splice(action.lessonPosition - 1, 1)[0];

// add lesson to the new lesson group
const newLessons = newState[action.newGroupPosition - 1].lessons;
Expand Down
5 changes: 2 additions & 3 deletions apps/src/sites/studio/pages/scripts/edit.js
Expand Up @@ -27,11 +27,10 @@ export default function initPage(scriptEditorData) {
bigQuestions: lesson_group.big_questions || '',
lessons: lesson_group.lessons
.filter(lesson => lesson.id)
.map(lesson => ({
.map((lesson, lessonIndex) => ({
id: lesson.id,
key: lesson.key,
position: lesson.position,
relativePosition: lesson.relative_position,
position: lessonIndex + 1,
lockable: lesson.lockable,
assessment: lesson.assessment,
unplugged: lesson.unplugged,
Expand Down