multiple practice unlink and delete
This commit is contained in:
parent
483ec8c15e
commit
7052173287
75
src/hooks/usePracticeListSelection.ts
Normal file
75
src/hooks/usePracticeListSelection.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
import { useCallback, useEffect, useMemo, useState } from "react"
|
||||||
|
import type { ParentContextPractice } from "../types/course.types"
|
||||||
|
|
||||||
|
export function usePracticeListSelection(
|
||||||
|
visiblePractices: ParentContextPractice[],
|
||||||
|
) {
|
||||||
|
const [selectedIds, setSelectedIds] = useState<Set<number>>(() => new Set())
|
||||||
|
|
||||||
|
const visibleIds = useMemo(
|
||||||
|
() => new Set(visiblePractices.map((practice) => practice.id)),
|
||||||
|
[visiblePractices],
|
||||||
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setSelectedIds((prev) => {
|
||||||
|
const next = new Set(
|
||||||
|
[...prev].filter((id) => visibleIds.has(id)),
|
||||||
|
)
|
||||||
|
return next.size === prev.size ? prev : next
|
||||||
|
})
|
||||||
|
}, [visibleIds])
|
||||||
|
|
||||||
|
const selectedPractices = useMemo(
|
||||||
|
() => visiblePractices.filter((practice) => selectedIds.has(practice.id)),
|
||||||
|
[visiblePractices, selectedIds],
|
||||||
|
)
|
||||||
|
|
||||||
|
const allSelected =
|
||||||
|
visiblePractices.length > 0 &&
|
||||||
|
visiblePractices.every((practice) => selectedIds.has(practice.id))
|
||||||
|
|
||||||
|
const isSelected = useCallback(
|
||||||
|
(practiceId: number) => selectedIds.has(practiceId),
|
||||||
|
[selectedIds],
|
||||||
|
)
|
||||||
|
|
||||||
|
const toggle = useCallback((practiceId: number) => {
|
||||||
|
setSelectedIds((prev) => {
|
||||||
|
const next = new Set(prev)
|
||||||
|
if (next.has(practiceId)) {
|
||||||
|
next.delete(practiceId)
|
||||||
|
} else {
|
||||||
|
next.add(practiceId)
|
||||||
|
}
|
||||||
|
return next
|
||||||
|
})
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const toggleAll = useCallback(() => {
|
||||||
|
setSelectedIds((prev) => {
|
||||||
|
if (
|
||||||
|
visiblePractices.length > 0 &&
|
||||||
|
visiblePractices.every((practice) => prev.has(practice.id))
|
||||||
|
) {
|
||||||
|
return new Set()
|
||||||
|
}
|
||||||
|
return new Set(visiblePractices.map((practice) => practice.id))
|
||||||
|
})
|
||||||
|
}, [visiblePractices])
|
||||||
|
|
||||||
|
const clear = useCallback(() => {
|
||||||
|
setSelectedIds(new Set())
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return {
|
||||||
|
selectedIds,
|
||||||
|
selectedCount: selectedIds.size,
|
||||||
|
selectedPractices,
|
||||||
|
allSelected,
|
||||||
|
isSelected,
|
||||||
|
toggle,
|
||||||
|
toggleAll,
|
||||||
|
clear,
|
||||||
|
}
|
||||||
|
}
|
||||||
174
src/lib/practiceBulkActions.ts
Normal file
174
src/lib/practiceBulkActions.ts
Normal file
|
|
@ -0,0 +1,174 @@
|
||||||
|
import {
|
||||||
|
deleteExamPrepPractice,
|
||||||
|
deleteParentLinkedPractice,
|
||||||
|
} from "../api/courses.api"
|
||||||
|
import type { ParentContextPractice, PracticeParent } from "../types/course.types"
|
||||||
|
import { parentsFromPractice } from "./practiceParents"
|
||||||
|
import {
|
||||||
|
isPracticeParentUnlinkNotLinkedError,
|
||||||
|
resolveCourseContextUnlinkParent,
|
||||||
|
resolveModuleContextUnlinkParent,
|
||||||
|
unlinkPracticeFromParent,
|
||||||
|
} from "./practiceParentUnlink"
|
||||||
|
|
||||||
|
export type PracticeUnlinkContext =
|
||||||
|
| { scope: "course"; courseId: number; moduleIds: number[] }
|
||||||
|
| { scope: "module"; moduleId: number; lessonIds: number[] }
|
||||||
|
| { scope: "lesson"; lessonId: number }
|
||||||
|
| { scope: "unit"; unitId: number }
|
||||||
|
| { scope: "catalog_course"; catalogCourseId: number }
|
||||||
|
|
||||||
|
export type BulkPracticeActionResult = {
|
||||||
|
succeeded: number
|
||||||
|
skipped: number
|
||||||
|
failed: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolvePracticeUnlinkParent(
|
||||||
|
practice: ParentContextPractice,
|
||||||
|
context: PracticeUnlinkContext,
|
||||||
|
): PracticeParent | null {
|
||||||
|
switch (context.scope) {
|
||||||
|
case "course":
|
||||||
|
return resolveCourseContextUnlinkParent(
|
||||||
|
practice,
|
||||||
|
context.courseId,
|
||||||
|
context.moduleIds,
|
||||||
|
)
|
||||||
|
case "module":
|
||||||
|
return resolveModuleContextUnlinkParent(
|
||||||
|
practice,
|
||||||
|
context.moduleId,
|
||||||
|
context.lessonIds,
|
||||||
|
)
|
||||||
|
case "lesson":
|
||||||
|
return (
|
||||||
|
parentsFromPractice(practice).find(
|
||||||
|
(p) =>
|
||||||
|
p.parent_kind === "LESSON" && p.parent_id === context.lessonId,
|
||||||
|
) ?? null
|
||||||
|
)
|
||||||
|
case "unit":
|
||||||
|
return (
|
||||||
|
parentsFromPractice(practice).find(
|
||||||
|
(p) => p.parent_kind === "UNIT" && p.parent_id === context.unitId,
|
||||||
|
) ?? null
|
||||||
|
)
|
||||||
|
case "catalog_course":
|
||||||
|
return (
|
||||||
|
parentsFromPractice(practice).find(
|
||||||
|
(p) =>
|
||||||
|
p.parent_kind === "CATALOG_COURSE" &&
|
||||||
|
p.parent_id === context.catalogCourseId,
|
||||||
|
) ?? null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function bulkUnlinkPractices(opts: {
|
||||||
|
practices: ParentContextPractice[]
|
||||||
|
context: PracticeUnlinkContext
|
||||||
|
isExamPrep: boolean
|
||||||
|
}): Promise<BulkPracticeActionResult> {
|
||||||
|
const result: BulkPracticeActionResult = {
|
||||||
|
succeeded: 0,
|
||||||
|
skipped: 0,
|
||||||
|
failed: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const practice of opts.practices) {
|
||||||
|
const parent = resolvePracticeUnlinkParent(practice, opts.context)
|
||||||
|
if (!parent) {
|
||||||
|
result.skipped += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await unlinkPracticeFromParent({
|
||||||
|
practiceId: practice.id,
|
||||||
|
parent,
|
||||||
|
isExamPrep: opts.isExamPrep,
|
||||||
|
})
|
||||||
|
result.succeeded += 1
|
||||||
|
} catch (error) {
|
||||||
|
if (isPracticeParentUnlinkNotLinkedError(error)) {
|
||||||
|
result.skipped += 1
|
||||||
|
} else {
|
||||||
|
result.failed += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function bulkDeletePractices(opts: {
|
||||||
|
practices: ParentContextPractice[]
|
||||||
|
isExamPrep: boolean
|
||||||
|
}): Promise<BulkPracticeActionResult> {
|
||||||
|
const result: BulkPracticeActionResult = {
|
||||||
|
succeeded: 0,
|
||||||
|
skipped: 0,
|
||||||
|
failed: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const practice of opts.practices) {
|
||||||
|
try {
|
||||||
|
if (opts.isExamPrep) {
|
||||||
|
await deleteExamPrepPractice(practice.id)
|
||||||
|
} else {
|
||||||
|
await deleteParentLinkedPractice(practice.id)
|
||||||
|
}
|
||||||
|
result.succeeded += 1
|
||||||
|
} catch {
|
||||||
|
result.failed += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatBulkPracticeActionToast(
|
||||||
|
action: "unlink" | "delete",
|
||||||
|
locationLabel: string,
|
||||||
|
result: BulkPracticeActionResult,
|
||||||
|
): { kind: "success" | "info" | "error"; message: string } {
|
||||||
|
const { succeeded, skipped, failed } = result
|
||||||
|
const count = succeeded + skipped + failed
|
||||||
|
|
||||||
|
if (succeeded === 0 && failed === 0 && skipped > 0) {
|
||||||
|
return {
|
||||||
|
kind: "info",
|
||||||
|
message:
|
||||||
|
action === "unlink"
|
||||||
|
? "Selected practices were already removed from this location."
|
||||||
|
: "No practices were deleted.",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failed > 0 && succeeded === 0) {
|
||||||
|
return {
|
||||||
|
kind: "error",
|
||||||
|
message:
|
||||||
|
action === "unlink"
|
||||||
|
? `Could not unlink ${failed} of ${count} selected practice${count === 1 ? "" : "s"}.`
|
||||||
|
: `Could not delete ${failed} of ${count} selected practice${count === 1 ? "" : "s"}.`,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action === "unlink") {
|
||||||
|
let message = `Removed ${succeeded} practice${succeeded === 1 ? "" : "s"} from ${locationLabel}.`
|
||||||
|
if (skipped > 0) {
|
||||||
|
message += ` ${skipped} ${skipped === 1 ? "was" : "were"} already removed or could not be matched.`
|
||||||
|
}
|
||||||
|
if (failed > 0) {
|
||||||
|
message += ` ${failed} failed.`
|
||||||
|
}
|
||||||
|
return { kind: failed > 0 ? "info" : "success", message }
|
||||||
|
}
|
||||||
|
|
||||||
|
let message = `Deleted ${succeeded} practice${succeeded === 1 ? "" : "s"}.`
|
||||||
|
if (failed > 0) {
|
||||||
|
message += ` ${failed} could not be deleted.`
|
||||||
|
}
|
||||||
|
return { kind: failed > 0 ? "info" : "success", message }
|
||||||
|
}
|
||||||
|
|
@ -62,7 +62,7 @@ import {
|
||||||
} from "../../lib/fetchAllOffsetPages";
|
} from "../../lib/fetchAllOffsetPages";
|
||||||
import { AddModuleModal } from "./components/AddModuleModal";
|
import { AddModuleModal } from "./components/AddModuleModal";
|
||||||
import { ModuleIconUploadField } from "./components/ModuleIconUploadField";
|
import { ModuleIconUploadField } from "./components/ModuleIconUploadField";
|
||||||
import { ModulePracticeCard } from "./components/ModulePracticeCard";
|
import { PracticeSelectableGrid } from "./components/PracticeSelectableGrid";
|
||||||
import { PublishPracticeButton } from "./components/PublishPracticeButton";
|
import { PublishPracticeButton } from "./components/PublishPracticeButton";
|
||||||
import { DisplayValue } from "../../lib/displayValue"
|
import { DisplayValue } from "../../lib/displayValue"
|
||||||
import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents"
|
import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents"
|
||||||
|
|
@ -938,32 +938,33 @@ export function CourseDetailPage() {
|
||||||
{practicesLoadError}
|
{practicesLoadError}
|
||||||
</div>
|
</div>
|
||||||
) : filteredPractices.length > 0 ? (
|
) : filteredPractices.length > 0 ? (
|
||||||
<div className="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-4">
|
<PracticeSelectableGrid
|
||||||
{filteredPractices.map((practice) => (
|
practices={filteredPractices}
|
||||||
<ModulePracticeCard
|
searchQuery={practiceSearch}
|
||||||
key={practice.id}
|
locationLabel={displayTitle}
|
||||||
practice={practice}
|
unlinkContext={{
|
||||||
statusUpdating={publishStatusPracticeId === practice.id}
|
scope: "course",
|
||||||
searchQuery={practiceSearch}
|
courseId: courseIdNum,
|
||||||
onEdit={() =>
|
moduleIds: courseModuleIds,
|
||||||
navigate(
|
}}
|
||||||
`/new-content/learn-english/${programIdParam}/courses/${courseIdNum}/edit-practice/${practice.id}?backTo=courses`,
|
publishStatusUpdatingId={publishStatusPracticeId}
|
||||||
)
|
onReload={loadCoursePractices}
|
||||||
}
|
unlinkActionLabel="Remove from course"
|
||||||
onPublish={() =>
|
deleteActionLabel="Delete selected"
|
||||||
void handlePracticePublishStatus(
|
onEdit={(practice) =>
|
||||||
practice.id,
|
navigate(
|
||||||
"PUBLISHED",
|
`/new-content/learn-english/${programIdParam}/courses/${courseIdNum}/edit-practice/${practice.id}?backTo=courses`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
onSaveAsDraft={() =>
|
onPublish={(practiceId) =>
|
||||||
void handlePracticePublishStatus(practice.id, "DRAFT")
|
void handlePracticePublishStatus(practiceId, "PUBLISHED")
|
||||||
}
|
}
|
||||||
onUnlink={() => setPracticeToUnlink(practice)}
|
onSaveAsDraft={(practiceId) =>
|
||||||
onDelete={() => setPracticeToDelete(practice)}
|
void handlePracticePublishStatus(practiceId, "DRAFT")
|
||||||
/>
|
}
|
||||||
))}
|
onUnlink={(practice) => setPracticeToUnlink(practice)}
|
||||||
</div>
|
onDelete={(practice) => setPracticeToDelete(practice)}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<div className="mx-auto flex max-w-4xl flex-col items-center justify-center rounded-[40px] border-2 border-dashed border-[#F1F5F9] bg-white px-4 py-32 shadow-sm">
|
<div className="mx-auto flex max-w-4xl flex-col items-center justify-center rounded-[40px] border-2 border-dashed border-[#F1F5F9] bg-white px-4 py-32 shadow-sm">
|
||||||
<div className="mb-6 flex h-20 w-20 items-center justify-center rounded-full bg-[#FAF5FF]">
|
<div className="mb-6 flex h-20 w-20 items-center justify-center rounded-full bg-[#FAF5FF]">
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ import {
|
||||||
} from "../../lib/practiceParentUnlink";
|
} from "../../lib/practiceParentUnlink";
|
||||||
import { Button } from "../../components/ui/button";
|
import { Button } from "../../components/ui/button";
|
||||||
import { PracticeActionButton } from "./components/PracticeActionButton";
|
import { PracticeActionButton } from "./components/PracticeActionButton";
|
||||||
import { ModulePracticeCard } from "./components/ModulePracticeCard";
|
import { PracticeSelectableGrid } from "./components/PracticeSelectableGrid";
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
|
|
@ -413,25 +413,26 @@ export function LessonPracticesPage() {
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-4">
|
<PracticeSelectableGrid
|
||||||
{filteredPractices.map((p) => (
|
practices={filteredPractices}
|
||||||
<ModulePracticeCard
|
searchQuery={listSearch}
|
||||||
key={p.id}
|
locationLabel={displayTitle}
|
||||||
practice={p}
|
unlinkContext={{ scope: "lesson", lessonId: lid }}
|
||||||
statusUpdating={publishStatusUpdatingId === p.id}
|
isExamPrep={isExamPrep}
|
||||||
searchQuery={listSearch}
|
publishStatusUpdatingId={publishStatusUpdatingId}
|
||||||
onEdit={() => void navigate(editPracticeHref(p.id))}
|
onReload={load}
|
||||||
onPublish={() =>
|
unlinkActionLabel="Remove from lesson"
|
||||||
void handlePracticePublishStatus(p.id, "PUBLISHED")
|
deleteActionLabel="Delete selected"
|
||||||
}
|
onEdit={(practice) => void navigate(editPracticeHref(practice.id))}
|
||||||
onSaveAsDraft={() =>
|
onPublish={(practiceId) =>
|
||||||
void handlePracticePublishStatus(p.id, "DRAFT")
|
void handlePracticePublishStatus(practiceId, "PUBLISHED")
|
||||||
}
|
}
|
||||||
onUnlink={() => setPracticeToUnlink(p)}
|
onSaveAsDraft={(practiceId) =>
|
||||||
onDelete={() => setPracticeToDelete(p)}
|
void handlePracticePublishStatus(practiceId, "DRAFT")
|
||||||
/>
|
}
|
||||||
))}
|
onUnlink={(practice) => setPracticeToUnlink(practice)}
|
||||||
</div>
|
onDelete={(practice) => setPracticeToDelete(practice)}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ import { Textarea } from "../../components/ui/textarea";
|
||||||
import { resolveThumbnailForPreview } from "../../lib/videoPreview";
|
import { resolveThumbnailForPreview } from "../../lib/videoPreview";
|
||||||
import { cn } from "../../lib/utils";
|
import { cn } from "../../lib/utils";
|
||||||
import { LessonMediaUploadField } from "./components/LessonMediaUploadField";
|
import { LessonMediaUploadField } from "./components/LessonMediaUploadField";
|
||||||
import { ModulePracticeCard } from "./components/ModulePracticeCard";
|
import { PracticeSelectableGrid } from "./components/PracticeSelectableGrid";
|
||||||
import { VideoCard } from "./components/VideoCard";
|
import { VideoCard } from "./components/VideoCard";
|
||||||
import { ContentListSearchFilterBar } from "./components/ContentListSearchFilterBar";
|
import { ContentListSearchFilterBar } from "./components/ContentListSearchFilterBar";
|
||||||
import { ContentPageDescription } from "./components/ContentPageDescription";
|
import { ContentPageDescription } from "./components/ContentPageDescription";
|
||||||
|
|
@ -746,32 +746,33 @@ export function ModuleDetailPage() {
|
||||||
{practicesLoadError}
|
{practicesLoadError}
|
||||||
</div>
|
</div>
|
||||||
) : filteredPractices.length > 0 ? (
|
) : filteredPractices.length > 0 ? (
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
|
<PracticeSelectableGrid
|
||||||
{filteredPractices.map((practice) => (
|
practices={filteredPractices}
|
||||||
<ModulePracticeCard
|
searchQuery={practiceSearch}
|
||||||
key={practice.id}
|
locationLabel={displayModuleName}
|
||||||
practice={practice}
|
unlinkContext={{
|
||||||
statusUpdating={publishStatusPracticeId === practice.id}
|
scope: "module",
|
||||||
searchQuery={practiceSearch}
|
moduleId: Number(moduleId),
|
||||||
onEdit={() =>
|
lessonIds: moduleLessonIds,
|
||||||
navigate(
|
}}
|
||||||
`/new-content/learn-english/${level}/courses/${courseId}/modules/${moduleId}/edit-practice/${practice.id}?backTo=module`,
|
publishStatusUpdatingId={publishStatusPracticeId}
|
||||||
)
|
onReload={loadModulePractices}
|
||||||
}
|
unlinkActionLabel="Remove from module"
|
||||||
onPublish={() =>
|
deleteActionLabel="Delete selected"
|
||||||
void handlePracticePublishStatus(
|
onEdit={(practice) =>
|
||||||
practice.id,
|
navigate(
|
||||||
"PUBLISHED",
|
`/new-content/learn-english/${level}/courses/${courseId}/modules/${moduleId}/edit-practice/${practice.id}?backTo=module`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
onSaveAsDraft={() =>
|
onPublish={(practiceId) =>
|
||||||
void handlePracticePublishStatus(practice.id, "DRAFT")
|
void handlePracticePublishStatus(practiceId, "PUBLISHED")
|
||||||
}
|
}
|
||||||
onUnlink={() => setPracticeToUnlink(practice)}
|
onSaveAsDraft={(practiceId) =>
|
||||||
onDelete={() => setPracticeToDelete(practice)}
|
void handlePracticePublishStatus(practiceId, "DRAFT")
|
||||||
/>
|
}
|
||||||
))}
|
onUnlink={(practice) => setPracticeToUnlink(practice)}
|
||||||
</div>
|
onDelete={(practice) => setPracticeToDelete(practice)}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex flex-col items-center justify-center py-32 px-4 rounded-[40px] border-2 border-dashed border-[#F1F5F9] bg-white max-w-4xl mx-auto shadow-sm">
|
<div className="flex flex-col items-center justify-center py-32 px-4 rounded-[40px] border-2 border-dashed border-[#F1F5F9] bg-white max-w-4xl mx-auto shadow-sm">
|
||||||
<div className="h-20 w-20 rounded-full bg-[#FAF5FF] flex items-center justify-center mb-6">
|
<div className="h-20 w-20 rounded-full bg-[#FAF5FF] flex items-center justify-center mb-6">
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ import {
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from "../../../components/ui/dialog"
|
} from "../../../components/ui/dialog"
|
||||||
import { ContentListSearchFilterBar } from "./ContentListSearchFilterBar"
|
import { ContentListSearchFilterBar } from "./ContentListSearchFilterBar"
|
||||||
import { ModulePracticeCard } from "./ModulePracticeCard"
|
import { PracticeSelectableGrid } from "./PracticeSelectableGrid"
|
||||||
import {
|
import {
|
||||||
filterBySearchAndPublishStatus,
|
filterBySearchAndPublishStatus,
|
||||||
type PublishStatusFilter,
|
type PublishStatusFilter,
|
||||||
|
|
@ -249,21 +249,26 @@ export function CatalogCoursePracticesPanel({
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-4">
|
<PracticeSelectableGrid
|
||||||
{filteredPractices.map((practice) => (
|
practices={filteredPractices}
|
||||||
<ModulePracticeCard
|
searchQuery={listSearch}
|
||||||
key={practice.id}
|
locationLabel={courseName}
|
||||||
practice={practice}
|
unlinkContext={{ scope: "catalog_course", catalogCourseId }}
|
||||||
statusUpdating={publishStatusUpdatingId === practice.id}
|
isExamPrep
|
||||||
searchQuery={listSearch}
|
publishStatusUpdatingId={publishStatusUpdatingId}
|
||||||
onEdit={() => navigate(editPracticeHref(practice.id))}
|
onReload={load}
|
||||||
onPublish={() => void handlePracticePublishStatus(practice.id, "PUBLISHED")}
|
unlinkActionLabel="Remove from course"
|
||||||
onSaveAsDraft={() => void handlePracticePublishStatus(practice.id, "DRAFT")}
|
deleteActionLabel="Delete selected"
|
||||||
onUnlink={() => setPracticeToUnlink(practice)}
|
onEdit={(practice) => navigate(editPracticeHref(practice.id))}
|
||||||
onDelete={() => setPracticeToDelete(practice)}
|
onPublish={(practiceId) =>
|
||||||
/>
|
void handlePracticePublishStatus(practiceId, "PUBLISHED")
|
||||||
))}
|
}
|
||||||
</div>
|
onSaveAsDraft={(practiceId) =>
|
||||||
|
void handlePracticePublishStatus(practiceId, "DRAFT")
|
||||||
|
}
|
||||||
|
onUnlink={(practice) => setPracticeToUnlink(practice)}
|
||||||
|
onDelete={(practice) => setPracticeToDelete(practice)}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Dialog
|
<Dialog
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,9 @@ type ModulePracticeCardProps = {
|
||||||
onSaveAsDraft?: () => void;
|
onSaveAsDraft?: () => void;
|
||||||
onUnlink?: () => void;
|
onUnlink?: () => void;
|
||||||
onDelete?: () => void;
|
onDelete?: () => void;
|
||||||
|
selectable?: boolean;
|
||||||
|
selected?: boolean;
|
||||||
|
onSelectedChange?: (selected: boolean) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function ModulePracticeCard({
|
export function ModulePracticeCard({
|
||||||
|
|
@ -42,6 +45,9 @@ export function ModulePracticeCard({
|
||||||
onSaveAsDraft,
|
onSaveAsDraft,
|
||||||
onUnlink,
|
onUnlink,
|
||||||
onDelete,
|
onDelete,
|
||||||
|
selectable = false,
|
||||||
|
selected = false,
|
||||||
|
onSelectedChange,
|
||||||
}: ModulePracticeCardProps) {
|
}: ModulePracticeCardProps) {
|
||||||
const isPublished = isPracticePublished(practice);
|
const isPublished = isPracticePublished(practice);
|
||||||
const statusLabel = practicePublishStatus(practice) ?? "DRAFT";
|
const statusLabel = practicePublishStatus(practice) ?? "DRAFT";
|
||||||
|
|
@ -81,8 +87,25 @@ export function ModulePracticeCard({
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Card className="group flex flex-col overflow-hidden rounded-[20px] border border-grayScale-50 bg-white shadow-sm transition-all hover:shadow-xl hover:shadow-grayScale-400/5">
|
<Card className={cn(
|
||||||
|
"group flex flex-col overflow-hidden rounded-[20px] border bg-white shadow-sm transition-all hover:shadow-xl hover:shadow-grayScale-400/5",
|
||||||
|
selected ? "border-brand-400 ring-2 ring-brand-200" : "border-grayScale-50",
|
||||||
|
)}>
|
||||||
<div className="relative h-44 w-full overflow-hidden bg-gradient-to-br from-[#E0F2FE] to-[#BFDBFE]">
|
<div className="relative h-44 w-full overflow-hidden bg-gradient-to-br from-[#E0F2FE] to-[#BFDBFE]">
|
||||||
|
{selectable ? (
|
||||||
|
<label className="absolute left-3 top-3 z-10 flex h-8 w-8 cursor-pointer items-center justify-center rounded-[6px] bg-white/95 shadow-sm">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
className="h-4 w-4 rounded border-grayScale-300 text-brand-500 focus:ring-brand-500"
|
||||||
|
checked={selected}
|
||||||
|
onChange={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
onSelectedChange?.(e.target.checked);
|
||||||
|
}}
|
||||||
|
aria-label={`Select ${practice.title}`}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
) : null}
|
||||||
{thumbnailSrc && !thumbFailed ? (
|
{thumbnailSrc && !thumbFailed ? (
|
||||||
<ResolvedImage
|
<ResolvedImage
|
||||||
src={thumbnailSrc}
|
src={thumbnailSrc}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,260 @@
|
||||||
|
import { useState } from "react"
|
||||||
|
import { toast } from "sonner"
|
||||||
|
import { notifyApiError } from "../../../lib/apiErrors"
|
||||||
|
import {
|
||||||
|
bulkDeletePractices,
|
||||||
|
bulkUnlinkPractices,
|
||||||
|
formatBulkPracticeActionToast,
|
||||||
|
type PracticeUnlinkContext,
|
||||||
|
} from "../../../lib/practiceBulkActions"
|
||||||
|
import { usePracticeListSelection } from "../../../hooks/usePracticeListSelection"
|
||||||
|
import type { ParentContextPractice } from "../../../types/course.types"
|
||||||
|
import { Button } from "../../../components/ui/button"
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
} from "../../../components/ui/dialog"
|
||||||
|
import { ModulePracticeCard } from "./ModulePracticeCard"
|
||||||
|
|
||||||
|
type PracticeSelectableGridProps = {
|
||||||
|
practices: ParentContextPractice[]
|
||||||
|
searchQuery?: string
|
||||||
|
locationLabel: string
|
||||||
|
unlinkContext: PracticeUnlinkContext
|
||||||
|
isExamPrep?: boolean
|
||||||
|
publishStatusUpdatingId?: number | null
|
||||||
|
onReload: () => Promise<void>
|
||||||
|
onEdit: (practice: ParentContextPractice) => void
|
||||||
|
onPublish: (practiceId: number) => void
|
||||||
|
onSaveAsDraft: (practiceId: number) => void
|
||||||
|
onUnlink: (practice: ParentContextPractice) => void
|
||||||
|
onDelete: (practice: ParentContextPractice) => void
|
||||||
|
unlinkActionLabel?: string
|
||||||
|
deleteActionLabel?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
function summarizePracticeTitles(practices: ParentContextPractice[]): string {
|
||||||
|
const titles = practices
|
||||||
|
.slice(0, 3)
|
||||||
|
.map((practice) => practice.title?.trim() || `Practice #${practice.id}`)
|
||||||
|
if (practices.length <= 3) {
|
||||||
|
return titles.join(", ")
|
||||||
|
}
|
||||||
|
return `${titles.join(", ")}, and ${practices.length - 3} more`
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PracticeSelectableGrid({
|
||||||
|
practices,
|
||||||
|
searchQuery = "",
|
||||||
|
locationLabel,
|
||||||
|
unlinkContext,
|
||||||
|
isExamPrep = false,
|
||||||
|
publishStatusUpdatingId = null,
|
||||||
|
onReload,
|
||||||
|
onEdit,
|
||||||
|
onPublish,
|
||||||
|
onSaveAsDraft,
|
||||||
|
onUnlink,
|
||||||
|
onDelete,
|
||||||
|
unlinkActionLabel = "Remove from location",
|
||||||
|
deleteActionLabel = "Delete permanently",
|
||||||
|
}: PracticeSelectableGridProps) {
|
||||||
|
const selection = usePracticeListSelection(practices)
|
||||||
|
const [bulkUnlinkOpen, setBulkUnlinkOpen] = useState(false)
|
||||||
|
const [bulkDeleteOpen, setBulkDeleteOpen] = useState(false)
|
||||||
|
const [bulkUnlinking, setBulkUnlinking] = useState(false)
|
||||||
|
const [bulkDeleting, setBulkDeleting] = useState(false)
|
||||||
|
|
||||||
|
const runBulkUnlink = async () => {
|
||||||
|
if (selection.selectedPractices.length === 0) return
|
||||||
|
setBulkUnlinking(true)
|
||||||
|
try {
|
||||||
|
const result = await bulkUnlinkPractices({
|
||||||
|
practices: selection.selectedPractices,
|
||||||
|
context: unlinkContext,
|
||||||
|
isExamPrep,
|
||||||
|
})
|
||||||
|
const toastResult = formatBulkPracticeActionToast(
|
||||||
|
"unlink",
|
||||||
|
locationLabel,
|
||||||
|
result,
|
||||||
|
)
|
||||||
|
if (toastResult.kind === "success") {
|
||||||
|
toast.success(toastResult.message)
|
||||||
|
} else if (toastResult.kind === "info") {
|
||||||
|
toast.info(toastResult.message)
|
||||||
|
} else {
|
||||||
|
toast.error(toastResult.message)
|
||||||
|
}
|
||||||
|
setBulkUnlinkOpen(false)
|
||||||
|
selection.clear()
|
||||||
|
await onReload()
|
||||||
|
} catch (error) {
|
||||||
|
notifyApiError(error, "Could not unlink selected practices")
|
||||||
|
} finally {
|
||||||
|
setBulkUnlinking(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const runBulkDelete = async () => {
|
||||||
|
if (selection.selectedPractices.length === 0) return
|
||||||
|
setBulkDeleting(true)
|
||||||
|
try {
|
||||||
|
const result = await bulkDeletePractices({
|
||||||
|
practices: selection.selectedPractices,
|
||||||
|
isExamPrep,
|
||||||
|
})
|
||||||
|
const toastResult = formatBulkPracticeActionToast(
|
||||||
|
"delete",
|
||||||
|
locationLabel,
|
||||||
|
result,
|
||||||
|
)
|
||||||
|
if (toastResult.kind === "success") {
|
||||||
|
toast.success(toastResult.message)
|
||||||
|
} else if (toastResult.kind === "info") {
|
||||||
|
toast.info(toastResult.message)
|
||||||
|
} else {
|
||||||
|
toast.error(toastResult.message)
|
||||||
|
}
|
||||||
|
setBulkDeleteOpen(false)
|
||||||
|
selection.clear()
|
||||||
|
await onReload()
|
||||||
|
} catch (error) {
|
||||||
|
notifyApiError(error, "Could not delete selected practices")
|
||||||
|
} finally {
|
||||||
|
setBulkDeleting(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="flex flex-wrap items-center gap-3 rounded-xl border border-grayScale-100 bg-grayScale-50/60 px-4 py-3">
|
||||||
|
<label className="flex cursor-pointer items-center gap-2 text-sm font-medium text-grayScale-700">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
className="h-4 w-4 rounded border-grayScale-300 text-brand-500 focus:ring-brand-500"
|
||||||
|
checked={selection.allSelected}
|
||||||
|
onChange={() => selection.toggleAll()}
|
||||||
|
aria-label="Select all visible practices"
|
||||||
|
/>
|
||||||
|
Select all ({selection.selectedCount}/{practices.length})
|
||||||
|
</label>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
className="rounded-[6px]"
|
||||||
|
disabled={selection.selectedCount === 0 || bulkUnlinking || bulkDeleting}
|
||||||
|
onClick={() => setBulkUnlinkOpen(true)}
|
||||||
|
>
|
||||||
|
Unlink selected ({selection.selectedCount})
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="destructive"
|
||||||
|
size="sm"
|
||||||
|
className="rounded-[6px]"
|
||||||
|
disabled={selection.selectedCount === 0 || bulkUnlinking || bulkDeleting}
|
||||||
|
onClick={() => setBulkDeleteOpen(true)}
|
||||||
|
>
|
||||||
|
Delete selected ({selection.selectedCount})
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-4">
|
||||||
|
{practices.map((practice) => (
|
||||||
|
<ModulePracticeCard
|
||||||
|
key={practice.id}
|
||||||
|
practice={practice}
|
||||||
|
statusUpdating={publishStatusUpdatingId === practice.id}
|
||||||
|
searchQuery={searchQuery}
|
||||||
|
selectable
|
||||||
|
selected={selection.isSelected(practice.id)}
|
||||||
|
onSelectedChange={() => selection.toggle(practice.id)}
|
||||||
|
onEdit={() => onEdit(practice)}
|
||||||
|
onPublish={() => onPublish(practice.id)}
|
||||||
|
onSaveAsDraft={() => onSaveAsDraft(practice.id)}
|
||||||
|
onUnlink={() => onUnlink(practice)}
|
||||||
|
onDelete={() => onDelete(practice)}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Dialog
|
||||||
|
open={bulkUnlinkOpen}
|
||||||
|
onOpenChange={(open) => {
|
||||||
|
if (!open && !bulkUnlinking) setBulkUnlinkOpen(false)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<DialogContent className="max-w-md rounded-2xl">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>
|
||||||
|
Remove {selection.selectedCount} practice
|
||||||
|
{selection.selectedCount === 1 ? "" : "s"} from {locationLabel}?
|
||||||
|
</DialogTitle>
|
||||||
|
<DialogDescription>
|
||||||
|
{summarizePracticeTitles(selection.selectedPractices)} will be
|
||||||
|
detached from this location. Questions are kept unless you delete
|
||||||
|
the practices afterward.
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogFooter className="gap-2 border-t border-grayScale-100 px-6 py-4 sm:justify-end">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
disabled={bulkUnlinking}
|
||||||
|
onClick={() => setBulkUnlinkOpen(false)}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
disabled={bulkUnlinking}
|
||||||
|
onClick={() => void runBulkUnlink()}
|
||||||
|
>
|
||||||
|
{bulkUnlinking ? "Removing…" : unlinkActionLabel}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
|
<Dialog
|
||||||
|
open={bulkDeleteOpen}
|
||||||
|
onOpenChange={(open) => {
|
||||||
|
if (!open && !bulkDeleting) setBulkDeleteOpen(false)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<DialogContent className="max-w-md rounded-2xl">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>
|
||||||
|
Delete {selection.selectedCount} practice
|
||||||
|
{selection.selectedCount === 1 ? "" : "s"} permanently?
|
||||||
|
</DialogTitle>
|
||||||
|
<DialogDescription>
|
||||||
|
{summarizePracticeTitles(selection.selectedPractices)} and all of
|
||||||
|
their questions will be deleted. This cannot be undone.
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogFooter className="gap-2 border-t border-grayScale-100 px-6 py-4 sm:justify-end">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
disabled={bulkDeleting}
|
||||||
|
onClick={() => setBulkDeleteOpen(false)}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
className="bg-red-500 hover:bg-red-600"
|
||||||
|
disabled={bulkDeleting}
|
||||||
|
onClick={() => void runBulkDelete()}
|
||||||
|
>
|
||||||
|
{bulkDeleting ? "Deleting…" : deleteActionLabel}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -25,7 +25,7 @@ import {
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from "../../../components/ui/dialog"
|
} from "../../../components/ui/dialog"
|
||||||
import { ContentListSearchFilterBar } from "./ContentListSearchFilterBar"
|
import { ContentListSearchFilterBar } from "./ContentListSearchFilterBar"
|
||||||
import { ModulePracticeCard } from "./ModulePracticeCard"
|
import { PracticeSelectableGrid } from "./PracticeSelectableGrid"
|
||||||
import {
|
import {
|
||||||
filterBySearchAndPublishStatus,
|
filterBySearchAndPublishStatus,
|
||||||
type PublishStatusFilter,
|
type PublishStatusFilter,
|
||||||
|
|
@ -248,21 +248,26 @@ export function UnitPracticesPanel({
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-4">
|
<PracticeSelectableGrid
|
||||||
{filteredPractices.map((practice) => (
|
practices={filteredPractices}
|
||||||
<ModulePracticeCard
|
searchQuery={listSearch}
|
||||||
key={practice.id}
|
locationLabel={unitName}
|
||||||
practice={practice}
|
unlinkContext={{ scope: "unit", unitId }}
|
||||||
statusUpdating={publishStatusUpdatingId === practice.id}
|
isExamPrep
|
||||||
searchQuery={listSearch}
|
publishStatusUpdatingId={publishStatusUpdatingId}
|
||||||
onEdit={() => navigate(editPracticeHref(practice.id))}
|
onReload={load}
|
||||||
onPublish={() => void handlePracticePublishStatus(practice.id, "PUBLISHED")}
|
unlinkActionLabel="Remove from unit"
|
||||||
onSaveAsDraft={() => void handlePracticePublishStatus(practice.id, "DRAFT")}
|
deleteActionLabel="Delete selected"
|
||||||
onUnlink={() => setPracticeToUnlink(practice)}
|
onEdit={(practice) => navigate(editPracticeHref(practice.id))}
|
||||||
onDelete={() => setPracticeToDelete(practice)}
|
onPublish={(practiceId) =>
|
||||||
/>
|
void handlePracticePublishStatus(practiceId, "PUBLISHED")
|
||||||
))}
|
}
|
||||||
</div>
|
onSaveAsDraft={(practiceId) =>
|
||||||
|
void handlePracticePublishStatus(practiceId, "DRAFT")
|
||||||
|
}
|
||||||
|
onUnlink={(practice) => setPracticeToUnlink(practice)}
|
||||||
|
onDelete={(practice) => setPracticeToDelete(practice)}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Dialog
|
<Dialog
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user