1- import React , { useMemo } from 'react' ;
1+ import React , { useEffect , useMemo , useState } from 'react' ;
22import { MdRefresh } from 'react-icons/md' ;
33import { Link , useLocation } from 'react-router-dom' ;
4+ import api from '../../config/api' ;
45
56import './styles.scss' ;
67
@@ -12,6 +13,33 @@ const Result: React.FC = () => {
1213 const query = useQuery ( ) ;
1314 const factorialNumber = Number ( query . get ( "factorial" ) ) ;
1415
16+ const [ loading , setLoading ] = useState < boolean > ( true ) ;
17+ const [ errors , setErrors ] = useState < string > ( "" ) ;
18+ const [ factorialResult , setFactorialResult ] = useState < string > ( "" ) ;
19+
20+ useEffect ( ( ) => {
21+ setLoading ( true ) ;
22+ setErrors ( "" ) ;
23+ setFactorialResult ( "" ) ;
24+
25+ async function getFactorial ( ) {
26+ if ( factorialNumber ) {
27+ const response = await fetch ( `${ api . baseURL } /factorial?number=${ factorialNumber } ` )
28+ if ( response . status === 200 ) {
29+ const { result } = await response . json ( ) ;
30+ setFactorialResult ( result ) ;
31+ setLoading ( false ) ;
32+ } else {
33+ const { errors } = await response . json ( ) ;
34+ setErrors ( errors [ 0 ] . msg ) ;
35+ setLoading ( false ) ;
36+ }
37+ }
38+ }
39+
40+ getFactorial ( ) ;
41+ } , [ factorialNumber ] )
42+
1543 const multipliers = useMemo ( ( ) => {
1644 if ( factorialNumber <= 20 ) {
1745 let string = "" ;
@@ -33,24 +61,42 @@ const Result: React.FC = () => {
3361 }
3462
3563 return (
36- < div className = "container" >
64+ < div id = "result-page" className = "container" >
3765 < h2 className = "title" >
3866 Resultado do cálculo de { factorialNumber } fatorial é de:
3967 </ h2 >
4068
41- < div className = "factorial-result" >
42- < p className = "multipliers" > { multipliers } </ p >
43- < h4 className = "result" >
44- 51391281291
45- </ h4 >
46- </ div >
47-
48- < Link to = "/" className = "back-button" >
49- < div className = "back" >
50- < MdRefresh size = { 22 } />
51- < span > Novo cálculo</ span >
52- </ div >
53- </ Link >
69+ {
70+ loading ?
71+ < >
72+ < p > Aguarde, computando...</ p >
73+ </ >
74+ :
75+ < >
76+ {
77+ errors ?
78+ < >
79+ < p > < strong > ERRO:</ strong > { errors } </ p >
80+ </ >
81+ :
82+ < >
83+ < div className = "factorial-result" >
84+ < p className = "multipliers" > { multipliers } </ p >
85+ < h4 className = "result" >
86+ { factorialResult }
87+ </ h4 >
88+ </ div >
89+
90+ </ >
91+ }
92+ < Link to = "/" className = "back-button" >
93+ < div className = "back" >
94+ < MdRefresh size = { 22 } />
95+ < span > Novo cálculo</ span >
96+ </ div >
97+ </ Link >
98+ </ >
99+ }
54100 </ div >
55101 ) ;
56102}
0 commit comments