RezinaLMS-Admin/src/pages/content-management/components/CatalogCoursePracticesPanel.tsx

359 lines
13 KiB
TypeScript

import { SpinnerIcon } from "@/components/ui/spinner-icon";
import { notifyApiError } from "../../../lib/apiErrors"
import { useCallback, useEffect, useMemo, useState } from "react"
import { useNavigate } from "react-router-dom"
import { BookOpen, RefreshCw } from "lucide-react"
import { toast } from "sonner"
import {
deleteExamPrepPractice,
getExamPrepCatalogCoursePractices,
setExamPrepPracticePublishStatus,
} from "../../../api/courses.api"
import { unwrapPracticesList } from "../../../lib/parentContextPractice"
import { fetchAllOffsetPages } from "../../../lib/fetchAllOffsetPages"
import { parentsFromPractice } from "../../../lib/practiceParents"
import {
hasCatalogCourseDirectPracticeLink,
isPracticeParentUnlinkNotLinkedError,
listCatalogCourseContextUnlinkParents,
practiceUnlinkBlockedMessage,
unlinkPracticeFromParent,
} from "../../../lib/practiceParentUnlink"
import { Button } from "../../../components/ui/button"
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from "../../../components/ui/dialog"
import { ContentListSearchFilterBar } from "./ContentListSearchFilterBar"
import { PracticeSelectableGrid } from "./PracticeSelectableGrid"
import {
filterBySearchAndPublishStatus,
type PublishStatusFilter,
} from "../../../lib/contentListFilters"
import type {
ParentContextPractice,
PracticePublishStatus,
} from "../../../types/course.types"
function extractPracticesPage(
res: Awaited<ReturnType<typeof getExamPrepCatalogCoursePractices>>,
): { practices: ParentContextPractice[]; totalCount: number } {
const body = res.data
const data =
body && typeof body === "object" && "data" in body
? (body as { data?: { practices?: unknown[]; total_count?: number } }).data
: null
const practices = unwrapPracticesList(res)
const totalCount =
typeof data?.total_count === "number" ? data.total_count : practices.length
return { practices, totalCount }
}
type CatalogCoursePracticesPanelProps = {
catalogCourseId: number
programType: string
courseName: string
}
export function CatalogCoursePracticesPanel({
catalogCourseId,
programType,
courseName,
}: CatalogCoursePracticesPanelProps) {
const navigate = useNavigate()
const [practices, setPractices] = useState<ParentContextPractice[]>([])
const [totalCount, setTotalCount] = useState(0)
const [loading, setLoading] = useState(true)
const [loadError, setLoadError] = useState<string | null>(null)
const [practiceToDelete, setPracticeToDelete] = useState<ParentContextPractice | null>(null)
const [practiceToUnlink, setPracticeToUnlink] = useState<ParentContextPractice | null>(null)
const [deleting, setDeleting] = useState(false)
const [unlinking, setUnlinking] = useState(false)
const [publishStatusUpdatingId, setPublishStatusUpdatingId] = useState<number | null>(null)
const [listSearch, setListSearch] = useState("")
const [publishStatusFilter, setPublishStatusFilter] = useState<PublishStatusFilter>("all")
const validCourse = Number.isFinite(catalogCourseId) && catalogCourseId > 0
const load = useCallback(async () => {
if (!validCourse) {
setLoading(false)
setLoadError("Invalid catalog course.")
setPractices([])
return
}
setLoading(true)
setLoadError(null)
try {
const list = await fetchAllOffsetPages(async (offset, limit) => {
const res = await getExamPrepCatalogCoursePractices(catalogCourseId, {
limit,
offset,
})
const { practices, totalCount } = extractPracticesPage(res)
return { items: practices, total_count: totalCount }
})
setPractices(list)
setTotalCount(list.length)
} catch (error) {
setPractices([])
setTotalCount(0)
setLoadError("Could not load practices for this catalog course.")
notifyApiError(error, "Failed to load practices")
} finally {
setLoading(false)
}
}, [catalogCourseId, validCourse])
useEffect(() => {
void load()
}, [load])
const filteredPractices = useMemo(
() =>
filterBySearchAndPublishStatus(practices, {
search: listSearch,
publishStatusFilter,
getSearchFields: (p) => [p.title, p.story_description, p.quick_tips],
getPublishStatus: (p) => p.publish_status,
}),
[listSearch, practices, publishStatusFilter],
)
const editPracticeHref = (practiceId: number) =>
`/new-content/courses/${programType}/${catalogCourseId}/edit-practice/${practiceId}?backTo=courses`
const handlePracticePublishStatus = async (
practiceId: number,
nextStatus: PracticePublishStatus,
) => {
setPublishStatusUpdatingId(practiceId)
try {
await setExamPrepPracticePublishStatus(practiceId, {
publish_status: nextStatus,
})
setPractices((prev) =>
prev.map((p) =>
p.id === practiceId ? { ...p, publish_status: nextStatus } : p,
),
)
toast.success(
nextStatus === "PUBLISHED" ? "Practice published" : "Practice saved as draft",
)
} catch (e: unknown) {
const err = e as { response?: { data?: { message?: string } } }
notifyApiError(err, "Failed to update practice status")
} finally {
setPublishStatusUpdatingId(null)
}
}
const catalogUnlinkContext = useMemo(
() => ({ scope: "catalog_course" as const, catalogCourseId }),
[catalogCourseId],
)
const confirmUnlinkPractice = async () => {
if (!practiceToUnlink) return
const parents = listCatalogCourseContextUnlinkParents(
practiceToUnlink,
catalogCourseId,
)
if (parents.length === 0) {
toast.error(
practiceUnlinkBlockedMessage(practiceToUnlink, catalogUnlinkContext) ??
"This practice is not linked to this catalog course.",
)
return
}
setUnlinking(true)
try {
for (const parent of parents) {
await unlinkPracticeFromParent({
practiceId: practiceToUnlink.id,
parent,
isExamPrep: true,
})
}
toast.success(`Practice removed from ${courseName}`)
setPracticeToUnlink(null)
await load()
} catch (e) {
if (isPracticeParentUnlinkNotLinkedError(e)) {
toast.info("This location was already removed.")
setPracticeToUnlink(null)
await load()
return
}
notifyApiError(e, "Could not remove location")
} finally {
setUnlinking(false)
}
}
const confirmDeletePractice = async () => {
if (!practiceToDelete) return
setDeleting(true)
try {
await deleteExamPrepPractice(practiceToDelete.id)
toast.success("Practice deleted")
setPracticeToDelete(null)
await load()
} catch (e: unknown) {
const err = e as { response?: { data?: { message?: string } } }
notifyApiError(err, "Failed to delete practice")
} finally {
setDeleting(false)
}
}
if (loading) {
return (
<div className="flex flex-col items-center justify-center py-24 text-grayScale-500">
<SpinnerIcon className="mb-3 h-8 w-8 text-brand-500" />
<p className="text-sm font-medium">Loading practices…</p>
</div>
)
}
if (loadError) {
return (
<div className="rounded-2xl border border-amber-100 bg-amber-50/80 px-6 py-8 text-center">
<p className="text-sm text-amber-900">{loadError}</p>
<Button variant="outline" size="sm" className="mt-4" onClick={() => void load()}>
<RefreshCw className="mr-2 h-4 w-4" />
Retry
</Button>
</div>
)
}
if (practices.length === 0) {
return (
<div className="rounded-xl border border-dashed border-grayScale-200 bg-grayScale-50/50 px-6 py-14 text-center">
<BookOpen className="mx-auto mb-3 h-10 w-10 text-grayScale-300" />
<p className="text-sm font-medium text-grayScale-600">No practices for {courseName} yet</p>
<p className="mt-1 text-sm text-grayScale-400">
Add a practice to attach questions directly to this catalog course.
</p>
</div>
)
}
return (
<div className="space-y-6">
<p className="text-sm text-grayScale-500">
{totalCount} practice{totalCount === 1 ? "" : "s"} linked to this catalog course
</p>
<ContentListSearchFilterBar
search={listSearch}
onSearchChange={setListSearch}
publishStatusFilter={publishStatusFilter}
onPublishStatusFilterChange={setPublishStatusFilter}
searchPlaceholder="Search practices by title or description…"
searchAriaLabel="Search catalog course practices"
/>
{filteredPractices.length === 0 ? (
<div className="rounded-xl border border-dashed border-grayScale-200 bg-grayScale-50/50 px-6 py-14 text-center">
<p className="text-sm font-medium text-grayScale-600">
No practices match your search or status filter
</p>
</div>
) : (
<PracticeSelectableGrid
practices={filteredPractices}
searchQuery={listSearch}
locationLabel={courseName}
unlinkContext={catalogUnlinkContext}
isExamPrep
publishStatusUpdatingId={publishStatusUpdatingId}
onReload={load}
unlinkActionLabel="Remove from course"
deleteActionLabel="Delete selected"
onEdit={(practice) => navigate(editPracticeHref(practice.id))}
onPublish={(practiceId) =>
void handlePracticePublishStatus(practiceId, "PUBLISHED")
}
onSaveAsDraft={(practiceId) =>
void handlePracticePublishStatus(practiceId, "DRAFT")
}
onUnlink={(practice) => {
if (!hasCatalogCourseDirectPracticeLink(practice, catalogCourseId)) {
toast.info(
practiceUnlinkBlockedMessage(practice, catalogUnlinkContext) ??
"This practice is not directly linked to this catalog course.",
)
return
}
setPracticeToUnlink(practice)
}}
onDelete={(practice) => setPracticeToDelete(practice)}
/>
)}
<Dialog
open={practiceToUnlink != null}
onOpenChange={(open) => !open && !unlinking && setPracticeToUnlink(null)}
>
<DialogContent className="max-w-md rounded-2xl">
<DialogHeader>
<DialogTitle>Remove from this catalog course?</DialogTitle>
</DialogHeader>
<p className="text-sm text-grayScale-600">
<span className="font-semibold text-grayScale-800">
{practiceToUnlink?.title || "This practice"}
</span>{" "}
will be detached from{" "}
<span className="font-semibold text-grayScale-800">{courseName}</span>.
{practiceToUnlink &&
parentsFromPractice(practiceToUnlink).filter(
(p) =>
!(
p.parent_kind === catalogParent.parent_kind &&
p.parent_id === catalogParent.parent_id
),
).length === 0
? " The practice will be unlinked until re-attached."
: " Other locations are unaffected."}
</p>
<DialogFooter className="gap-2 border-t border-grayScale-100 px-6 py-4 sm:justify-end">
<Button variant="outline" onClick={() => setPracticeToUnlink(null)} disabled={unlinking}>
Cancel
</Button>
<Button onClick={() => void confirmUnlinkPractice()} disabled={unlinking}>
{unlinking ? "Removing…" : "Remove location"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<Dialog open={practiceToDelete != null} onOpenChange={(open) => !open && setPracticeToDelete(null)}>
<DialogContent className="max-w-md rounded-2xl">
<DialogHeader>
<DialogTitle>Delete practice permanently?</DialogTitle>
</DialogHeader>
<p className="text-sm text-grayScale-600">
This permanently removes{" "}
<span className="font-semibold text-grayScale-800">
{practiceToDelete?.title || "this practice"}
</span>{" "}
and all of its questions.
</p>
<DialogFooter className="gap-2 border-t border-grayScale-100 px-6 py-4 sm:justify-end">
<Button variant="outline" onClick={() => setPracticeToDelete(null)} disabled={deleting}>
Cancel
</Button>
<Button variant="destructive" onClick={() => void confirmDeletePractice()} disabled={deleting}>
{deleting ? "Deleting…" : "Delete"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
)
}