Skip to content

Commit 235e8c2

Browse files
committed
refactor(course-outline): merge reorder helpers into drag utils
1 parent 4e16b77 commit 235e8c2

5 files changed

Lines changed: 55 additions & 61 deletions

File tree

src/course-outline/OutlineTree.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
type SubsectionMoveDetails,
1616
type UnitMoveDetails,
1717
} from './drag-helper/utils';
18-
import { applyReorderMove } from './drag-helper/reorderHelpers';
18+
import { applyReorderMove } from './drag-helper/utils';
1919

2020
export interface OutlineTreeProps {
2121
sections: XBlock[];

src/course-outline/drag-helper/reorderHelpers.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/course-outline/drag-helper/utils.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,55 @@ export const possibleUnitMoves = (
457457

458458
return null;
459459
};
460+
461+
/**
462+
* Commit callback type for reorder operations.
463+
* Uses variadic tuple rest to express both call signatures:
464+
* - 3 params: subsection reorder (sectionId, prevSectionId, subsectionListIds)
465+
* - 4 params: unit reorder (sectionId, prevSectionId, subsectionId, unitListIds)
466+
*/
467+
type ReorderCommitFn = (
468+
sectionId: string,
469+
prevSectionId: string,
470+
...rest: [string[]] | [string, string[]]
471+
) => void | Promise<void>;
472+
473+
/**
474+
* Apply a reorder from moveDetails and preview + commit.
475+
*
476+
* Handles both subsection and unit reorders. If moveDetails contains
477+
* `subsectionId` the unit commit signature is used; otherwise the
478+
* subsection commit signature is used.
479+
*/
480+
export function applyReorderMove(
481+
moveDetails: SubsectionMoveDetails | null,
482+
currentSection: XBlock,
483+
previewSections: (sections: XBlock[]) => void,
484+
commitReorder: ReorderCommitFn,
485+
): void;
486+
export function applyReorderMove(
487+
moveDetails: UnitMoveDetails | null,
488+
currentSection: XBlock,
489+
previewSections: (sections: XBlock[]) => void,
490+
commitReorder: ReorderCommitFn,
491+
): void;
492+
export function applyReorderMove(
493+
moveDetails: MoveDetails | null,
494+
currentSection: XBlock,
495+
previewSections: (sections: XBlock[]) => void,
496+
commitReorder: ReorderCommitFn,
497+
): void {
498+
if (!moveDetails) { return; }
499+
const { fn, args, sectionId, subsectionId } = moveDetails;
500+
const [sectionsCopy, newItems] = fn(...args);
501+
if (!newItems || !sectionId) { return; }
502+
previewSections(sectionsCopy);
503+
const ids = newItems.map((s: XBlock) => s.id);
504+
if (subsectionId) {
505+
// Unit reorder
506+
commitReorder(sectionId, currentSection.id, subsectionId, ids);
507+
} else {
508+
// Subsection reorder
509+
commitReorder(sectionId, currentSection.id, ids);
510+
}
511+
}

src/course-outline/outline-sidebar/info-sidebar/SubsectionInfoSidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { useCourseOutlineContext } from '@src/course-outline/CourseOutlineContex
1414
import { useOutlineSidebarContext } from '@src/course-outline/outline-sidebar/OutlineSidebarContext';
1515
import { getLibraryId } from '@src/generic/key-utils';
1616
import { possibleSubsectionMoves } from '@src/course-outline/drag-helper/utils';
17-
import { applyReorderMove } from '@src/course-outline/drag-helper/reorderHelpers';
17+
import { applyReorderMove } from '@src/course-outline/drag-helper/utils';
1818
import { XBlock } from '@src/data/types';
1919

2020
import { InfoSection } from './InfoSection';

src/course-outline/outline-sidebar/info-sidebar/UnitInfoSidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { Link, useNavigate } from 'react-router-dom';
2828
import { getLibraryId } from '@src/generic/key-utils';
2929
import { extractCourseUnitId } from '@src/course-unit/legacy-sidebar/utils';
3030
import { possibleUnitMoves } from '@src/course-outline/drag-helper/utils';
31-
import { applyReorderMove } from '@src/course-outline/drag-helper/reorderHelpers';
31+
import { applyReorderMove } from '@src/course-outline/drag-helper/utils';
3232
import { GenericUnitInfoSettings } from '@src/course-unit/unit-sidebar/unit-info/GenericUnitInfoSettings';
3333
import { useQueryClient } from '@tanstack/react-query';
3434
import { useOutlineSidebarContext } from '../OutlineSidebarContext';

0 commit comments

Comments
 (0)