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/redeemed.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, plural } from "@cocalc/util/misc";21import { r_join } from "@cocalc/frontend/components/r_join";22import License from "components/licenses/license";23import Help from "components/vouchers/help";2425const VOUCHER_CODES_QUERY = {26voucher_codes: [27{28code: null,29id: null,30when_redeemed: null,31canceled: null,32license_ids: null,33purchase_ids: null,34},35],36} as const;3738const COLUMNS = [39{40title: "Code",41dataIndex: "code",42key: "code",43},44{45title: "When Redeemed",46dataIndex: "when_redeemed",47key: "when_redeemed",48render: (_, { when_redeemed }) => (49<>50<TimeAgo datetime={when_redeemed} />51</>52),53},54{55title: "Canceled",56dataIndex: "canceled",57key: "canceled",58align: "center",59render: (_, { canceled }) => (canceled ? "Yes" : "-"),60},61{62title: "Licenses",63dataIndex: "license_ids",64key: "license_ids",65render: (_, { license_ids }) => {66if (!license_ids || license_ids.length == 0) return null;67return r_join(68license_ids.map((license_id) => (69<License key={license_id} license_id={license_id} />70))71);72},73},74{75title: "Credits to Account",76dataIndex: "purchase_ids",77key: "purchase_ids",78render: (_, { purchase_ids }) => {79if (!purchase_ids || purchase_ids.length == 0) return null;80return (81<div>82<A href="/settings/purchases" external>83{plural(purchase_ids.length, "Transaction Id")}:{" "}84{purchase_ids.join(", ")}85</A>86</div>87);88},89},90] as any;9192export default function Redeemed({ customize }) {93const { loading, value, error, setError } = useDatabase(VOUCHER_CODES_QUERY);94const profile = useProfile({ noCache: true });95const router = useRouter();96const data = useMemo(() => {97const cmp = field_cmp("when_redeemed");98return (value?.voucher_codes ?? []).sort((a, b) => -cmp(a, b));99}, [value]);100101return (102<Customize value={customize}>103<Head title="Vouchers You Redeemed" />104<Layout>105<Header />106<Layout.Content style={{ background: "white" }}>107<div108style={{109width: "100%",110margin: "10vh 0",111display: "flex",112justifyContent: "center",113}}114>115{profile == null && <Loading />}116{profile != null && !profile.account_id && (117<Card>118<div style={{ fontSize: "75px", textAlign: "center" }}>119<Icon name="gift2"/>120</div>121<InPlaceSignInOrUp122title="Redeemed Vouchers"123why="to see vouchers you've redeemed"124style={{ width: "450px" }}125onSuccess={() => {126router.reload();127}}128/>129</Card>130)}131{profile?.account_id && (132<Card style={{ background: "#fafafa" }}>133<Space direction="vertical" align="center">134<A href="/vouchers">135<Icon name="gift2" style={{ fontSize: "75px" }} />136</A>137<h1>Vouchers You Redeemed</h1>138{error && (139<Alert140type="error"141message={error}142showIcon143style={{ width: "100%", marginBottom: "30px" }}144closable145onClose={() => setError("")}146/>147)}148{loading && <Loading />}149{!loading && data.length > 0 && (150<Table151columns={COLUMNS}152dataSource={data}153rowKey="code"154pagination={{ defaultPageSize: 50 }}155/>156)}157{!loading && data.length == 0 && (158<div>159You have not <A href="/redeem">redeemed any vouchers</A>{" "}160yet.161</div>162)}163<Help />164</Space>165</Card>166)}167</div>168<Footer />169</Layout.Content>170</Layout>171</Customize>172);173}174175export async function getServerSideProps(context) {176return await withCustomize({ context });177}178179180