Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 'use client' import { useState } from 'react' import type { WorksheetFormState } from '@/app/create/worksheets/types' import { validateWorksheetConfig } from '../validation' type GenerationStatus = 'idle' | 'generating' | 'error' interface UseWorksheetGenerationReturn { status: GenerationStatus error: string | null generate: (config: WorksheetFormState) => Promise<void> reset: () => void } /** * Handle PDF generation workflow * * Features: * - Status tracking ('idle', 'generating', 'error') * - Validation before generation * - API call to generate PDF * - Automatic download of generated PDF * - Error handling with detailed messages */ export function useWorksheetGeneration(): UseWorksheetGenerationReturn { const [status, setStatus] = useState<GenerationStatus>('idle') const [error, setError] = useState<string | null>(null) const generate = async (config: WorksheetFormState) => { setStatus('generating') setError(null) try { // Validate configuration const validation = validateWorksheetConfig(config) if (!validation.isValid || !validation.config) { throw new Error(validation.errors?.join(', ') || 'Invalid configuration') } const response = await fetch('/api/create/worksheets', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(config), }) if (!response.ok) { const errorResult = await response.json() const errorMsg = errorResult.details ? `${errorResult.error}\n\n${errorResult.details}` : errorResult.error || 'Generation failed' throw new Error(errorMsg) } // Success - response is binary PDF data, trigger download const blob = await response.blob() const filename = `addition-worksheet-${config.name || 'student'}-${Date.now()}.pdf` // Create download link and trigger download const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.style.display = 'none' a.href = url a.download = filename document.body.appendChild(a) a.click() window.URL.revokeObjectURL(url) document.body.removeChild(a) setStatus('idle') } catch (err) { console.error('Generation error:', err) setError(err instanceof Error ? err.message : 'Unknown error occurred') setStatus('error') } } const reset = () => { setStatus('idle') setError(null) } return { status, error, generate, reset, } } |