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/pages/vouchers/created.tsx
Views: 687
/*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,31cancel_by: null,32title: null,33count: null,34cost: null,35tax: null,36cart: null,37when_pay: null,38purchased: null,39},40],41} as const;4243export const COLUMNS = [44{45title: "ID",46dataIndex: "id",47key: "id",48},49{50title: "Created",51dataIndex: "created",52key: "created",53render: (_, { id, created }) => (54<A href={`/vouchers/${id}`}>{<TimeAgo datetime={created} />}</A>55),56},57{58title: (59<>60Codes61<br />62(click to view)63</>64),65dataIndex: "count",66key: "count",67align: "center",68render: (_, { id, count }) => <A href={`/vouchers/${id}`}>{count}</A>,69},7071{72title: "Cost",73dataIndex: "cost",74key: "cost",75align: "center",76render: (_, { id, cost, tax }) => (77<A href={`/vouchers/${id}`}>78{money(cost, true)}79{tax ? ` (+ ${money(tax, true)} tax)` : ""} each80</A>81),82},83{84title: "Status",85render: (_, { id, when_pay, purchased }) => (86<A href={`/vouchers/${id}`}>87<Status when_pay={when_pay} purchased={purchased} />88</A>89),90},91{92title: "Description",93dataIndex: "title",94key: "title",95render: (_, { title, id }) => {96return <A href={`/vouchers/${id}`}>{title}</A>;97},98},99{100title: "Active",101dataIndex: "active",102key: "active",103align: "center",104render: (_, { id, active }) => (105<A href={`/vouchers/${id}`}>106<TimeAgo datetime={active} />107</A>108),109},110{111title: "Expire",112dataIndex: "expire",113key: "expire",114align: "center",115render: (_, { id, expire }) => (116<A href={`/vouchers/${id}`}>117<TimeAgo datetime={expire} />118</A>119),120},121{122title: "Cancel By",123dataIndex: "cancel_by",124key: "cancel_by",125align: "center",126render: (_, { id, cancel_by }) => (127<A href={`/vouchers/${id}`}>128<TimeAgo datetime={cancel_by} />129</A>130),131},132] as any;133134export default function Created({ customize }) {135const { loading, value, error, setError } = useDatabase(QUERY);136const profile = useProfile({ noCache: true });137const router = useRouter();138const data = useMemo(() => {139const cmp = field_cmp("created");140return (value?.vouchers ?? []).sort((a, b) => -cmp(a, b));141}, [value]);142143return (144<Customize value={customize}>145<Head title="Your Vouchers" />146<Layout>147<Header />148<Layout.Content style={{ background: "white" }}>149<div150style={{151width: "100%",152margin: "10vh 0",153display: "flex",154justifyContent: "center",155}}156>157{profile == null && <Loading />}158{profile != null && !profile.account_id && (159<Card>160<div style={{ fontSize: "75px", textAlign: "center" }}>161<Icon name="gift2"/>162</div>163<InPlaceSignInOrUp164title="Created Vouchers"165why="to see vouchers you've created"166style={{ width: "450px" }}167onSuccess={() => {168router.reload();169}}170/>171</Card>172)}173{profile?.account_id && (174<Card style={{ background: "#fafafa" }}>175<Space direction="vertical" align="center">176<A href="/vouchers">177<Icon name="gift2" style={{ fontSize: "75px" }} />178</A>179<h1>Your Vouchers ({data.length})</h1>180{error && (181<Alert182type="error"183message={error}184showIcon185style={{ width: "100%", marginBottom: "30px" }}186closable187onClose={() => setError("")}188/>189)}190{loading && <Loading />}191{!loading && data.length > 0 && (192<Table193columns={COLUMNS}194dataSource={data}195rowKey="id"196pagination={{ defaultPageSize: 50 }}197/>198)}199{!loading && data.length == 0 && (200<div>201You have not <A href="/redeem">redeemed any vouchers</A>{" "}202yet.203</div>204)}205<Help />206</Space>207</Card>208)}209</div>210<Footer />211</Layout.Content>212</Layout>213</Customize>214);215}216217export async function getServerSideProps(context) {218return await withCustomize({ context });219}220221function Status({ when_pay, purchased }) {222if (when_pay == "now") {223return <>Paid</>;224}225if (when_pay == "invoice") {226return purchased?.time ? (227<>228Paid <TimeAgo datetime={purchased.time} />229</>230) : (231<>Invoice at Expiration</>232);233}234if (when_pay == "admin") {235return <>Admin (free)</>;236}237return null;238}239240241