Path: blob/master/src/packages/next/pages/vouchers/created.tsx
1450 views
/*1* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { useMemo } from "react";6import Footer from "components/landing/footer";7import Header from "components/landing/header";8import Head from "components/landing/head";9import { Alert, Card, Layout, Space, Table } from "antd";10import withCustomize from "lib/with-customize";11import { Customize } from "lib/customize";12import { Icon } from "@cocalc/frontend/components/icon";13import A from "components/misc/A";14import InPlaceSignInOrUp from "components/auth/in-place-sign-in-or-up";15import useProfile from "lib/hooks/profile";16import { useRouter } from "next/router";17import Loading from "components/share/loading";18import useDatabase from "lib/hooks/database";19import TimeAgo from "timeago-react";20import { field_cmp } from "@cocalc/util/misc";21import { money } from "@cocalc/util/licenses/purchase/utils";22import Help from "components/vouchers/help";2324const QUERY = {25vouchers: [26{27id: null,28created: null,29active: null,30expire: null,31title: null,32count: null,33cost: null,34tax: null,35when_pay: null,36purchased: null,37},38],39} as const;4041export const COLUMNS = [42{43title: "ID",44dataIndex: "id",45key: "id",46},47{48title: "Created",49dataIndex: "created",50key: "created",51render: (_, { id, created }) => (52<A href={`/vouchers/${id}`}>{<TimeAgo datetime={created} />}</A>53),54},55{56title: (57<>58Codes59<br />60(click to view)61</>62),63dataIndex: "count",64key: "count",65align: "center",66render: (_, { id, count }) => <A href={`/vouchers/${id}`}>{count}</A>,67},6869{70title: "Cost",71dataIndex: "cost",72key: "cost",73align: "center",74render: (_, { id, cost, tax }) => (75<A href={`/vouchers/${id}`}>76{money(cost, true)}77{tax ? ` (+ ${money(tax, true)} tax)` : ""} each78</A>79),80},81{82title: "Status",83render: (_, { id, when_pay, purchased }) => (84<A href={`/vouchers/${id}`}>85<Status when_pay={when_pay} purchased={purchased} />86</A>87),88},89{90title: "Description",91dataIndex: "title",92key: "title",93render: (_, { title, id }) => {94return <A href={`/vouchers/${id}`}>{title}</A>;95},96},97{98title: "Active",99dataIndex: "active",100key: "active",101align: "center",102render: (_, { id, active }) => (103<A href={`/vouchers/${id}`}>104<TimeAgo datetime={active} />105</A>106),107},108{109title: "Expire",110dataIndex: "expire",111key: "expire",112align: "center",113render: (_, { id, expire }) => {114return expire ? (115<A href={`/vouchers/${id}`}>116<TimeAgo datetime={expire} />117</A>118) : (119"never"120);121},122},123] as any;124125export default function Created({ customize }) {126const { loading, value, error, setError } = useDatabase(QUERY);127const profile = useProfile({ noCache: true });128const router = useRouter();129const data = useMemo(() => {130const cmp = field_cmp("created");131return (value?.vouchers ?? []).sort((a, b) => -cmp(a, b));132}, [value]);133134return (135<Customize value={customize}>136<Head title="Your Vouchers" />137<Layout>138<Header />139<Layout.Content style={{ background: "white" }}>140<div141style={{142width: "100%",143margin: "10vh 0",144display: "flex",145justifyContent: "center",146}}147>148{profile == null && <Loading />}149{profile != null && !profile.account_id && (150<Card>151<div style={{ fontSize: "75px", textAlign: "center" }}>152<Icon name="gift2" />153</div>154<InPlaceSignInOrUp155title="Created Vouchers"156why="to see vouchers you've created"157style={{ width: "450px" }}158onSuccess={() => {159router.reload();160}}161/>162</Card>163)}164{profile?.account_id && (165<Card style={{ background: "#fafafa" }}>166<Space direction="vertical" align="center">167<A href="/vouchers">168<Icon name="gift2" style={{ fontSize: "75px" }} />169</A>170<h1>Your Vouchers ({data.length})</h1>171{error && (172<Alert173type="error"174message={error}175showIcon176style={{ width: "100%", marginBottom: "30px" }}177closable178onClose={() => setError("")}179/>180)}181{loading && <Loading />}182{!loading && data.length > 0 && (183<Table184columns={COLUMNS}185dataSource={data}186rowKey="id"187pagination={{ defaultPageSize: 50 }}188/>189)}190{!loading && data.length == 0 && (191<div>192You have not <A href="/redeem">redeemed any vouchers</A>{" "}193yet.194</div>195)}196<Help />197</Space>198</Card>199)}200</div>201<Footer />202</Layout.Content>203</Layout>204</Customize>205);206}207208export async function getServerSideProps(context) {209return await withCustomize({ context });210}211212function Status({ when_pay, purchased }) {213if (when_pay == "now") {214return <>Paid</>;215}216if (when_pay == "invoice") {217return purchased?.time ? (218<>219Paid <TimeAgo datetime={purchased.time} />220</>221) : (222<>Invoice at Expiration</>223);224}225if (when_pay == "admin") {226return <>Admin (free)</>;227}228return null;229}230231232