1
+ import { useEffect , useState } from "react" ;
2
+
1
3
import { useAppSelector , useAppDispatch } from "../../../app/hooks/useRedux" ;
2
- import { setPortfolioCurrencies } from "../../../entities/currency/model" ;
4
+ import { setActiveCurrency , setPortfolioCurrencies } from "../../../entities/currency/model" ;
5
+ import { ICurrency } from "../../../pages/Currency/types" ;
3
6
import { Button } from "../../button" ;
7
+ import { AddModal } from "../AddModal/AddModal" ;
8
+ import { Modal } from "../Modal/Modal" ;
4
9
5
10
export const PortfolioModal : React . FC = ( ) => {
6
11
const { portfolioCurrencies } = useAppSelector ( ( store ) => store . currency ) ;
7
12
const dispatch = useAppDispatch ( ) ;
13
+ const [ modalActive , setModalActive ] = useState < boolean > ( false ) ;
14
+ const [ portfolioPrice , setPortfolioPrice ] = useState < number > ( 0 ) ;
15
+ const { currenciesData } = useAppSelector ( ( store ) => store . currency ) ;
8
16
const handleRemoveCurrencyFromPortfolio = ( id : number ) => {
9
17
const confirmDeleting = window . confirm (
10
18
"Delete this currency from Portfolio?"
@@ -21,30 +29,65 @@ export const PortfolioModal: React.FC = () => {
21
29
}
22
30
} ;
23
31
32
+ const handleAddClick = ( currency : ICurrency ) => {
33
+ setModalActive ( true ) ;
34
+ document . body . classList . toggle ( "active" ) ;
35
+ dispatch ( setActiveCurrency ( currency ) ) ;
36
+ } ;
37
+ const calcPortfolioTotalPrice = ( ) => {
38
+ let priceResult = 0 ;
39
+
40
+ if ( portfolioCurrencies ?. length ) {
41
+ portfolioCurrencies ?. forEach ( ( { count } ) => {
42
+ priceResult += + count ;
43
+ } ) ;
44
+ setPortfolioPrice ( priceResult ) ;
45
+ } else {
46
+ setPortfolioPrice ( 0 ) ;
47
+ }
48
+ } ;
49
+
50
+ useEffect ( ( ) => {
51
+ calcPortfolioTotalPrice ( ) ;
52
+ } , [ portfolioCurrencies ] ) ;
53
+
24
54
return (
25
55
< div className = "portfolio-modal-window" >
26
56
< h3 > My Portfolio</ h3 >
27
57
< ul className = "portfolio-modal-window__list" >
28
58
{ portfolioCurrencies ?. length ? (
29
- portfolioCurrencies . map ( ( { id , name , count } ) => (
30
- < li key = { id } >
59
+ portfolioCurrencies . map ( ( item : ICurrency ) => (
60
+ < li key = { item . id } >
31
61
< div >
32
- < p className = "portfolio-modal-window__list__name" > { name } </ p >
33
- < p > { count } </ p >
62
+ < p className = "portfolio-modal-window__list__name" > { item . name } </ p >
63
+ < p > { item . count } </ p >
64
+ < Button
65
+ isSubmit = { false }
66
+ text = { "✏️" }
67
+ className = { "btn-edit" }
68
+ type = "button"
69
+ onClickButton = { ( ) => handleAddClick ( item ) }
70
+ />
34
71
< Button
35
72
isSubmit = { false }
36
73
text = { "-" }
37
74
className = { "btn-remove" }
38
75
type = { "button" }
39
- onClickButton = { ( ) => handleRemoveCurrencyFromPortfolio ( id ) }
76
+ onClickButton = { ( ) => handleRemoveCurrencyFromPortfolio ( item . id ) }
40
77
/>
41
78
</ div >
42
79
</ li >
43
80
) )
44
81
) : (
45
82
< p > Sorry, your portfolio is empty :(</ p >
46
83
) }
84
+ < p className = "top-currency" >
85
+ Portfolio : { parseFloat ( `${ portfolioPrice } ` ) . toFixed ( 2 ) } USD
86
+ </ p >
47
87
</ ul >
88
+ < Modal active = { modalActive } setActive = { setModalActive } >
89
+ < AddModal />
90
+ </ Modal >
48
91
</ div >
49
92
) ;
50
93
} ;
0 commit comments