11import { useState , useEffect } from 'react'
22import { useForm } from 'react-hook-form'
33import { fetchPostJSON } from '../utils/api-helpers'
4- import FormButton from '@/components/FormButton'
54import CustomLink from '@/components/Link'
65import { useRouter } from 'next/router'
76import ReportPreview from '@/components/ReportPreview'
@@ -35,7 +34,6 @@ export default function GrantReportForm({
3534 email_hash,
3635} : GrantReportFormProps ) {
3736 const router = useRouter ( )
38- const [ loading , setLoading ] = useState ( false )
3937 const [ submitted , setSubmitted ] = useState ( false )
4038 const [ error , setError ] = useState < string > ( )
4139 const [ showPreview , setShowPreview ] = useState ( false )
@@ -130,7 +128,7 @@ export default function GrantReportForm({
130128 console . error ( 'Error loading saved form data:' , e )
131129 // If there's an error, we just continue with empty form
132130 }
133- } , [ grantDetails . issue_number , setValue ] )
131+ } , [ grantDetails . issue_number , setValue , watchAllFields . report_number ] )
134132
135133 // Save form data to localStorage whenever it changes
136134 useEffect ( ( ) => {
@@ -172,33 +170,29 @@ export default function GrantReportForm({
172170 ] )
173171
174172 const onSubmit = async ( data : GrantReportFormData ) => {
175- setLoading ( true )
176173 setError ( undefined )
177174
178175 try {
179- const response = await fetchPostJSON ( '/api/submit- report' , {
176+ const response = await fetchPostJSON ( '/api/report-bot ' , {
180177 ...data ,
181178 issue_number : grantDetails . issue_number ,
182179 email_hash,
183180 } )
184181
185182 if ( response . error ) {
186183 setError ( response . error )
187- setLoading ( false )
188184 return
189185 }
190186
191187 // Clear all saved form data for this grant after successful submission
192188 clearSavedData ( )
193189
194190 setSubmitted ( true )
195- setLoading ( false )
196191 router . push ( '/reports/success' )
197192 } catch ( e ) {
198193 setError (
199194 'Error submitting report. Your data has been saved locally. Please try again later.'
200195 )
201- setLoading ( false )
202196 }
203197 }
204198
@@ -479,9 +473,13 @@ export default function GrantReportForm({
479473
480474 { ! showPreview ? (
481475 < div className = "flex justify-end" >
482- < FormButton type = "button" onClick = { ( ) => setShowPreview ( true ) } >
476+ < button
477+ type = "button"
478+ onClick = { ( ) => setShowPreview ( true ) }
479+ className = "rounded bg-gray-200 px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-300 dark:bg-gray-700 dark:text-gray-200 dark:hover:bg-gray-600"
480+ >
483481 Preview Report
484- </ FormButton >
482+ </ button >
485483 </ div >
486484 ) : (
487485 < >
@@ -498,13 +496,7 @@ export default function GrantReportForm({
498496 strokeLinecap = "round"
499497 strokeLinejoin = "round"
500498 strokeWidth = { 2 }
501- d = "M15 12a3 3 0 11-6 0 3 3 0 016 0z"
502- />
503- < path
504- strokeLinecap = "round"
505- strokeLinejoin = "round"
506- strokeWidth = { 2 }
507- d = "M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"
499+ d = "M15 12a3 3 0 11-6 0 3 3 0 016 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2h-1V9a1 1 0 00-1-1z"
508500 />
509501 </ svg >
510502 Report Preview
@@ -545,9 +537,12 @@ export default function GrantReportForm({
545537 </ svg >
546538 Back to Edit
547539 </ button >
548- < FormButton type = "submit" loading = { loading } >
540+ < button
541+ type = "submit"
542+ className = "rounded bg-orange-500 px-4 py-2 text-sm font-medium text-white hover:bg-orange-600 focus:outline-none focus:ring-2 focus:ring-orange-500 focus:ring-offset-2 dark:bg-orange-600 dark:hover:bg-orange-700"
543+ >
549544 Submit Report
550- </ FormButton >
545+ </ button >
551546 </ div >
552547 </ div >
553548 </ >
@@ -634,4 +629,29 @@ export default function GrantReportForm({
634629 </ div >
635630 </ form >
636631 )
632+
633+ if ( recoveredData ) {
634+ return (
635+ < div className = "mt-4 flex justify-end space-x-2" >
636+ < button
637+ type = "button"
638+ onClick = { ( ) => {
639+ clearSavedData ( )
640+ reset ( {
641+ project_name : grantDetails . project_name ,
642+ report_number : '' ,
643+ time_spent : '' ,
644+ next_quarter : '' ,
645+ money_usage : '' ,
646+ help_needed : '' ,
647+ } )
648+ setRecoveredData ( false )
649+ } }
650+ className = "rounded bg-gray-200 px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-300 dark:bg-gray-700 dark:text-gray-200 dark:hover:bg-gray-600"
651+ >
652+ Clear Data
653+ </ button >
654+ </ div >
655+ )
656+ }
637657}
0 commit comments