Skip to content

Commit bf51be2

Browse files
committedAug 1, 2020
Add a tab to show the top contributions
1 parent 3d7eedc commit bf51be2

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed
 

‎src/components/CrowdFund/donors.tsx

+43-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { FunctionComponent } from 'react';
1+
import React, { FunctionComponent, useState } from 'react';
22
import fromnow from 'fromnow';
33

44
import { AwardIcon } from '../Icons/common';
5-
import { CampaignWithFundings } from '../../services/airtable';
5+
import { CampaignWithFundings, Funding } from '../../services/airtable';
66
import { truncateString } from '../../utils';
77

88
interface Props {
@@ -11,6 +11,12 @@ interface Props {
1111

1212
export const DonorsList: FunctionComponent<Props> = ({ campaign }) => {
1313
const { fundings } = campaign;
14+
const [showRecent, setShowRecent] = useState(true);
15+
const sortedFundings = () =>
16+
showRecent
17+
? fundings.sort((a, b) => +new Date(b.created_at) - +new Date(a.created_at))
18+
: fundings.sort((a, b) => b.donated_amount - a.donated_amount);
19+
1420
if (fundings.length === 0) {
1521
return (
1622
<div className="bg-white shadow my-4 py-2 rounded-lg px-4">
@@ -21,27 +27,42 @@ export const DonorsList: FunctionComponent<Props> = ({ campaign }) => {
2127
</div>
2228
);
2329
}
30+
const tabCommonClass = 'bg-white inline-block py-2 px-4 hover:text-pink-800 font-semibold';
31+
const tabClass = `${tabCommonClass} text-pink-500`;
32+
const tabActiveClass = `${tabCommonClass} text-pink-700 border-b border-pink-500`;
2433
return (
25-
<ul className="w-full overflow-y-scroll" style={{ height: 500 }}>
26-
{fundings.map(item => (
27-
<li key={item.id} className="flex items-center bg-white shadow my-4 py-2 rounded-lg">
28-
<div className="px-4">
29-
<AwardIcon />
30-
</div>
31-
<div className="flex-1">
32-
<h6 className="font-medium text-gray-800" title={item.name}>
33-
{truncateString(item.name, 15)}
34-
</h6>
35-
<p className="text-xs text-gray-600">{fromnow(new Date(item.created_at), { max: 2, suffix: true })}</p>
36-
<p className="text-xs text-gray-600">{item.message}</p>
37-
</div>
38-
<div className="px-4">
39-
<span className="inline-block bg-green-600 text-xs rounded-full px-3 py-1 text-white">
40-
{item.donated_amount}
41-
</span>
42-
</div>
34+
<>
35+
<h2 className="text-xl mb-1 font-medium text-gray-800">Contributions</h2>
36+
<p className="text-sm text-gray-700">A special thanks to all who raised the funds for this campaign.</p>
37+
<ul className="flex border-b mt-2 justify-center">
38+
<li className="-mb-px mr-1 cursor-pointer whitespace-no-wrap" onClick={() => setShowRecent(true)}>
39+
<a className={showRecent ? tabActiveClass : tabClass}>Most Recent</a>
40+
</li>
41+
<li className="mr-1 cursor-pointer whitespace-no-wrap" onClick={() => setShowRecent(false)}>
42+
<a className={showRecent ? tabClass : tabActiveClass}>Top Contributions</a>
4343
</li>
44-
))}
45-
</ul>
44+
</ul>
45+
<ul className="w-full overflow-y-scroll" style={{ height: 500 }}>
46+
{sortedFundings().map(item => (
47+
<li key={item.id} className="flex items-center bg-white shadow my-4 py-2 rounded-lg">
48+
<div className="px-4 text-center">
49+
<AwardIcon />
50+
</div>
51+
<div className="flex-1">
52+
<h6 className="font-medium text-gray-800" title={item.name}>
53+
{truncateString(item.name, 15)}
54+
</h6>
55+
<p className="text-xs text-gray-600">{fromnow(new Date(item.created_at), { max: 2, suffix: true })}</p>
56+
<p className="text-xs text-gray-600">{item.message}</p>
57+
</div>
58+
<div className="px-4">
59+
<span className="inline-block bg-pink-600 text-xs rounded-full px-3 py-1 text-white">
60+
{item.donated_amount}
61+
</span>
62+
</div>
63+
</li>
64+
))}
65+
</ul>
66+
</>
4667
);
4768
};

‎src/components/CrowdFund/list.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const CampaignList: FunctionComponent<Props> = ({ inline = false }) => {
2323
if (error) {
2424
return (
2525
<div className="mx-0 my-4 md:mx-4 md:h-64">
26-
<h3 className="text-lg md:text-xl mb-1 text-red-700">{error.message || 'An error occured. Try again later'}</h3>
26+
<h3 className="text-lg md:text-xl mb-1 text-red-700">{'An error occured. Try again later'}</h3>
2727
</div>
2828
);
2929
}

‎src/pages/crowdfund/[slug].tsx

-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ const CampaignPage: NextPage = ({ initialData, slug }) => {
5050
</div>
5151

5252
<div className="mt-10 md:mt-0 md:mx-2 md:w-1/3 md:bg-white md:p-4 md:shadow md:rounded-lg">
53-
<h2 className="text-xl mb-1 font-medium text-gray-800">Recent Contributions</h2>
54-
<p className="text-sm text-gray-700">A special thanks to all who raised the funds for this campaign.</p>
5553
<DonorsList campaign={data} />
5654
</div>
5755
</div>