Skip to content

Commit dab9d32

Browse files
committed
improve: add edit button and total price for portfolio modal
1 parent 778b0bf commit dab9d32

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"typescript.updateImportsOnFileMove.enabled": "always",
44
"editor.formatOnSave": false,
55
"editor.codeActionsOnSave": {
6-
"source.fixAll.eslint": true,
7-
"source.fixAll.stylelint": true
6+
"source.fixAll.eslint": "explicit",
7+
"source.fixAll.stylelint": "explicit"
88
},
99
"eslint.validate": [
1010
"javascript",

src/components/button/button.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
$white: #f9f9f9;
22
$lite-green: rgb(24, 198, 131);
3+
$blue: rgb(100, 181, 246);
34

45
.btn {
56
border: none;
@@ -41,6 +42,13 @@ $lite-green: rgb(24, 198, 131);
4142
border-radius: 50%;
4243
padding:0;
4344
}
45+
&-edit{
46+
background-color: $blue;
47+
width: 30px;
48+
height: 30px;
49+
border-radius: 50%;
50+
padding:0;
51+
}
4452
&-portfolio {
4553
width: 60px;
4654
height: 60px;

src/components/modals/PortfolioModal/PortfolioModal.tsx

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
import { useEffect, useState } from "react";
2+
13
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";
36
import { Button } from "../../button";
7+
import { AddModal } from "../AddModal/AddModal";
8+
import { Modal } from "../Modal/Modal";
49

510
export const PortfolioModal: React.FC = () => {
611
const { portfolioCurrencies } = useAppSelector((store) => store.currency);
712
const dispatch = useAppDispatch();
13+
const [modalActive, setModalActive] = useState<boolean>(false);
14+
const [portfolioPrice, setPortfolioPrice] = useState<number>(0);
15+
const { currenciesData } = useAppSelector((store) => store.currency);
816
const handleRemoveCurrencyFromPortfolio = (id: number) => {
917
const confirmDeleting = window.confirm(
1018
"Delete this currency from Portfolio?"
@@ -21,30 +29,65 @@ export const PortfolioModal: React.FC = () => {
2129
}
2230
};
2331

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+
2454
return (
2555
<div className="portfolio-modal-window">
2656
<h3>My Portfolio</h3>
2757
<ul className="portfolio-modal-window__list">
2858
{portfolioCurrencies?.length ? (
29-
portfolioCurrencies.map(({ id, name, count }) => (
30-
<li key={ id }>
59+
portfolioCurrencies.map((item: ICurrency) => (
60+
<li key={ item.id }>
3161
<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+
/>
3471
<Button
3572
isSubmit={ false }
3673
text={ "-" }
3774
className={ "btn-remove" }
3875
type={ "button" }
39-
onClickButton={ () => handleRemoveCurrencyFromPortfolio(id) }
76+
onClickButton={ () => handleRemoveCurrencyFromPortfolio(item.id) }
4077
/>
4178
</div>
4279
</li>
4380
))
4481
) : (
4582
<p>Sorry, your portfolio is empty :(</p>
4683
)}
84+
<p className="top-currency">
85+
Portfolio : {parseFloat(`${portfolioPrice}`).toFixed(2)} USD
86+
</p>
4787
</ul>
88+
<Modal active={ modalActive } setActive={ setModalActive }>
89+
<AddModal />
90+
</Modal>
4891
</div>
4992
);
5093
};

0 commit comments

Comments
 (0)