import { Button } from '@/components/ui/button'; import { SharedData } from '@/types/global'; import { Link, router, usePage } from '@inertiajs/react'; import { CourseDetailsProps } from '../show'; // Separate component for the play button to reduce duplication const EnabledPlayButton = ({ watchHistory }: { watchHistory: WatchHistory }) => { const { props } = usePage(); const { translate } = props; const { frontend } = translate; return ( ); }; // Disabled play button component const DisabledPlayButton = ({ course, approvalStatus }: { course: Course; approvalStatus: CourseApprovalValidation }) => { const { props } = usePage(); const { translate } = props; const { frontend } = translate; const approve_able = approvalStatus.approve_able; return approve_able ? ( ) : ( ); }; // Enrollment/Buy button component const EnrollmentButton = ({ auth, course }: { auth: Auth; course: Course }) => { const { props } = usePage(); const { translate } = props; const { frontend } = translate; const enrollmentHandler = (course: Course) => { router.post(route('enrollments.store'), { user_id: auth.user?.id, course_id: course.id, enrollment_type: 'free', }); }; return course.pricing_type === 'free' ? ( ) : ( ); }; const EnrollOrPlayerButton = () => { const { auth, course, enrollment, watchHistory, approvalStatus, wishlists, translate, system } = usePage().props; const { frontend } = translate; const showWishlist = system.fields?.show_student_wishlist !== false; // Compute access conditions - improves readability const isEnrolled = !!enrollment; const hasWatchHistory = !!watchHistory; const isAdminOrInstructor = auth.user && ['admin', 'instructor'].includes(auth.user.role); const canPlay = hasWatchHistory && (isAdminOrInstructor || isEnrolled); const isWishlisted = wishlists.find((wishlist) => wishlist.course_id === course.id); const handleWishlist = () => { if (isWishlisted) { router.delete(route('course-wishlists.destroy', { id: isWishlisted.id })); } else { router.post(route('course-wishlists.store', { user_id: auth.user?.id, course_id: course.id })); } }; // Render the appropriate button based on conditions if (canPlay) { return ; } else if (isAdminOrInstructor) { return ; } else { return ( <> {showWishlist && ( )} ); } }; export default EnrollOrPlayerButton;