diff --git a/web/components/charity/charity-card.tsx b/web/components/charity/charity-card.tsx new file mode 100644 index 00000000..e8f7a5cb --- /dev/null +++ b/web/components/charity/charity-card.tsx @@ -0,0 +1,39 @@ +import Link from 'next/link' +import { Row } from '../layout/row' + +// TODO: type probably belongs elsewhere +export interface Charity { + name: string + slug: string + website: string + ein: string + photo?: string + blurb: string + raised: number +} + +interface Props { + charity: Charity +} + +export default function Card({ charity }: Props) { + const { name, slug, photo, raised, blurb } = charity + + return ( + +
+
+ {photo && } +
+
+

{name}

+
{blurb}
+ + ${Math.floor(raised / 100)} + raised + +
+
+ + ) +} diff --git a/web/package.json b/web/package.json index ff28c54f..60ee1c77 100644 --- a/web/package.json +++ b/web/package.json @@ -34,6 +34,7 @@ }, "devDependencies": { "@tailwindcss/forms": "0.4.0", + "@tailwindcss/line-clamp": "^0.3.1", "@tailwindcss/typography": "^0.5.1", "@types/lodash": "4.14.178", "@types/node": "16.11.11", diff --git a/web/pages/charity/[...slugs]/index.tsx b/web/pages/charity/[...slugs]/index.tsx new file mode 100644 index 00000000..ba54e7f8 --- /dev/null +++ b/web/pages/charity/[...slugs]/index.tsx @@ -0,0 +1,168 @@ +import clsx from 'clsx' +import { useEffect, useMemo, useRef, useState } from 'react' +import { Charity } from '../../../components/charity/charity-card' +import { Col } from '../../../components/layout/col' +import { Row } from '../../../components/layout/row' +import { Page } from '../../../components/page' +import { Title } from '../../../components/title' +import { BuyAmountInput } from '../../../components/amount-input' +import { Spacer } from '../../../components/layout/spacer' +import { User } from '../../../../common/user' +import { useUser } from '../../../hooks/use-user' +import { manaTo$ } from '../misc' +import { Linkify } from '../../../components/linkify' + +// TODO: replace with props +const data: Charity = { + name: 'QRI', + slug: 'qri', + website: 'https://www.google.com', + ein: '123456789', + photo: 'https://placekitten.com/200/200', + blurb: + 'Lorem Ipsum is simply dummy text of Lorem Ipsum is simply dummy text ofLorem Ipsum is simply dummy text ofLorem Ipsum is simply dLorem Ipsum is simply dummy text ofLorem Ipsum is simply dummy text ofLorem Ipsum is simply dummy text ofLorem Ipsum is simply dummy text ofLorem Ipsum is simply dummy text ofLorem Ipsum is simply dummy text ofLorem Ipsum is simply dummy text ofLorem Ipsum isLorem Ipsum is simply dummy text ofLorem Ipsum is simply dummy text of simply dummy text ofummy text of ', + raised: 23450, +} + +export default function CharityPage() { + const { name, photo, blurb } = data + + // TODO: why not just useUser inside Donation Box rather than passing in? + const user = useUser() + + return ( + }> + + + + {/* TODO: donations over time chart */} + <Row className="justify-between"> + {photo && <img src={photo} alt="" className="w-40 rounded-2xl" />} + <Details userDonated={4} numSupporters={1} /> + </Row> + <h2 className="mt-7 mb-2 text-xl text-indigo-700">About</h2> + <Blurb text={blurb} /> + </Col> + </Col> + </Page> + ) +} + +function Blurb({ text }: { text: string }) { + const [open, setOpen] = useState(false) + + // Calculate whether the full blurb is already shown + const ref = useRef<HTMLDivElement>(null) + const [hideExpander, setHideExpander] = useState(false) + useEffect(() => { + if (ref.current) { + setHideExpander(ref.current.scrollHeight <= ref.current.clientHeight) + } + }, []) + + return ( + <> + <div + className={clsx(' text-gray-500', !open && 'line-clamp-5')} + ref={ref} + > + {text} + </div> + <button + onClick={() => setOpen(!open)} + className={clsx( + 'btn btn-link capitalize-none my-3 normal-case text-indigo-700', + hideExpander && 'hidden' + )} + > + {open ? 'Hide' : 'Read more'} + </button> + </> + ) +} + +function Details(props: { userDonated?: number; numSupporters: number }) { + const { userDonated, numSupporters } = props + const { raised, website } = data + return ( + <Col className="gap-1 text-right"> + <div className="text-primary mb-2 text-4xl">{manaTo$(raised)} raised</div> + {userDonated && ( + <div className="text-primary text-xl"> + {manaTo$(userDonated)} from you! + </div> + )} + {numSupporters > 0 && ( + <div className="text-gray-500">{numSupporters} supporters</div> + )} + <Linkify text={website} /> + </Col> + ) +} + +function DonationBox(props: { user?: User | null }) { + const { user } = props + const [amount, setAmount] = useState<number | undefined>() + const [isSubmitting, setIsSubmitting] = useState(false) + const [error, setError] = useState<string | undefined>() + + const donateDisabled = isSubmitting || !amount || error + + const onSubmit: React.FormEventHandler = async (e) => { + e.preventDefault() + setIsSubmitting(true) + setError(undefined) + // TODO await sending to db + await new Promise((resolve) => setTimeout(resolve, 1000)) + setIsSubmitting(false) + setAmount(undefined) + } + + return ( + <div className="rounded-lg bg-white py-6 px-8 shadow-lg"> + <div className="mb-6 text-2xl text-gray-700">Donate</div> + <form onSubmit={onSubmit}> + <label + className="mb-2 block text-sm text-gray-500" + htmlFor="donate-input" + > + Amount + </label> + <BuyAmountInput + inputClassName="w-full donate-input" + amount={amount} + onChange={setAmount} + error={error} + setError={setError} + /> + + <Col className="mt-3 w-full gap-3"> + <Row className="items-center justify-between text-sm"> + <span className="text-gray-500">Conversion</span> + <span> + {amount || 0} Mana + <span className="mx-2">→</span> + {manaTo$(amount || 0)} + </span> + </Row> + {/* TODO: matching pool */} + </Col> + + <Spacer h={8} /> + + {user && ( + <button + type="submit" + className={clsx( + 'btn w-full', + donateDisabled ? 'btn-disabled' : 'btn-primary', + isSubmitting && 'loading' + )} + > + Donate + </button> + )} + </form> + </div> + ) +} diff --git a/web/pages/charity/index.tsx b/web/pages/charity/index.tsx new file mode 100644 index 00000000..043376ad --- /dev/null +++ b/web/pages/charity/index.tsx @@ -0,0 +1,70 @@ +import _ from 'lodash' +import { useState, useMemo } from 'react' +import Card from '../../components/charity/charity-card' +import { Col } from '../../components/layout/col' +import { Page } from '../../components/page' +import { Title } from '../../components/title' + +const charities = [ + 'QRI', + 'Redwood Research', + '._.', + 'Center for Effective Altruism', + 'AllFed', + 'Against Malaria Foundation', + 'Institution for Long Long Loooong Loquacious Language (ILL)', + 'American Red Cross', +].map((name, i) => ({ + name, + slug: name, + website: 'https://www.google.com', + ein: '123456789', + photo: i === 4 ? '' : 'https://placekitten.com/200/200', + blurb: + i === 2 + ? 'short text' + : "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", + raised: 4001, +})) +// TODO: actual data + +export default function Charity() { + const [query, setQuery] = useState('') + const debouncedQuery = _.debounce(setQuery, 50) + + const filterCharities = useMemo( + () => charities.filter((charity) => charity.name.includes(query)), + [query] + ) + + return ( + <Page> + <Col className="w-full items-center px-4 sm:px-0"> + <Col className="max-w-xl"> + <Title text="Donate to a charity" /> + <div className="mb-6 text-gray-500"> + Exchange your $M for real dollars in the form of charity donations! + </div> + <input + type="text" + onChange={(e) => debouncedQuery(e.target.value)} + placeholder="Search charities" + className="input input-bordered mb-4 w-full" + /> + </Col> + <div className="grid max-w-xl grid-flow-row grid-cols-1 gap-3 lg:max-w-full lg:grid-cols-2 xl:grid-cols-3"> + {filterCharities.map((charity) => ( + <div key={charity.name}> + <Card charity={charity} /> + </div> + ))} + </div> + {filterCharities.length === 0 && ( + <div className="text-center text-gray-500"> + No charities match your search :( + </div> + )} + </Col> + </Page> + ) +} diff --git a/web/pages/charity/misc.ts b/web/pages/charity/misc.ts new file mode 100644 index 00000000..726ab00b --- /dev/null +++ b/web/pages/charity/misc.ts @@ -0,0 +1,2 @@ +export const manaTo$ = (mana: number) => + (mana / 100).toLocaleString('en-US', { style: 'currency', currency: 'USD' }) diff --git a/web/tailwind.config.js b/web/tailwind.config.js index 8c72e89b..199f39d4 100644 --- a/web/tailwind.config.js +++ b/web/tailwind.config.js @@ -24,6 +24,7 @@ module.exports = { plugins: [ require('@tailwindcss/forms'), require('@tailwindcss/typography'), + require('@tailwindcss/line-clamp'), require('daisyui'), ], daisyui: { diff --git a/yarn.lock b/yarn.lock index 2ea669e5..188532da 100644 --- a/yarn.lock +++ b/yarn.lock @@ -906,6 +906,11 @@ dependencies: mini-svg-data-uri "^1.2.3" +"@tailwindcss/line-clamp@^0.3.1": + version "0.3.1" + resolved "https://registry.yarnpkg.com/@tailwindcss/line-clamp/-/line-clamp-0.3.1.tgz#4d8441b509b87ece84e94f28a4aa9998413ab849" + integrity sha512-pNr0T8LAc3TUx/gxCfQZRe9NB2dPEo/cedPHzUGIPxqDMhgjwNm6jYxww4W5l0zAsAddxr+XfZcqttGiFDgrGg== + "@tailwindcss/typography@^0.5.1": version "0.5.1" resolved "https://registry.yarnpkg.com/@tailwindcss/typography/-/typography-0.5.1.tgz#486248a9426501f11a9b0295f7cfc0eb29659c46"