@@ -4,8 +4,10 @@ import { PointerSensor, KeyboardSensor } from "@dnd-kit/dom";
44import { DragDropProvider , DragDropEventHandlers } from "@dnd-kit/react" ;
55import { isSortable } from "@dnd-kit/react/sortable" ;
66
7+ import { v4 as uuidv4 } from "uuid" ;
8+
79import { useCourseWorkspace } from "@/core/workspace/useCourseWorkspace" ;
8- import { UserCourse } from "@/lib/types" ;
10+ import { APICourse , UserCourse } from "@/lib/types" ;
911
1012export default function WorkspaceDndProvider ( {
1113 children,
@@ -85,6 +87,9 @@ export default function WorkspaceDndProvider({
8587 const targetType = target . data ?. type ;
8688 const sourceCourse = source . data ?. course as UserCourse | undefined ;
8789
90+ // Catalog courses are handled in onDragEnd only
91+ if ( sourceType === "catalog-course" ) return ;
92+
8893 if ( ! sourceCourse ) return ;
8994
9095 let targetIndex ;
@@ -175,6 +180,40 @@ export default function WorkspaceDndProvider({
175180 const sourceType = source . data ?. type ;
176181 const targetType = target . data ?. type ;
177182
183+ // --- CATALOG TO PLANNER ---
184+ if ( sourceType === "catalog-course" ) {
185+ const apiCourse = source . data ?. course as APICourse | undefined ;
186+ if ( ! apiCourse ) return ;
187+
188+ const targetSemesterId =
189+ targetType === "semester"
190+ ? target . data ?. semesterId || targetId
191+ : courseLocationMap . get ( targetId ) ?. semesterId ;
192+
193+ if ( ! targetSemesterId ) return ;
194+
195+ let targetIndex ;
196+ if ( isSortable ( target ) ) {
197+ targetIndex = target . index ;
198+ } else {
199+ const targetSemester =
200+ plannerCourses [
201+ courseLocationMap . get ( targetId ) ?. semesterIndex || - 1
202+ ] ;
203+ targetIndex = targetSemester ?. courseList . length || 0 ;
204+ }
205+
206+ const newCourse : UserCourse = {
207+ id : uuidv4 ( ) ,
208+ name : `${ apiCourse . subj_code } ${ apiCourse . code_num } ` ,
209+ count : 1 ,
210+ data : apiCourse ,
211+ } ;
212+
213+ addCourseToSemester ( targetSemesterId , newCourse , targetIndex ) ;
214+ return ;
215+ }
216+
178217 // --- UTILITY DROPZONES ---
179218 if ( targetId === "garbage" ) {
180219 const semId = courseLocationMap . get ( sourceId ) ?. semesterId ;
@@ -223,6 +262,7 @@ export default function WorkspaceDndProvider({
223262 resetToolbox ,
224263 consolidateToolbox ,
225264 courseLocationMap ,
265+ addCourseToSemester ,
226266 removeCourseFromSemester ,
227267 removeCourseFromToolbox ,
228268 insertCourseIntoToolbox ,
0 commit comments