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/pages/vouchers/notes.tsx
Views: 687
1
import { useState } from "react";
2
import Markdown from "@cocalc/frontend/editors/slate/static-markdown";
3
import apiPost from "lib/api/post";
4
import { Alert, Button, Input, Space, Tooltip } from "antd";
5
import { Icon } from "@cocalc/frontend/components/icon";
6
7
export default function Notes({ notes: notes0, code }) {
8
const [editing, setEditing] = useState<boolean>(false);
9
const [notes, setNotes] = useState<string>(notes0);
10
const [editVal, setEditVal] = useState<string>(notes);
11
const [error, setError] = useState<string>("");
12
13
if (editing) {
14
return (
15
<div style={{ width: "450px" }}>
16
<Input.TextArea
17
value={editVal}
18
autoSize
19
onChange={(e) => setEditVal(e.target.value)}
20
/>
21
<Space>
22
<Button.Group style={{ marginTop: "5px" }}>
23
<Button
24
type="primary"
25
onClick={async () => {
26
try {
27
await apiPost("/vouchers/set-voucher-code-notes", {
28
code,
29
notes: editVal,
30
});
31
setNotes(editVal);
32
setEditing(false);
33
} catch (err) {
34
setError(`${err}`);
35
}
36
}}
37
>
38
Save
39
</Button>
40
<Button
41
onClick={() => {
42
setEditing(false);
43
}}
44
>
45
Cancel
46
</Button>
47
</Button.Group>
48
<span style={{ color: "#666" }}>
49
Add notes about this voucher code that only you can see (supports
50
markdown).
51
</span>
52
</Space>
53
{error && (
54
<Alert
55
type="error"
56
message={error}
57
showIcon
58
closable
59
onClose={() => setError("")}
60
/>
61
)}
62
</div>
63
);
64
}
65
66
if (notes) {
67
return (
68
<Tooltip title={"Click to edit"}>
69
<div onClick={() => setEditing(true)} style={{ cursor: "pointer" }}>
70
<Markdown value={notes} />
71
</div>
72
</Tooltip>
73
);
74
}
75
76
return (
77
<Tooltip title={"Click to add a private note about this voucher code"}>
78
{" "}
79
<Button type="text" onClick={() => setEditing(true)}>
80
<Icon name="plus" />
81
</Button>
82
</Tooltip>
83
);
84
}
85
86