Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

buy-me-coffee: calc the price in USD of a coffee #528

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions template/web/app/buy-me-coffee/_components/FormBuyCoffee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ function FormBuyCoffee({ refetchMemos }: FormBuyCoffeeProps) {
const contract = useBuyMeACoffeeContract();

const { fields, setField, resetFields } = useFields<Fields>(initFields);
const [ethPrice, setEthPrice] = useState<number | null>(null);

useEffect(() => {
const fetchEthPrice = async () => {
const response = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd');
const data = await response.json();
setEthPrice(data.ethereum.usd);
};

fetchEthPrice();
}, []);

const reset = useCallback(async () => {
resetFields();
Expand Down Expand Up @@ -87,6 +98,7 @@ function FormBuyCoffee({ refetchMemos }: FormBuyCoffeeProps) {
)}
// eslint-disable-next-line react-perf/jsx-no-new-function-as-prop
onClick={() => setField('coffeeCount', count)}
title={`${(GAS_COST * count).toFixed(4)} ETH / $${ethPrice ? (GAS_COST * count * ethPrice).toFixed(2) : 'loading...'} `}
>
{count}
</button>
Expand Down Expand Up @@ -134,16 +146,16 @@ function FormBuyCoffee({ refetchMemos }: FormBuyCoffeeProps) {

<ContractAlert contract={contract} amount={GAS_COST} />

<Button
buttonContent={
<>
Send {fields.coffeeCount} coffee{fields.coffeeCount > 1 ? 's' : null} for{' '}
{String((GAS_COST * fields.coffeeCount).toFixed(4))} ETH
</>
}
type="submit"
disabled={disabled}
/>
<Button
buttonContent={
<>
Send {fields.coffeeCount} cupcake{fields.coffeeCount > 1 ? 's' : ''} for{' '}
{(GAS_COST * fields.coffeeCount).toFixed(4)} ETH (${ethPrice ? (GAS_COST * fields.coffeeCount * ethPrice).toFixed(2) : 'loading...'})
</>
}
type="submit"
disabled={disabled}
/>
</div>
</form>
</>
Expand Down