import FormLayout, { FormField, FormLabelAndMessage, Input, Textarea, } from '@components/Objects/StyledElements/Form/Form'; import { useFormik } from 'formik'; import { AlertTriangle, Award, CheckCircle, FileText, Settings } from 'lucide-react'; import CertificatePreview from './CertificatePreview'; import * as Form from '@radix-ui/react-form'; import React, { useEffect, useState } from 'react'; import { useCourse, useCourseDispatch } from '@components/Contexts/CourseContext'; import { CustomSelect, CustomSelectContent, CustomSelectItem, CustomSelectTrigger, CustomSelectValue, } from "../EditCourseGeneral/CustomSelect"; type EditCourseCertificationProps = { orgslug: string course_uuid?: string } const validate = (values: any) => { const errors = {} as any; if (values.enable_certification && !values.certification_name) { errors.certification_name = 'Required when certification is enabled'; } else if (values.certification_name && values.certification_name.length > 100) { errors.certification_name = 'Must be 100 characters or less'; } if (values.enable_certification && !values.certification_description) { errors.certification_description = 'Required when certification is enabled'; } else if (values.certification_description && values.certification_description.length > 500) { errors.certification_description = 'Must be 500 characters or less'; } return errors; }; function EditCourseCertification(props: EditCourseCertificationProps) { const [error, setError] = useState(''); const course = useCourse(); const dispatchCourse = useCourseDispatch() as any; const { isLoading, courseStructure } = course as any; // Create initial values object const getInitialValues = () => { // Helper function to get instructor name from authors const getInstructorName = () => { if (courseStructure?.authors && courseStructure.authors.length > 0) { const author = courseStructure.authors[0]; const firstName = author.first_name || ''; const lastName = author.last_name || ''; // Only return if at least one name exists if (firstName || lastName) { return `${firstName} ${lastName}`.trim(); } } return ''; }; return { enable_certification: courseStructure?.enable_certification || false, certification_name: courseStructure?.certification_name || courseStructure?.name || '', certification_description: courseStructure?.certification_description || courseStructure?.description || '', certification_type: courseStructure?.certification_type || 'completion', certificate_pattern: courseStructure?.certificate_pattern || 'professional', certificate_instructor: courseStructure?.certificate_instructor || getInstructorName(), }; }; const formik = useFormik({ initialValues: getInitialValues(), validate, onSubmit: async values => { try { // Add your submission logic here dispatchCourse({ type: 'setIsSaved' }); } catch (e) { setError('Failed to save certification settings.'); } }, enableReinitialize: true, }) as any; // Reset form when courseStructure changes useEffect(() => { if (courseStructure && !isLoading) { const newValues = getInitialValues(); formik.resetForm({ values: newValues }); } }, [courseStructure, isLoading]); useEffect(() => { if (!isLoading) { const formikValues = formik.values as any; const initialValues = formik.initialValues as any; const valuesChanged = Object.keys(formikValues).some( key => formikValues[key] !== initialValues[key] ); if (valuesChanged) { dispatchCourse({ type: 'setIsNotSaved' }); const updatedCourse = { ...courseStructure, ...formikValues, }; dispatchCourse({ type: 'setCourseStructure', payload: updatedCourse }); } } }, [formik.values, isLoading]); if (isLoading || !courseStructure) { return
Loading...
; } return (
{courseStructure && (
{/* Header Section */}

Course Certification

Enable and configure certificates for students who complete this course

{error && (
{error}
)} {/* Certification Configuration */} {formik.values.enable_certification && (
{/* Form Section */}
{/* Basic Information Section */}

Basic Information

Configure the basic details of your certification

{/* Certification Name */} {/* Certification Type */} { if (!value) return; formik.setFieldValue('certification_type', value); }} > {formik.values.certification_type === 'completion' ? 'Course Completion' : formik.values.certification_type === 'achievement' ? 'Achievement Based' : formik.values.certification_type === 'assessment' ? 'Assessment Based' : formik.values.certification_type === 'participation' ? 'Participation' : formik.values.certification_type === 'mastery' ? 'Skill Mastery' : formik.values.certification_type === 'professional' ? 'Professional Development' : formik.values.certification_type === 'continuing' ? 'Continuing Education' : formik.values.certification_type === 'workshop' ? 'Workshop Attendance' : formik.values.certification_type === 'specialization' ? 'Specialization' : 'Course Completion'} Course Completion Achievement Based Assessment Based Participation Skill Mastery Professional Development Continuing Education Workshop Attendance Specialization
{/* Certification Description */}