Skip to content

Commit 730fded

Browse files
authored
Merge pull request #1095 from dacadeorg/fix/show-prize-in-10k
fix: shorten numbers to K in rewards, bounties, challenge cards
2 parents e6020cf + 706f4d7 commit 730fded

File tree

9 files changed

+38
-14
lines changed

9 files changed

+38
-14
lines changed

src/components/badges/RewardBadge.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Coin from "@/components/ui/Coin";
2+
import { shortenNumber } from "@/utilities";
23
import classNames from "classnames";
34
import { ReactElement } from "react";
45

@@ -46,8 +47,7 @@ export default function RewardBadge({ reward = {}, type = "transparent", display
4647
{token && <Coin token={token} size="small" />}
4748
{amount && (
4849
<div className="font-medium pl-0 pr-2">
49-
{displayAmount && amount}
50-
{token}
50+
{displayAmount && shortenNumber(amount)} {token}
5151
</div>
5252
)}
5353
</span>

src/components/cards/challenge/Challenge.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import RelatedContent from "./RelatedContent";
88
import Badges from "./Badges";
99
import { useMemo } from "react";
1010
import { useTranslation } from "next-i18next";
11+
import { shortenNumber } from "@/utilities";
1112

1213
/**
1314
* `ChallengeCard` is a function component that renders a card
@@ -59,7 +60,7 @@ export default function ChallengeCard({ data, community, isCourseEnd }: Challeng
5960
<div className="md:pl-2 max-w-max">
6061
<div className="flex text-sm text-gray-700">
6162
<span className="block font-medium">
62-
{`${data?.isHackathon ? "$" : ""} ${totalReward} ${data?.isHackathon ? `Prize Pool Rewards` : `${reward?.token} Rewards`}`}
63+
{shortenNumber(totalReward)} {reward?.token} {` ${data?.isHackathon ? `Prize pool` : ""} rewards`}
6364
</span>
6465
</div>
6566
<div className="text-gray-400 text-xs font-normal">{data?.isHackathon ? "Top projects win money prizes" : "For submission and feedback"}</div>

src/components/cards/challenge/Overview.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useTranslation } from "next-i18next";
44
import DateManager from "@/utilities/DateManager";
55
import Certificate from "@/components/ui/Certificate";
66
import { Community } from "@/types/community";
7+
import { shortenNumber } from "@/utilities";
78

89
interface Props {
910
challenge: Challenge;
@@ -53,15 +54,15 @@ export default function Overview({ challenge, community }: Props) {
5354
<Coin size="medium" token={reward?.token} />
5455
<div className="text-sm md:pl-2 max-w-max">
5556
<div className="flex gap-1 text-gray-700 font-medium">
56-
<span>{reward.amount}</span>
57+
<span>{shortenNumber(reward.amount)}</span>
5758
<span>{reward?.token}</span>
5859
<span>{t("communities.overview.challenge.rewards")}</span>
5960
</div>
6061
<div className="text-gray-400 text-xs font-medium leading-3 mt-1 flex">
6162
{challenge.rewards.map((reward, index) => (
6263
<span key={`reward-${index}`}>
6364
{index > 0 && "\u003B "}
64-
{reward.amount} {reward.token}/{formatToken(reward.type)}
65+
{shortenNumber(reward.amount)} {reward.token}/{formatToken(reward.type)}
6566
</span>
6667
))}
6768
</div>

src/components/cards/community/_partials/RewardBadge.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CSSProperties, ReactElement } from "react";
22
import Coin from "@/components/ui/Coin";
33
import classNames from "classnames";
4+
import { shortenNumber } from "@/utilities";
45

56
/**
67
* Interface for Badge props
@@ -40,8 +41,7 @@ export default function RewardBadge({ reward = {}, type = "transparent", styles
4041
{reward.token && <Coin token={reward.token} size="small" />}
4142

4243
<div className="pl-0 pr-2 font-medium">
43-
{reward.amount}
44-
{reward.token}
44+
{shortenNumber(reward.amount as number)} {reward.token}
4545
</div>
4646
</span>
4747
);

src/components/cards/community/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export default function CommunityCard({ showRewards = true, community }: Communi
7474
</div>
7575
<div className="flex flex-col items-start justify-start max-w-xs -mt-4 md:flex-row lg:flex-col md:-mt-7 md:max-w-lg">
7676
{showRewards && reward && (
77-
<div className="text-sm flex">
77+
<div className="text-sm flex bg-lime-400">
7878
<RewardBadge reward={{ token: reward.token }} styles={rewardBadgeStyle} />
7979
</div>
8080
)}

src/components/sections/challenges/_partials/HackathonPrize.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { Distribution, Reward } from "@/types/course";
2+
import { shortenNumber } from "@/utilities";
23

34
export default function HackathonPrize({ reward, description }: { reward: Reward; description: string }) {
45
const { first, second, third } = reward?.distribution || ({} as Distribution);
56
return (
67
<>
78
<div className="flex gap-1 text-gray-700 font-medium">
8-
<span>{`$${reward?.amount} Prize Pool`}</span>
9+
<span>{`${shortenNumber(reward?.amount)} ${reward.token} Prize Pool`}</span>
910
<span>{description}</span>
1011
</div>
1112
<div className="text-gray-400 text-xs font-medium leading-3 mt-1 flex">
12-
<span>{`1st Place $${first}; 2nd Place $${second}; 3rd Place $${third}`}</span>
13+
<span>{`1st Place ${shortenNumber(first)}; 2nd Place ${shortenNumber(second)}; 3rd Place ${shortenNumber(third)}`}</span>
1314
</div>
1415
</>
1516
);

src/components/ui/Reward.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Coin from "../ui/Coin";
22
import { useTranslation } from "next-i18next";
33
import { Reward } from "@/types/course";
4+
import { shortenNumber } from "@/utilities";
45

56
interface OverviewRewardsProps {
67
reward: Reward;
@@ -18,7 +19,7 @@ export default function OverviewRewards({ reward, category, size }: OverviewRewa
1819
<Coin token={reward.token} className="flex-none" />
1920
<div className="text-base lg:pl-2 lg:pr-3 md:px-2 max-w-max">
2021
<div className="flex">
21-
<span className="block font-medium text-base pr-1">{reward.amount}</span>
22+
<span className="block font-medium text-base pr-1">{shortenNumber(reward.amount)}</span>
2223
<span className="block font-medium text-base">{reward.token}</span>
2324
</div>
2425
<div className="flex">

src/types/course.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ export type Reward = {
143143
distribution?: Distribution;
144144
};
145145
export type Distribution = {
146-
first: string;
147-
second: string;
148-
third: string;
146+
first: number;
147+
second: number;
148+
third: number;
149149
};
150150

151151
export type Introduction = {

src/utilities/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,23 @@
88
export function sleep(ms: number): Promise<unknown> {
99
return new Promise((resolve) => setTimeout(resolve, ms));
1010
}
11+
12+
/**
13+
* Convert a number to K ex : 7000 -> 7K, 2510 -> 2.5K
14+
* @date 12/7/2023 - 6:50:18 PM
15+
*/
16+
export const shortenNumber = (amount: number) => {
17+
const abreviations = [
18+
{ value: 1000000000, notation: "B" },
19+
{ value: 1000000, notation: "M" },
20+
{ value: 1000, notation: "K" },
21+
];
22+
23+
const abbreviation = abreviations.find(({ value }) => amount >= value);
24+
25+
if (!abbreviation) return amount;
26+
27+
const product = amount / abbreviation.value;
28+
const isFloat = product % 1 !== 0;
29+
return `${isFloat ? product.toFixed(1) : product}${abbreviation.notation}`;
30+
};

0 commit comments

Comments
 (0)