back button fix
This commit is contained in:
parent
7052173287
commit
620670cf0c
|
|
@ -1,6 +1,5 @@
|
||||||
import { ArrowLeft } from "lucide-react";
|
import { ArrowLeft } from "lucide-react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { navigateBack } from "../../lib/navigateBack";
|
|
||||||
import { cn } from "../../lib/utils";
|
import { cn } from "../../lib/utils";
|
||||||
|
|
||||||
type PageBackLinkProps = {
|
type PageBackLinkProps = {
|
||||||
|
|
@ -16,12 +15,9 @@ export function PageBackLink({
|
||||||
className,
|
className,
|
||||||
iconClassName,
|
iconClassName,
|
||||||
}: PageBackLinkProps) {
|
}: PageBackLinkProps) {
|
||||||
const navigate = useNavigate();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<Link
|
||||||
type="button"
|
to={fallbackTo}
|
||||||
onClick={() => navigateBack(navigate, fallbackTo)}
|
|
||||||
className={cn(
|
className={cn(
|
||||||
"group flex w-fit items-center gap-2 text-[15px] font-medium text-grayScale-600 transition-colors hover:text-brand-500",
|
"group flex w-fit items-center gap-2 text-[15px] font-medium text-grayScale-600 transition-colors hover:text-brand-500",
|
||||||
className,
|
className,
|
||||||
|
|
@ -34,6 +30,6 @@ export function PageBackLink({
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
{label}
|
{label}
|
||||||
</button>
|
</Link>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,6 @@
|
||||||
import type { NavigateFunction } from "react-router-dom";
|
import type { NavigateFunction } from "react-router-dom";
|
||||||
|
|
||||||
export function canNavigateBack(): boolean {
|
/** Navigate to an explicit back destination (never browser history). */
|
||||||
const idx = (window.history.state as { idx?: number } | null)?.idx;
|
|
||||||
return typeof idx === "number" && idx > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function navigateBack(navigate: NavigateFunction, fallbackTo: string): void {
|
export function navigateBack(navigate: NavigateFunction, fallbackTo: string): void {
|
||||||
if (canNavigateBack()) {
|
|
||||||
navigate(-1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
navigate(fallbackTo);
|
navigate(fallbackTo);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,15 @@ import type { ParentContextPractice, PracticeParent } from "../types/course.type
|
||||||
import { parentsFromPractice } from "./practiceParents"
|
import { parentsFromPractice } from "./practiceParents"
|
||||||
import {
|
import {
|
||||||
isPracticeParentUnlinkNotLinkedError,
|
isPracticeParentUnlinkNotLinkedError,
|
||||||
|
listCourseContextUnlinkParents,
|
||||||
|
listModuleContextUnlinkParents,
|
||||||
resolveCourseContextUnlinkParent,
|
resolveCourseContextUnlinkParent,
|
||||||
resolveModuleContextUnlinkParent,
|
resolveModuleContextUnlinkParent,
|
||||||
unlinkPracticeFromParent,
|
unlinkPracticeFromParent,
|
||||||
} from "./practiceParentUnlink"
|
} from "./practiceParentUnlink"
|
||||||
|
|
||||||
export type PracticeUnlinkContext =
|
export type PracticeUnlinkContext =
|
||||||
| { scope: "course"; courseId: number; moduleIds: number[] }
|
| { scope: "course"; courseId: number; moduleIds: number[]; lessonIds?: number[] }
|
||||||
| { scope: "module"; moduleId: number; lessonIds: number[] }
|
| { scope: "module"; moduleId: number; lessonIds: number[] }
|
||||||
| { scope: "lesson"; lessonId: number }
|
| { scope: "lesson"; lessonId: number }
|
||||||
| { scope: "unit"; unitId: number }
|
| { scope: "unit"; unitId: number }
|
||||||
|
|
@ -34,6 +36,7 @@ export function resolvePracticeUnlinkParent(
|
||||||
practice,
|
practice,
|
||||||
context.courseId,
|
context.courseId,
|
||||||
context.moduleIds,
|
context.moduleIds,
|
||||||
|
context.lessonIds ?? [],
|
||||||
)
|
)
|
||||||
case "module":
|
case "module":
|
||||||
return resolveModuleContextUnlinkParent(
|
return resolveModuleContextUnlinkParent(
|
||||||
|
|
@ -65,6 +68,36 @@ export function resolvePracticeUnlinkParent(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function listPracticeUnlinkParents(
|
||||||
|
practice: ParentContextPractice,
|
||||||
|
context: PracticeUnlinkContext,
|
||||||
|
): PracticeParent[] {
|
||||||
|
switch (context.scope) {
|
||||||
|
case "course":
|
||||||
|
return listCourseContextUnlinkParents(
|
||||||
|
practice,
|
||||||
|
context.courseId,
|
||||||
|
context.moduleIds,
|
||||||
|
context.lessonIds ?? [],
|
||||||
|
)
|
||||||
|
case "module":
|
||||||
|
return listModuleContextUnlinkParents(
|
||||||
|
practice,
|
||||||
|
context.moduleId,
|
||||||
|
context.lessonIds,
|
||||||
|
)
|
||||||
|
case "lesson": {
|
||||||
|
const parent = resolvePracticeUnlinkParent(practice, context)
|
||||||
|
return parent ? [parent] : []
|
||||||
|
}
|
||||||
|
case "unit":
|
||||||
|
case "catalog_course": {
|
||||||
|
const parent = resolvePracticeUnlinkParent(practice, context)
|
||||||
|
return parent ? [parent] : []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function bulkUnlinkPractices(opts: {
|
export async function bulkUnlinkPractices(opts: {
|
||||||
practices: ParentContextPractice[]
|
practices: ParentContextPractice[]
|
||||||
context: PracticeUnlinkContext
|
context: PracticeUnlinkContext
|
||||||
|
|
@ -77,27 +110,42 @@ export async function bulkUnlinkPractices(opts: {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const practice of opts.practices) {
|
for (const practice of opts.practices) {
|
||||||
const parent = resolvePracticeUnlinkParent(practice, opts.context)
|
const parents = listPracticeUnlinkParents(practice, opts.context)
|
||||||
if (!parent) {
|
if (parents.length === 0) {
|
||||||
result.skipped += 1
|
result.skipped += 1
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let practiceSucceeded = false
|
||||||
|
let practiceFailed = false
|
||||||
|
let practiceSkipped = false
|
||||||
|
|
||||||
|
for (const parent of parents) {
|
||||||
try {
|
try {
|
||||||
await unlinkPracticeFromParent({
|
await unlinkPracticeFromParent({
|
||||||
practiceId: practice.id,
|
practiceId: practice.id,
|
||||||
parent,
|
parent,
|
||||||
isExamPrep: opts.isExamPrep,
|
isExamPrep: opts.isExamPrep,
|
||||||
})
|
})
|
||||||
result.succeeded += 1
|
practiceSucceeded = true
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (isPracticeParentUnlinkNotLinkedError(error)) {
|
if (isPracticeParentUnlinkNotLinkedError(error)) {
|
||||||
result.skipped += 1
|
practiceSkipped = true
|
||||||
} else {
|
} else {
|
||||||
result.failed += 1
|
practiceFailed = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (practiceFailed) {
|
||||||
|
result.failed += 1
|
||||||
|
} else if (practiceSucceeded) {
|
||||||
|
result.succeeded += 1
|
||||||
|
} else if (practiceSkipped) {
|
||||||
|
result.skipped += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import {
|
||||||
} from "../api/courses.api"
|
} from "../api/courses.api"
|
||||||
import type { ParentContextPractice, PracticeParent } from "../types/course.types"
|
import type { ParentContextPractice, PracticeParent } from "../types/course.types"
|
||||||
import { normalizeParentContextPractice } from "./parentContextPractice"
|
import { normalizeParentContextPractice } from "./parentContextPractice"
|
||||||
import { formatPracticeParentLabel, parentsFromPractice } from "./practiceParents"
|
import { formatPracticeParentLabel, parentsFromPractice, dedupeParents } from "./practiceParents"
|
||||||
|
|
||||||
export function unlinkParentConfirmMessage(
|
export function unlinkParentConfirmMessage(
|
||||||
locationLabel: string,
|
locationLabel: string,
|
||||||
|
|
@ -51,41 +51,85 @@ export function resolveModuleContextUnlinkParent(
|
||||||
moduleId: number,
|
moduleId: number,
|
||||||
lessonIdsInModule: number[],
|
lessonIdsInModule: number[],
|
||||||
): PracticeParent | null {
|
): PracticeParent | null {
|
||||||
const parents = parentsFromPractice(practice)
|
|
||||||
const moduleLink = parents.find(
|
|
||||||
(p) => p.parent_kind === "MODULE" && p.parent_id === moduleId,
|
|
||||||
)
|
|
||||||
if (moduleLink) return moduleLink
|
|
||||||
|
|
||||||
const lessonIds = new Set(lessonIdsInModule.filter((id) => id > 0))
|
|
||||||
if (lessonIds.size === 0) return null
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
parents.find(
|
listModuleContextUnlinkParents(practice, moduleId, lessonIdsInModule)[0] ??
|
||||||
(p) => p.parent_kind === "LESSON" && lessonIds.has(p.parent_id),
|
null
|
||||||
) ?? null
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** All parent rows to remove when detaching a practice from a module practice tab. */
|
||||||
|
export function listModuleContextUnlinkParents(
|
||||||
|
practice: ParentContextPractice,
|
||||||
|
moduleId: number,
|
||||||
|
lessonIdsInModule: number[],
|
||||||
|
): PracticeParent[] {
|
||||||
|
const parents = parentsFromPractice(practice)
|
||||||
|
const lessonIds = new Set(lessonIdsInModule.filter((id) => id > 0))
|
||||||
|
const targets: PracticeParent[] = []
|
||||||
|
|
||||||
|
for (const parent of parents) {
|
||||||
|
if (parent.parent_kind === "COURSE") continue
|
||||||
|
if (parent.parent_kind === "MODULE" && parent.parent_id === moduleId) {
|
||||||
|
targets.push(parent)
|
||||||
|
} else if (
|
||||||
|
parent.parent_kind === "LESSON" &&
|
||||||
|
lessonIds.has(parent.parent_id)
|
||||||
|
) {
|
||||||
|
targets.push(parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dedupeParents(targets)
|
||||||
|
}
|
||||||
|
|
||||||
/** Parent row to DELETE when removing a practice from a course's practice tab. */
|
/** Parent row to DELETE when removing a practice from a course's practice tab. */
|
||||||
export function resolveCourseContextUnlinkParent(
|
export function resolveCourseContextUnlinkParent(
|
||||||
practice: ParentContextPractice,
|
practice: ParentContextPractice,
|
||||||
courseId: number,
|
courseId: number,
|
||||||
moduleIdsInCourse: number[],
|
moduleIdsInCourse: number[],
|
||||||
|
lessonIdsInCourse: number[] = [],
|
||||||
): PracticeParent | null {
|
): PracticeParent | null {
|
||||||
|
return (
|
||||||
|
listCourseContextUnlinkParents(
|
||||||
|
practice,
|
||||||
|
courseId,
|
||||||
|
moduleIdsInCourse,
|
||||||
|
lessonIdsInCourse,
|
||||||
|
)[0] ?? null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** All parent rows to remove when detaching a practice from a course practice tab. */
|
||||||
|
export function listCourseContextUnlinkParents(
|
||||||
|
practice: ParentContextPractice,
|
||||||
|
courseId: number,
|
||||||
|
moduleIdsInCourse: number[],
|
||||||
|
lessonIdsInCourse: number[] = [],
|
||||||
|
): PracticeParent[] {
|
||||||
const parents = parentsFromPractice(practice)
|
const parents = parentsFromPractice(practice)
|
||||||
const courseLink = parents.find(
|
|
||||||
(p) => p.parent_kind === "COURSE" && p.parent_id === courseId,
|
|
||||||
)
|
|
||||||
if (courseLink) return courseLink
|
|
||||||
|
|
||||||
const moduleIds = new Set(moduleIdsInCourse.filter((id) => id > 0))
|
const moduleIds = new Set(moduleIdsInCourse.filter((id) => id > 0))
|
||||||
const moduleLink = parents.find(
|
const lessonIds = new Set(lessonIdsInCourse.filter((id) => id > 0))
|
||||||
(p) => p.parent_kind === "MODULE" && moduleIds.has(p.parent_id),
|
const targets: PracticeParent[] = []
|
||||||
)
|
|
||||||
if (moduleLink) return moduleLink
|
|
||||||
|
|
||||||
return parents.find((p) => p.parent_kind === "LESSON") ?? null
|
for (const parent of parents) {
|
||||||
|
if (parent.parent_kind === "COURSE" && parent.parent_id === courseId) {
|
||||||
|
targets.push(parent)
|
||||||
|
} else if (
|
||||||
|
parent.parent_kind === "MODULE" &&
|
||||||
|
moduleIds.has(parent.parent_id)
|
||||||
|
) {
|
||||||
|
targets.push(parent)
|
||||||
|
} else if (
|
||||||
|
parent.parent_kind === "LESSON" &&
|
||||||
|
(lessonIds.size === 0 || lessonIds.has(parent.parent_id))
|
||||||
|
) {
|
||||||
|
if (lessonIds.size > 0) {
|
||||||
|
targets.push(parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dedupeParents(targets)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function unlinkPracticeFromParent(opts: {
|
export async function unlinkPracticeFromParent(opts: {
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ export function normalizePracticeParent(row: unknown): PracticeParent | null {
|
||||||
const kind = String(o.parent_kind ?? o.ParentKind ?? o.parentKind ?? "")
|
const kind = String(o.parent_kind ?? o.ParentKind ?? o.parentKind ?? "")
|
||||||
.trim()
|
.trim()
|
||||||
.toUpperCase()
|
.toUpperCase()
|
||||||
const parent_id = Number(o.parent_id ?? o.ParentId ?? o.parentId ?? o.id ?? o.ID)
|
const parent_id = Number(o.parent_id ?? o.ParentId ?? o.parentId)
|
||||||
if (!isPracticeParentKind(kind)) return null
|
if (!isPracticeParentKind(kind)) return null
|
||||||
if (!Number.isFinite(parent_id) || parent_id <= 0) return null
|
if (!Number.isFinite(parent_id) || parent_id <= 0) return null
|
||||||
return { parent_kind: kind, parent_id }
|
return { parent_kind: kind, parent_id }
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ import alertSrc from "../../assets/Alert.svg";
|
||||||
import {
|
import {
|
||||||
deleteTopLevelCourseModule,
|
deleteTopLevelCourseModule,
|
||||||
deleteParentLinkedPractice,
|
deleteParentLinkedPractice,
|
||||||
|
getModuleLessons,
|
||||||
getPracticesByParentCourse,
|
getPracticesByParentCourse,
|
||||||
getProgramCourses,
|
getProgramCourses,
|
||||||
getTopLevelCourseModules,
|
getTopLevelCourseModules,
|
||||||
|
|
@ -46,6 +47,7 @@ import type {
|
||||||
PracticePublishStatus,
|
PracticePublishStatus,
|
||||||
ProgramCourseListItem,
|
ProgramCourseListItem,
|
||||||
TopLevelCourseModuleItem,
|
TopLevelCourseModuleItem,
|
||||||
|
TopLevelModuleLessonItem,
|
||||||
} from "../../types/course.types";
|
} from "../../types/course.types";
|
||||||
import { ContentPublishStatusChip } from "./components/ContentPublishStatusChip";
|
import { ContentPublishStatusChip } from "./components/ContentPublishStatusChip";
|
||||||
import { ContentAccessTierChip } from "./components/ContentAccessTierChip";
|
import { ContentAccessTierChip } from "./components/ContentAccessTierChip";
|
||||||
|
|
@ -68,7 +70,7 @@ import { DisplayValue } from "../../lib/displayValue"
|
||||||
import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents"
|
import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents"
|
||||||
import {
|
import {
|
||||||
isPracticeParentUnlinkNotLinkedError,
|
isPracticeParentUnlinkNotLinkedError,
|
||||||
resolveCourseContextUnlinkParent,
|
listCourseContextUnlinkParents,
|
||||||
unlinkPracticeFromParent,
|
unlinkPracticeFromParent,
|
||||||
} from "../../lib/practiceParentUnlink"
|
} from "../../lib/practiceParentUnlink"
|
||||||
import { SearchHighlight } from "../../components/SearchHighlight"
|
import { SearchHighlight } from "../../components/SearchHighlight"
|
||||||
|
|
@ -211,6 +213,7 @@ export function CourseDetailPage() {
|
||||||
useState<ParentContextPractice | null>(null);
|
useState<ParentContextPractice | null>(null);
|
||||||
const [unlinkingPractice, setUnlinkingPractice] = useState(false);
|
const [unlinkingPractice, setUnlinkingPractice] = useState(false);
|
||||||
const [deletingPractice, setDeletingPractice] = useState(false);
|
const [deletingPractice, setDeletingPractice] = useState(false);
|
||||||
|
const [courseLessonIds, setCourseLessonIds] = useState<number[]>([]);
|
||||||
const [publishStatusModuleId, setPublishStatusModuleId] = useState<
|
const [publishStatusModuleId, setPublishStatusModuleId] = useState<
|
||||||
number | null
|
number | null
|
||||||
>(null);
|
>(null);
|
||||||
|
|
@ -415,29 +418,68 @@ export function CourseDetailPage() {
|
||||||
[modules],
|
[modules],
|
||||||
);
|
);
|
||||||
|
|
||||||
const practiceUnlinkParent = useMemo((): PracticeParent | null => {
|
const loadCourseLessonIds = useCallback(async () => {
|
||||||
if (!practiceToUnlink || !Number.isFinite(courseIdNum) || courseIdNum < 1) {
|
if (courseModuleIds.length === 0) {
|
||||||
return null;
|
setCourseLessonIds([]);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return resolveCourseContextUnlinkParent(
|
try {
|
||||||
|
const lessonLists = await Promise.all(
|
||||||
|
courseModuleIds.map((moduleId) =>
|
||||||
|
fetchAllOffsetPages(async (offset, limit) =>
|
||||||
|
offsetPageFromListEnvelope<TopLevelModuleLessonItem>(
|
||||||
|
await getModuleLessons(moduleId, { limit, offset }),
|
||||||
|
"lessons",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
setCourseLessonIds(
|
||||||
|
lessonLists
|
||||||
|
.flatMap((list) => list.map((lesson) => lesson.id))
|
||||||
|
.filter((id) => id > 0),
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
setCourseLessonIds([]);
|
||||||
|
}
|
||||||
|
}, [courseModuleIds]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (activeTab !== "practice") return;
|
||||||
|
void loadCourseLessonIds();
|
||||||
|
}, [activeTab, loadCourseLessonIds]);
|
||||||
|
|
||||||
|
const practiceUnlinkParents = useMemo((): PracticeParent[] => {
|
||||||
|
if (!practiceToUnlink || !Number.isFinite(courseIdNum) || courseIdNum < 1) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return listCourseContextUnlinkParents(
|
||||||
practiceToUnlink,
|
practiceToUnlink,
|
||||||
courseIdNum,
|
courseIdNum,
|
||||||
courseModuleIds,
|
courseModuleIds,
|
||||||
|
courseLessonIds,
|
||||||
);
|
);
|
||||||
}, [courseIdNum, courseModuleIds, practiceToUnlink]);
|
}, [
|
||||||
|
courseIdNum,
|
||||||
|
courseLessonIds,
|
||||||
|
courseModuleIds,
|
||||||
|
practiceToUnlink,
|
||||||
|
]);
|
||||||
|
|
||||||
const confirmUnlinkPractice = async () => {
|
const confirmUnlinkPractice = async () => {
|
||||||
if (!practiceToUnlink || !practiceUnlinkParent) {
|
if (!practiceToUnlink || practiceUnlinkParents.length === 0) {
|
||||||
toast.error("This practice is not linked to this course.");
|
toast.error("This practice is not linked to this course.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setUnlinkingPractice(true);
|
setUnlinkingPractice(true);
|
||||||
try {
|
try {
|
||||||
|
for (const parent of practiceUnlinkParents) {
|
||||||
await unlinkPracticeFromParent({
|
await unlinkPracticeFromParent({
|
||||||
practiceId: practiceToUnlink.id,
|
practiceId: practiceToUnlink.id,
|
||||||
parent: practiceUnlinkParent,
|
parent,
|
||||||
isExamPrep: false,
|
isExamPrep: false,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
toast.success(`Practice removed from ${displayTitle}`);
|
toast.success(`Practice removed from ${displayTitle}`);
|
||||||
setPracticeToUnlink(null);
|
setPracticeToUnlink(null);
|
||||||
await loadCoursePractices();
|
await loadCoursePractices();
|
||||||
|
|
@ -946,6 +988,7 @@ export function CourseDetailPage() {
|
||||||
scope: "course",
|
scope: "course",
|
||||||
courseId: courseIdNum,
|
courseId: courseIdNum,
|
||||||
moduleIds: courseModuleIds,
|
moduleIds: courseModuleIds,
|
||||||
|
lessonIds: courseLessonIds,
|
||||||
}}
|
}}
|
||||||
publishStatusUpdatingId={publishStatusPracticeId}
|
publishStatusUpdatingId={publishStatusPracticeId}
|
||||||
onReload={loadCoursePractices}
|
onReload={loadCoursePractices}
|
||||||
|
|
@ -1003,7 +1046,7 @@ export function CourseDetailPage() {
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{practiceToUnlink && practiceUnlinkParent ? (
|
{practiceToUnlink && practiceUnlinkParents.length > 0 ? (
|
||||||
<Dialog
|
<Dialog
|
||||||
open={practiceToUnlink != null}
|
open={practiceToUnlink != null}
|
||||||
onOpenChange={(open) => {
|
onOpenChange={(open) => {
|
||||||
|
|
@ -1024,9 +1067,10 @@ export function CourseDetailPage() {
|
||||||
.
|
.
|
||||||
{parentsFromPractice(practiceToUnlink).filter(
|
{parentsFromPractice(practiceToUnlink).filter(
|
||||||
(p) =>
|
(p) =>
|
||||||
!(
|
!practiceUnlinkParents.some(
|
||||||
p.parent_kind === practiceUnlinkParent.parent_kind &&
|
(target) =>
|
||||||
p.parent_id === practiceUnlinkParent.parent_id
|
target.parent_kind === p.parent_kind &&
|
||||||
|
target.parent_id === p.parent_id,
|
||||||
),
|
),
|
||||||
).length === 0
|
).length === 0
|
||||||
? " The practice will be unlinked until re-attached."
|
? " The practice will be unlinked until re-attached."
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ import {
|
||||||
import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents";
|
import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents";
|
||||||
import {
|
import {
|
||||||
isPracticeParentUnlinkNotLinkedError,
|
isPracticeParentUnlinkNotLinkedError,
|
||||||
resolveModuleContextUnlinkParent,
|
listModuleContextUnlinkParents,
|
||||||
unlinkPracticeFromParent,
|
unlinkPracticeFromParent,
|
||||||
} from "../../lib/practiceParentUnlink";
|
} from "../../lib/practiceParentUnlink";
|
||||||
import { Button } from "../../components/ui/button";
|
import { Button } from "../../components/ui/button";
|
||||||
|
|
@ -361,10 +361,10 @@ export function ModuleDetailPage() {
|
||||||
[lessons],
|
[lessons],
|
||||||
);
|
);
|
||||||
|
|
||||||
const practiceUnlinkParent = useMemo((): PracticeParent | null => {
|
const practiceUnlinkParents = useMemo((): PracticeParent[] => {
|
||||||
const mid = Number(moduleId);
|
const mid = Number(moduleId);
|
||||||
if (!practiceToUnlink || !Number.isFinite(mid) || mid < 1) return null;
|
if (!practiceToUnlink || !Number.isFinite(mid) || mid < 1) return [];
|
||||||
return resolveModuleContextUnlinkParent(
|
return listModuleContextUnlinkParents(
|
||||||
practiceToUnlink,
|
practiceToUnlink,
|
||||||
mid,
|
mid,
|
||||||
moduleLessonIds,
|
moduleLessonIds,
|
||||||
|
|
@ -372,17 +372,19 @@ export function ModuleDetailPage() {
|
||||||
}, [moduleId, moduleLessonIds, practiceToUnlink]);
|
}, [moduleId, moduleLessonIds, practiceToUnlink]);
|
||||||
|
|
||||||
const confirmUnlinkPractice = async () => {
|
const confirmUnlinkPractice = async () => {
|
||||||
if (!practiceToUnlink || !practiceUnlinkParent) {
|
if (!practiceToUnlink || practiceUnlinkParents.length === 0) {
|
||||||
toast.error("This practice is not linked to this module.");
|
toast.error("This practice is not linked to this module.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setUnlinkingPractice(true);
|
setUnlinkingPractice(true);
|
||||||
try {
|
try {
|
||||||
|
for (const parent of practiceUnlinkParents) {
|
||||||
await unlinkPracticeFromParent({
|
await unlinkPracticeFromParent({
|
||||||
practiceId: practiceToUnlink.id,
|
practiceId: practiceToUnlink.id,
|
||||||
parent: practiceUnlinkParent,
|
parent,
|
||||||
isExamPrep: false,
|
isExamPrep: false,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
toast.success(`Practice removed from ${displayModuleName}`);
|
toast.success(`Practice removed from ${displayModuleName}`);
|
||||||
setPracticeToUnlink(null);
|
setPracticeToUnlink(null);
|
||||||
await loadModulePractices();
|
await loadModulePractices();
|
||||||
|
|
@ -964,7 +966,7 @@ export function ModuleDetailPage() {
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{practiceToUnlink && practiceUnlinkParent ? (
|
{practiceToUnlink && practiceUnlinkParents.length > 0 ? (
|
||||||
<Dialog
|
<Dialog
|
||||||
open={practiceToUnlink != null}
|
open={practiceToUnlink != null}
|
||||||
onOpenChange={(open) => {
|
onOpenChange={(open) => {
|
||||||
|
|
@ -985,9 +987,10 @@ export function ModuleDetailPage() {
|
||||||
.
|
.
|
||||||
{parentsFromPractice(practiceToUnlink).filter(
|
{parentsFromPractice(practiceToUnlink).filter(
|
||||||
(p) =>
|
(p) =>
|
||||||
!(
|
!practiceUnlinkParents.some(
|
||||||
p.parent_kind === practiceUnlinkParent.parent_kind &&
|
(target) =>
|
||||||
p.parent_id === practiceUnlinkParent.parent_id
|
target.parent_kind === p.parent_kind &&
|
||||||
|
target.parent_id === p.parent_id,
|
||||||
),
|
),
|
||||||
).length === 0
|
).length === 0
|
||||||
? " The practice will be unlinked until re-attached."
|
? " The practice will be unlinked until re-attached."
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user