@@ -31,16 +31,19 @@ import {
3131import Link from "next/link" ;
3232import {
3333 BUTTON_NEW_LESSON_TEXT ,
34+ BUTTON_NEW_LESSON_TEXT_DOWNLOAD ,
3435 COURSE_CONTENT_HEADER ,
3536 EDIT_SECTION_HEADER ,
37+ LESSON_GROUP_DELETED ,
3638 MANAGE_COURSES_PAGE_HEADING ,
3739 TOAST_TITLE_ERROR ,
40+ TOAST_TITLE_SUCCESS ,
3841} from "@ui-config/strings" ;
3942import DashboardContent from "@components/admin/dashboard-content" ;
4043import { AddressContext } from "@components/contexts" ;
4144import useProduct from "../../../../../../../hooks/use-product" ;
4245import { truncate } from "@ui-lib/utils" ;
43- import { Lesson } from "@courselit/common-models" ;
46+ import { Constants , Lesson } from "@courselit/common-models" ;
4447import { DragAndDrop , useToast } from "@courselit/components-library" ;
4548import { FetchBuilder } from "@courselit/utils" ;
4649import {
@@ -53,7 +56,10 @@ import { Droplets } from "lucide-react";
5356
5457export default function ContentPage ( ) {
5558 const [ deleteDialogOpen , setDeleteDialogOpen ] = useState ( false ) ;
56- const [ itemToDelete , setItemToDelete ] = useState ( null ) ;
59+ const [ itemToDelete , setItemToDelete ] = useState < Record <
60+ string ,
61+ string
62+ > | null > ( null ) ;
5763 const [ collapsedSections , setCollapsedSections ] = useState < string [ ] > ( [ ] ) ;
5864 const [ hoveredSectionIndex , setHoveredSectionIndex ] = useState <
5965 number | null
@@ -73,9 +79,10 @@ export default function ContentPage() {
7379 ] ;
7480 const { toast } = useToast ( ) ;
7581
76- const handleDelete = ( ) => {
82+ const handleDelete = async ( ) => {
7783 setDeleteDialogOpen ( false ) ;
7884 setItemToDelete ( null ) ;
85+ await removeGroup ( itemToDelete ?. id ! , product ?. courseId ! ) ;
7986 } ;
8087
8188 const toggleSectionCollapse = ( sectionId : string ) => {
@@ -135,6 +142,49 @@ export default function ContentPage() {
135142 }
136143 } ;
137144
145+ const removeGroup = async ( groupId : string , courseId : string ) => {
146+ const mutation = `
147+ mutation RemoveGroup ($id: String!, $courseId: String!) {
148+ removeGroup(
149+ id: $id,
150+ courseId: $courseId
151+ ) {
152+ courseId
153+ }
154+ }
155+ ` ;
156+ const fetch = new FetchBuilder ( )
157+ . setUrl ( `${ address . backend } /api/graph` )
158+ . setPayload ( {
159+ query : mutation ,
160+ variables : {
161+ id : groupId ,
162+ courseId : courseId ,
163+ } ,
164+ } )
165+ . setIsGraphQLEndpoint ( true )
166+ . build ( ) ;
167+ try {
168+ const response = await fetch . exec ( ) ;
169+ if ( response . removeGroup ?. courseId ) {
170+ toast ( {
171+ title : TOAST_TITLE_SUCCESS ,
172+ description : LESSON_GROUP_DELETED ,
173+ } ) ;
174+ // course.groups.splice(
175+ // course.groups.findIndex((group) => group.id === groupId),
176+ // 1,
177+ // );
178+ }
179+ } catch ( err : any ) {
180+ toast ( {
181+ title : TOAST_TITLE_ERROR ,
182+ description : err . message ,
183+ variant : "destructive" ,
184+ } ) ;
185+ }
186+ } ;
187+
138188 return (
139189 < DashboardContent breadcrumbs = { breadcrumbs } >
140190 < h1 className = "text-4xl font-semibold tracking-tight mb-8" >
@@ -215,19 +265,28 @@ export default function ContentPage() {
215265 >
216266 Add Section Below
217267 </DropdownMenuItem> */ }
218- < DropdownMenuSeparator />
219- < DropdownMenuItem
220- onClick = { ( ) => {
221- setItemToDelete ( {
222- type : "section" ,
223- title : section . name ,
224- } ) ;
225- setDeleteDialogOpen ( true ) ;
226- } }
227- className = "text-red-600"
228- >
229- Delete Section
230- </ DropdownMenuItem >
268+ { ! (
269+ product ?. type ?. toLowerCase ( ) ===
270+ Constants . CourseType . DOWNLOAD &&
271+ product ?. groups ?. length === 1
272+ ) && (
273+ < >
274+ < DropdownMenuSeparator />
275+ < DropdownMenuItem
276+ onClick = { ( ) => {
277+ setItemToDelete ( {
278+ type : "section" ,
279+ title : section . name ,
280+ id : section . id ,
281+ } ) ;
282+ setDeleteDialogOpen ( true ) ;
283+ } }
284+ className = "text-red-600"
285+ >
286+ Delete Section
287+ </ DropdownMenuItem >
288+ </ >
289+ ) }
231290 </ DropdownMenuContent >
232291 </ DropdownMenu >
233292 </ div >
@@ -307,7 +366,10 @@ export default function ContentPage() {
307366 href = { `/dashboard/product/${ productId } /content/section/${ section . id } /lesson` }
308367 >
309368 < Plus className = "mr-2 h-4 w-4" />
310- { BUTTON_NEW_LESSON_TEXT }
369+ { product ?. type ?. toLowerCase ( ) ===
370+ Constants . CourseType . DOWNLOAD
371+ ? BUTTON_NEW_LESSON_TEXT_DOWNLOAD
372+ : BUTTON_NEW_LESSON_TEXT }
311373 </ Link >
312374 </ Button >
313375 </ div >
@@ -337,20 +399,23 @@ export default function ContentPage() {
337399 ) }
338400 </ div >
339401 ) ) }
340- < div className = "mt-8 flex justify-center" >
341- < Button
342- variant = "outline"
343- className = "text-sm font-medium"
344- asChild
345- >
346- < Link
347- href = { `/dashboard/product/${ productId } /content/section/new` }
402+ { product ?. type ?. toLowerCase ( ) !==
403+ Constants . CourseType . DOWNLOAD && (
404+ < div className = "mt-8 flex justify-center" >
405+ < Button
406+ variant = "outline"
407+ className = "text-sm font-medium"
408+ asChild
348409 >
349- < Plus className = "mr-2 h-4 w-4" />
350- Add Section
351- </ Link >
352- </ Button >
353- </ div >
410+ < Link
411+ href = { `/dashboard/product/${ productId } /content/section/new` }
412+ >
413+ < Plus className = "mr-2 h-4 w-4" />
414+ Add Section
415+ </ Link >
416+ </ Button >
417+ </ div >
418+ ) }
354419 </ ScrollArea >
355420
356421 < Dialog open = { deleteDialogOpen } onOpenChange = { setDeleteDialogOpen } >
0 commit comments