Skip to content

Commit 543a598

Browse files
committed
refactor: separates TransactionSummary component
- moves code to show transaction summary into it's own component - transaction summary e.g. "Donating $5 for 1 tree every month"
1 parent e838374 commit 543a598

File tree

3 files changed

+97
-65
lines changed

3 files changed

+97
-65
lines changed

src/Donations/LeftPanel/LeftPanel.module.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
}
9999
}
100100

101+
.transaction-summary {
102+
margin-top: 10px;
103+
color: $light;
104+
}
105+
101106
@include smTabletView {
102107
.left-panel-container {
103108
padding: 30px;

src/Donations/LeftPanel/LeftPanelInfo.tsx

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import {
1111
OnBehalfDonor,
1212
SentGift,
1313
} from "src/Common/Types";
14-
import getFormatedCurrency from "src/Utils/getFormattedCurrency";
15-
import { getFormattedNumber } from "src/Utils/getFormattedNumber";
1614
import Avatar from "./Avatar";
15+
import TransactionSummary from "./TransactionSummary";
1716

1817
interface Props {
1918
isMobile: boolean;
@@ -58,7 +57,9 @@ const LeftPanelInfo = ({
5857
const { t, i18n } = useTranslation("common");
5958
const router = useRouter();
6059

61-
const canShowAvatar = Boolean(donationStep !== null && donationStep > 0);
60+
const canShowAvatar = donationStep !== null && donationStep > 0;
61+
const canShowTransactionSummary =
62+
paymentSetup !== null && (donationStep === 2 || donationStep === 3);
6263

6364
const handlePopoverOpen = (event: MouseEvent<HTMLElement>) => {
6465
setAnchorElement(event.currentTarget);
@@ -71,68 +72,14 @@ const LeftPanelInfo = ({
7172
return projectDetails ? (
7273
<div className="donations-info text-white">
7374
{canShowAvatar && <Avatar projectDetails={projectDetails} />}
74-
{paymentSetup &&
75-
(donationStep === 2 || donationStep === 3) &&
76-
(projectDetails.purpose === "trees" ||
77-
projectDetails.purpose === "conservation") && (
78-
<div className="contact-details-info">
79-
<div className={"w-100 mt-10 text-white"}>
80-
{t("donating")}{" "}
81-
<span className="text-bold" style={{ marginRight: "4px" }}>
82-
{getFormatedCurrency(
83-
i18n.language,
84-
currency,
85-
paymentSetup.unitCost * quantity
86-
)}
87-
</span>
88-
{paymentSetup.purpose === "trees"
89-
? t("fortreeCountTrees", {
90-
count: Number(quantity),
91-
treeCount: getFormattedNumber(
92-
i18n.language,
93-
Number(quantity)
94-
),
95-
})
96-
: paymentSetup.purpose === "conservation"
97-
? t("forQuantitym2", {
98-
quantity: getFormattedNumber(
99-
i18n.language,
100-
Number(quantity)
101-
),
102-
})
103-
: []}{" "}
104-
{frequency === "monthly"
105-
? t("everyMonth")
106-
: frequency === "yearly"
107-
? t("everyYear")
108-
: ""}
109-
</div>
110-
</div>
111-
)}
112-
113-
{paymentSetup &&
114-
(donationStep === 2 || donationStep === 3) &&
115-
(projectDetails.purpose === "bouquet" ||
116-
projectDetails.purpose === "funds") && (
117-
<div className="contact-details-info">
118-
<div className={"w-100 mt-10 text-white"}>
119-
{t("donating")}{" "}
120-
<span className="text-bold" style={{ marginRight: "4px" }}>
121-
{getFormatedCurrency(
122-
i18n.language,
123-
currency,
124-
paymentSetup.unitCost * quantity
125-
)}
126-
</span>
127-
{frequency === "monthly"
128-
? t("everyMonth")
129-
: frequency === "yearly"
130-
? t("everyYear")
131-
: ""}
132-
</div>
133-
</div>
134-
)}
135-
75+
{canShowTransactionSummary && (
76+
<TransactionSummary
77+
currency={currency}
78+
quantity={quantity}
79+
frequency={frequency}
80+
paymentSetup={paymentSetup}
81+
/>
82+
)}
13683
{donationStep && donationStep > 0 ? (
13784
<>
13885
{projectDetails.purpose === "trees" ||
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { ReactElement } from "react";
2+
import { useTranslation } from "next-i18next";
3+
import getFormatedCurrency from "src/Utils/getFormattedCurrency";
4+
import { getFormattedNumber } from "src/Utils/getFormattedNumber";
5+
import { PaymentOptions, ProjectPurpose } from "src/Common/Types";
6+
import styles from "./LeftPanel.module.scss";
7+
8+
interface Props {
9+
currency: string;
10+
quantity: number;
11+
frequency: string;
12+
paymentSetup: PaymentOptions;
13+
}
14+
15+
const TransactionSummary = ({
16+
currency,
17+
quantity,
18+
frequency,
19+
paymentSetup,
20+
}: Props): ReactElement => {
21+
const { t, i18n } = useTranslation("common");
22+
23+
/** Generates unit/frequency info when needed */
24+
const getAdditionalInfo = (
25+
purpose: ProjectPurpose,
26+
frequency: string
27+
): string => {
28+
let info = "";
29+
switch (purpose) {
30+
case "trees":
31+
info =
32+
info +
33+
" " +
34+
t("fortreeCountTrees", {
35+
count: Number(quantity),
36+
treeCount: getFormattedNumber(i18n.language, Number(quantity)),
37+
});
38+
break;
39+
case "conservation":
40+
info =
41+
info +
42+
" " +
43+
t("forQuantitym2", {
44+
quantity: getFormattedNumber(i18n.language, Number(quantity)),
45+
});
46+
break;
47+
default:
48+
break;
49+
}
50+
51+
switch (frequency) {
52+
case "monthly":
53+
info = info + " " + t("everyMonth");
54+
break;
55+
case "yearly":
56+
info = info + " " + t("everyYear");
57+
break;
58+
default:
59+
break;
60+
}
61+
62+
return info;
63+
};
64+
65+
return (
66+
<div className={styles["transaction-summary"]}>
67+
{t("donating")}{" "}
68+
<strong>
69+
{getFormatedCurrency(
70+
i18n.language,
71+
currency,
72+
paymentSetup.unitCost * quantity
73+
)}
74+
</strong>
75+
{getAdditionalInfo(paymentSetup.purpose, frequency)}
76+
</div>
77+
);
78+
};
79+
80+
export default TransactionSummary;

0 commit comments

Comments
 (0)