1- import React from 'react' ;
2- import { Link } from 'react-router-dom' ;
1+ import React , { useEffect , useState } from 'react' ;
2+ import { Link , useHistory } from 'react-router-dom' ;
33import { MdArrowBack } from 'react-icons/md' ;
44
55import './styles.scss' ;
66
7+ export interface FactorialResults {
8+ factorial : number ,
9+ result : string
10+ }
11+
712const History : React . FC = ( ) => {
13+ const history = useHistory ( ) ;
14+
15+ const [ resultsHistory , setResultsHistory ] = useState < Array < FactorialResults > > ( [ ] ) ;
16+
17+ useEffect ( ( ) => {
18+ const storageFactorialResults = localStorage . getItem ( 'factorialResults' ) ;
19+
20+ if ( storageFactorialResults ) {
21+ setResultsHistory ( JSON . parse ( storageFactorialResults ) ) ;
22+ }
23+ } , [ ] ) ;
24+
25+ function handleClickHistoryItem ( factorial : number ) {
26+ history . push ( `/calc?factorial=${ factorial } ` ) ;
27+ }
28+
829 return (
930 < div id = "history-page" className = "container" >
1031 < Link to = "/" className = "back-button" >
@@ -16,21 +37,21 @@ const History: React.FC = () => {
1637 < h2 className = "title" > Seus últimos cálculos fatorais.</ h2 >
1738
1839 {
19- false &&
40+ resultsHistory . length > 0 &&
2041 < ul className = "history" >
2142 {
22- [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 ] . map ( ( item ) => (
23- < li key = { String ( item ) } className = "history-item" >
24- < span className = "factorial" > 14 </ span >
25- < span className = "result" > 9823823 </ span >
43+ resultsHistory . map ( ( history ) => (
44+ < li key = { String ( history . factorial ) } className = "history-item" onClick = { ( ) => handleClickHistoryItem ( history . factorial ) } >
45+ < span className = "factorial" > { history . factorial } </ span >
46+ < span className = "result" > { history . result } </ span >
2647 </ li >
2748 ) )
2849 }
2950 </ ul >
3051 }
3152
3253 {
33- true &&
54+ resultsHistory . length === 0 &&
3455 < p >
3556 Você ainda não realizou nenhum cálculo.
3657 </ p >
0 commit comments