import { useEffect, useState } from 'react'; import { getErrorSummary } from '../../api/errors'; import { getWpApi } from '../../api'; import { appViewHref } from '../../lib/appUrl'; import { useAdminRouting } from '../../lib/adminRouting'; import type { SikshyaReactConfig } from '../../types'; import { ButtonPrimary } from './buttons'; import { Modal } from './Modal'; import { __, sprintf } from '../../lib/i18n'; type Props = { config: SikshyaReactConfig; open: boolean; onClose: () => void; }; export function CreateCertificateModal({ config, open, onClose }: Props) { const { navigateHref } = useAdminRouting(); const [title, setTitle] = useState(''); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (!open) { return; } setTitle(''); setSubmitting(false); setError(null); }, [open]); const handleClose = () => { if (!submitting) { onClose(); } }; const onSubmit = async (e: React.FormEvent) => { e.preventDefault(); const name = title.trim(); if (!name) { return; } setSubmitting(true); setError(null); try { const created = await getWpApi().post<{ id: number }>('/sikshya_certificate', { title: name, status: 'draft', }); if (!created?.id) { throw new Error(__('Could not create certificate.', 'sikshya')); } navigateHref( appViewHref(config, 'edit-content', { post_type: 'sikshya_certificate', post_id: String(created.id), }) ); } catch (err) { setError(getErrorSummary(err)); setSubmitting(false); } }; return ( {submitting ? __('Opening builder…', 'sikshya') : __('Build certificate', 'sikshya')} } >

{__('This becomes the template name shown in your certificates list.', 'sikshya')}

setTitle(e.target.value)} placeholder={__('e.g. Course Completion Certificate', 'sikshya')} disabled={submitting} className="mt-2 block w-full rounded-xl border border-slate-200 bg-white px-4 py-3 text-sm text-slate-900 shadow-sm placeholder:text-slate-400 focus:border-brand-500 focus:outline-none focus:ring-2 focus:ring-brand-500/20 disabled:cursor-not-allowed disabled:opacity-50 dark:border-slate-600 dark:bg-slate-800 dark:text-white dark:placeholder:text-slate-500" />
{error ? (
{error}
) : null}

{sprintf( __('The certificate is created as a %s and opened directly in the builder.', 'sikshya'), __('draft', 'sikshya') )}

); }