Path: blob/master/src/packages/next/components/store/add-to-cart.tsx
1450 views
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import {6delete_local_storage,7get_local_storage,8} from "@cocalc/frontend/misc/local-storage";9import apiPost from "lib/api/post";10import { LS_KEY_LICENSE_PROJECT } from "./util";11import { ALL_FIELDS } from "./quota-query-params";1213// these are the hidden type fields of the forms14// regular and boost end up as "quota" types15// where the description.boost flag is true or false16export type LicenseTypeInForms = "regular" | "boost" | "vm" | "disk";1718interface Props {19form: any;20router: any;21setCartError: (msg: string) => void;22}2324// this is used by the "addBox" and the thin "InfoBar" to add/modify the selected license configuration to the cart25// If something goes wrong it throws an error *and* also calls setCartError.26export async function addToCart({ form, setCartError, router }: Props) {27// we make a copy, because otherwise this actually modifies the fields (user sees brief red errors)28const description = {29...form.getFieldsValue(true),30};31let product;32if (description.numVouchers != null) {33product = "cash-voucher";34} else {35product = "site-license";36}3738if (product == "site-license") {39// exclude extra fields that are for UI only. See https://github.com/sagemathinc/cocalc/issues/625840for (const field in description) {41if (!ALL_FIELDS.has(field)) {42delete description[field];43}44}45description.type = "quota";46description.boost = false;47} else {48description.type = "cash-voucher";49}5051try {52setCartError("");53if (router.query.id != null) {54await apiPost("/shopping/cart/edit", {55id: router.query.id,56description,57});58} else {59// we get the project_id from local storage and save it to the new/edited license60const project_id = get_local_storage(LS_KEY_LICENSE_PROJECT);61delete_local_storage(LS_KEY_LICENSE_PROJECT);6263await apiPost("/shopping/cart/add", {64product,65description,66project_id,67});68}69router.push("/store/cart");70} catch (err) {71setCartError(err.message);72throw err;73}74}757677