Skip to content

Commit

Permalink
Add a tab to show the top contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
pbteja1998 committed Aug 1, 2020
1 parent 3d7eedc commit bf51be2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
65 changes: 43 additions & 22 deletions src/components/CrowdFund/donors.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { FunctionComponent } from 'react';
import React, { FunctionComponent, useState } from 'react';
import fromnow from 'fromnow';

import { AwardIcon } from '../Icons/common';
import { CampaignWithFundings } from '../../services/airtable';
import { CampaignWithFundings, Funding } from '../../services/airtable';
import { truncateString } from '../../utils';

interface Props {
Expand All @@ -11,6 +11,12 @@ interface Props {

export const DonorsList: FunctionComponent<Props> = ({ campaign }) => {
const { fundings } = campaign;
const [showRecent, setShowRecent] = useState(true);
const sortedFundings = () =>
showRecent
? fundings.sort((a, b) => +new Date(b.created_at) - +new Date(a.created_at))
: fundings.sort((a, b) => b.donated_amount - a.donated_amount);

if (fundings.length === 0) {
return (
<div className="bg-white shadow my-4 py-2 rounded-lg px-4">
Expand All @@ -21,27 +27,42 @@ export const DonorsList: FunctionComponent<Props> = ({ campaign }) => {
</div>
);
}
const tabCommonClass = 'bg-white inline-block py-2 px-4 hover:text-pink-800 font-semibold';
const tabClass = `${tabCommonClass} text-pink-500`;
const tabActiveClass = `${tabCommonClass} text-pink-700 border-b border-pink-500`;
return (
<ul className="w-full overflow-y-scroll" style={{ height: 500 }}>
{fundings.map(item => (
<li key={item.id} className="flex items-center bg-white shadow my-4 py-2 rounded-lg">
<div className="px-4">
<AwardIcon />
</div>
<div className="flex-1">
<h6 className="font-medium text-gray-800" title={item.name}>
{truncateString(item.name, 15)}
</h6>
<p className="text-xs text-gray-600">{fromnow(new Date(item.created_at), { max: 2, suffix: true })}</p>
<p className="text-xs text-gray-600">{item.message}</p>
</div>
<div className="px-4">
<span className="inline-block bg-green-600 text-xs rounded-full px-3 py-1 text-white">
{item.donated_amount}
</span>
</div>
<>
<h2 className="text-xl mb-1 font-medium text-gray-800">Contributions</h2>
<p className="text-sm text-gray-700">A special thanks to all who raised the funds for this campaign.</p>
<ul className="flex border-b mt-2 justify-center">
<li className="-mb-px mr-1 cursor-pointer whitespace-no-wrap" onClick={() => setShowRecent(true)}>
<a className={showRecent ? tabActiveClass : tabClass}>Most Recent</a>
</li>
<li className="mr-1 cursor-pointer whitespace-no-wrap" onClick={() => setShowRecent(false)}>
<a className={showRecent ? tabClass : tabActiveClass}>Top Contributions</a>
</li>
))}
</ul>
</ul>
<ul className="w-full overflow-y-scroll" style={{ height: 500 }}>
{sortedFundings().map(item => (
<li key={item.id} className="flex items-center bg-white shadow my-4 py-2 rounded-lg">
<div className="px-4 text-center">
<AwardIcon />
</div>
<div className="flex-1">
<h6 className="font-medium text-gray-800" title={item.name}>
{truncateString(item.name, 15)}
</h6>
<p className="text-xs text-gray-600">{fromnow(new Date(item.created_at), { max: 2, suffix: true })}</p>
<p className="text-xs text-gray-600">{item.message}</p>
</div>
<div className="px-4">
<span className="inline-block bg-pink-600 text-xs rounded-full px-3 py-1 text-white">
{item.donated_amount}
</span>
</div>
</li>
))}
</ul>
</>
);
};
2 changes: 1 addition & 1 deletion src/components/CrowdFund/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const CampaignList: FunctionComponent<Props> = ({ inline = false }) => {
if (error) {
return (
<div className="mx-0 my-4 md:mx-4 md:h-64">
<h3 className="text-lg md:text-xl mb-1 text-red-700">{error.message || 'An error occured. Try again later'}</h3>
<h3 className="text-lg md:text-xl mb-1 text-red-700">{'An error occured. Try again later'}</h3>
</div>
);
}
Expand Down
2 changes: 0 additions & 2 deletions src/pages/crowdfund/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ const CampaignPage: NextPage = ({ initialData, slug }) => {
</div>

<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">
<h2 className="text-xl mb-1 font-medium text-gray-800">Recent Contributions</h2>
<p className="text-sm text-gray-700">A special thanks to all who raised the funds for this campaign.</p>
<DonorsList campaign={data} />
</div>
</div>
Expand Down

1 comment on commit bf51be2

@vercel
Copy link

@vercel vercel bot commented on bf51be2 Aug 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.