Skip to content

Commit

Permalink
Merge pull request #37440 from code-dot-org/reorder-lessons
Browse files Browse the repository at this point in the history
Fix reordering of lessons via drag-and-drop on the script edit gui page
  • Loading branch information
davidsbailey committed Oct 27, 2020
2 parents 7ba4a39 + 9caa083 commit cb3e9d5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 205 deletions.
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

0 comments on commit cb3e9d5

Please sign in to comment.