CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/store/add-cash-voucher.tsx
Views: 687
1
import { useState } from "react";
2
import { Button, Card, InputNumber, Space } from "antd";
3
import { Icon } from "@cocalc/frontend/components/icon";
4
import A from "components/misc/A";
5
import SiteName from "components/share/site-name";
6
import { currency } from "@cocalc/util/misc";
7
import apiPost from "lib/api/post";
8
import { MAX_COST } from "@cocalc/util/db-schema/purchases";
9
10
interface Props {
11
onAdd: () => void;
12
defaultExpand?: boolean;
13
style?;
14
}
15
16
export default function AddCashVoucher({ onAdd, defaultExpand, style }: Props) {
17
const [expand, setExpand] = useState<boolean>(!!defaultExpand);
18
const [amount, setAmount] = useState<number>(5);
19
20
if (!expand) {
21
return (
22
<Button style={style} onClick={() => setExpand(!expand)}>
23
Add Cash Voucher...
24
</Button>
25
);
26
}
27
28
const disabled = !amount;
29
const addVoucher = async () => {
30
if (disabled) return;
31
await apiPost("/shopping/cart/add", {
32
product: "cash-voucher",
33
description: { type: "cash-voucher", amount },
34
});
35
onAdd();
36
};
37
38
return (
39
<Card
40
title={
41
<>
42
Add Cash Voucher{" "}
43
{!defaultExpand && (
44
<Button
45
style={{ marginLeft: "30px" }}
46
onClick={() => setExpand(false)}
47
>
48
Close
49
</Button>
50
)}
51
</>
52
}
53
style={{ margin: "15px 0", ...style }}
54
>
55
<Space>
56
<InputNumber
57
min={0}
58
max={MAX_COST}
59
defaultValue={amount}
60
onChange={(value) => setAmount(value ?? 0)}
61
precision={2} // for two decimal places
62
step={5}
63
addonAfter="$"
64
addonBefore="Amount (USD)"
65
/>
66
<Button
67
onClick={() => {
68
addVoucher();
69
if (!defaultExpand) {
70
setExpand(false);
71
}
72
}}
73
disabled={disabled}
74
type="primary"
75
>
76
<Icon name="shopping-cart" /> Add to Cart
77
</Button>
78
</Space>
79
<p style={{ marginTop: "15px", color: "#666" }}>
80
When the recipient <A href="/redeem">redeems</A> a cash voucher code,
81
their <SiteName /> account will be credited for {currency(amount)}. They
82
can then buy anything from the store or make pay-as-you-go purchases
83
using this credit. They can also make a new voucher to transfer money
84
back. In the <A href="/vouchers">Voucher Center</A>, you can track
85
whether or not any voucher code has been redeemed, and redeem unused
86
vouchers yourself if you do not want to use them.
87
</p>
88
</Card>
89
);
90
}
91
92