Skip to content

Commit 84a3d94

Browse files
fix: shortnen numbers to K in rewards, bounties, challenge cards
1 parent 3974367 commit 84a3d94

File tree

7 files changed

+35
-10
lines changed

7 files changed

+35
-10
lines changed

src/components/badges/RewardBadge.tsx

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

@@ -46,7 +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+
{displayAmount && shorternNumber(amount)}
5051
{token}
5152
</div>
5253
)}

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 { shorternNumber } 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+
{shorternNumber(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 { shorternNumber } 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>{shorternNumber(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+
{shorternNumber(reward.amount)} {reward.token}/{formatToken(reward.type)}
6566
</span>
6667
))}
6768
</div>

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 { shorternNumber } 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>{`${shorternNumber(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 ${shorternNumber(first)}; 2nd Place $${shorternNumber(second)}; 3rd Place $${shorternNumber(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 { shorternNumber } 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">{shorternNumber(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 shorternNumber = (amount: number) => {
17+
const abbrevObject = [
18+
{ value: 1000000000, abbreviation: "B" },
19+
{ value: 1000000, abbreviation: "M" },
20+
{ value: 1000, abbreviation: "K" },
21+
];
22+
for (const { value, abbreviation } of abbrevObject) {
23+
if (amount >= value) {
24+
const product = amount / value;
25+
const isFloat = product % 1 !== 0;
26+
return `${isFloat ? product.toFixed(1) : product}${abbreviation}`;
27+
}
28+
}
29+
return amount;
30+
};

0 commit comments

Comments
 (0)