@@ -2,44 +2,44 @@ import React, { useEffect } from "react"
2
2
import { Input } from "../../components/ui/form" ;
3
3
import Button from "../../components/ui/Button" ;
4
4
import Router from "next/router" ;
5
- import { Alert } from "react-bootstrap" ;
5
+ import { Alert } from "react-bootstrap" ;
6
6
7
7
const Find = ( ) => {
8
8
const [ token , setToken ] = React . useState ( "" ) ;
9
- const [ to , setTo ] = React . useState ( "" ) ;
10
9
const [ loading , setLoading ] = React . useState ( false ) ;
11
- const [ notify , setNotify ] = React . useState ( { msg : "" , type : "" } ) ;
10
+ const [ notify , setNotify ] = React . useState ( { msg : "" , type : "" } ) ;
12
11
const [ show , setShow ] = React . useState ( true ) ;
13
12
14
- const onSubmit = ( e ) => {
15
- e . preventDefault ( ) ;
16
- fetch ( `${ process . env . NEXT_PUBLIC_BC_HOST } /${ token } ` )
17
- . then ( res => res . json ( ) )
18
- . then ( data => {
19
- if ( token . length < 1 || data . status_code === 404 ) {
20
- setNotify ( { msg :"Invalid Token or Certificate not found" , type :"error" } )
21
- } else {
22
- setLoading ( true ) ;
23
- Router . push ( `/${ to } /${ token } ` ) }
24
- }
25
- )
26
- . catch ( err => err )
13
+ const onSubmit = async ( previewFormat ) => {
14
+ try {
15
+ if ( ! token ) return ;
16
+ setLoading ( true )
17
+
18
+ const res = await fetch ( `${ process . env . NEXT_PUBLIC_BC_HOST } /${ token } ` )
19
+ const data = res . json ( )
20
+
21
+ if ( ! res . ok || ! data ) throw new Error ( data . detail )
22
+ Router . push ( `/${ previewFormat } /${ token } ` )
23
+ } catch ( err ) {
24
+ console . error ( "Error fetching data:" , err ) ;
25
+ setNotify ( { msg : "An error occurred while fetching data" , type : "error" } ) ;
26
+ } finally {
27
+ setLoading ( false )
28
+ }
27
29
}
28
30
29
31
return < div className = "container" >
30
32
< div className = "row text-center" >
31
33
< div className = "col-12" >
32
34
< h1 > Looking for a certificate?</ h1 >
33
35
</ div >
34
- < div className = "col-12" >
35
- < form className = "d-flex" onSubmit = { ( e ) => onSubmit ( e ) } >
36
- < Input type = "text" required onChange = { ( e ) => setToken ( e . target . value ) } placeholder = "Certificate token" className = "mr-1 ml-auto" />
37
- < Button disabled = { loading } className = "mr-1" type = "submit" onClick = { ( ) => setTo ( "pdf" ) } > { loading ? "Loading" : "Get certificate" } </ Button >
38
- < Button disabled = { loading } type = "submit" onClick = { ( ) => setTo ( "preview" ) } className = "mr-auto" > { loading ? "Loading" : "Get HTML" } </ Button >
39
- </ form >
36
+ < div className = "row mx-auto" >
37
+ < Input type = "text" required onChange = { ( e ) => setToken ( e . target . value ) } placeholder = "Certificate token" className = "mr-1 ml-auto" />
38
+ < Button disabled = { loading } className = "mr-1" type = "submit" onClick = { ( ) => onSubmit ( "pdf" ) } > { loading ? "Loading" : "Get certificate" } </ Button >
39
+ < Button disabled = { loading } type = "submit" onClick = { ( ) => onSubmit ( "preview" ) } className = "mr-auto" > { loading ? "Loading" : "Get HTML" } </ Button >
40
40
</ div >
41
41
</ div >
42
- { notify . type == "error" ? < Alert onClose = { setTimeout ( ( ) => setShow ( false ) , 3000 ) } show = { show } variant = { "danger" } className = "shadow-one mt-4 alert-position" > { notify . msg } </ Alert > : "" }
42
+ { notify . type == "error" ? < Alert onClose = { setTimeout ( ( ) => setShow ( false ) , 3000 ) } show = { show } variant = { "danger" } className = "shadow-one mt-4 alert-position" > { notify . msg } </ Alert > : "" }
43
43
</ div >
44
44
}
45
45
export default Find ;
0 commit comments