Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/components/store/add-cash-voucher.tsx
Views: 687
import { useState } from "react";1import { Button, Card, InputNumber, Space } from "antd";2import { Icon } from "@cocalc/frontend/components/icon";3import A from "components/misc/A";4import SiteName from "components/share/site-name";5import { currency } from "@cocalc/util/misc";6import apiPost from "lib/api/post";7import { MAX_COST } from "@cocalc/util/db-schema/purchases";89interface Props {10onAdd: () => void;11defaultExpand?: boolean;12style?;13}1415export default function AddCashVoucher({ onAdd, defaultExpand, style }: Props) {16const [expand, setExpand] = useState<boolean>(!!defaultExpand);17const [amount, setAmount] = useState<number>(5);1819if (!expand) {20return (21<Button style={style} onClick={() => setExpand(!expand)}>22Add Cash Voucher...23</Button>24);25}2627const disabled = !amount;28const addVoucher = async () => {29if (disabled) return;30await apiPost("/shopping/cart/add", {31product: "cash-voucher",32description: { type: "cash-voucher", amount },33});34onAdd();35};3637return (38<Card39title={40<>41Add Cash Voucher{" "}42{!defaultExpand && (43<Button44style={{ marginLeft: "30px" }}45onClick={() => setExpand(false)}46>47Close48</Button>49)}50</>51}52style={{ margin: "15px 0", ...style }}53>54<Space>55<InputNumber56min={0}57max={MAX_COST}58defaultValue={amount}59onChange={(value) => setAmount(value ?? 0)}60precision={2} // for two decimal places61step={5}62addonAfter="$"63addonBefore="Amount (USD)"64/>65<Button66onClick={() => {67addVoucher();68if (!defaultExpand) {69setExpand(false);70}71}}72disabled={disabled}73type="primary"74>75<Icon name="shopping-cart" /> Add to Cart76</Button>77</Space>78<p style={{ marginTop: "15px", color: "#666" }}>79When the recipient <A href="/redeem">redeems</A> a cash voucher code,80their <SiteName /> account will be credited for {currency(amount)}. They81can then buy anything from the store or make pay-as-you-go purchases82using this credit. They can also make a new voucher to transfer money83back. In the <A href="/vouchers">Voucher Center</A>, you can track84whether or not any voucher code has been redeemed, and redeem unused85vouchers yourself if you do not want to use them.86</p>87</Card>88);89}909192