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/components/store/add-to-cart.tsx
Views: 687
/*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 { getDedicatedDiskKey, PRICES } from "@cocalc/util/upgrades/dedicated";10import apiPost from "lib/api/post";11import { LS_KEY_LICENSE_PROJECT } from "./util";12import { ALL_FIELDS } from "./quota-query-params";1314// these are the hidden type fields of the forms15// regular and boost end up as "quota" types16// where the description.boost flag is true or false17export type LicenseTypeInForms = "regular" | "boost" | "vm" | "disk";1819interface Props {20form: any;21router: any;22setCartError: (msg: string) => void;23}2425// this is used by the "addBox" and the thin "InfoBar" to add/modify the selected license configuration to the cart2627export async function addToCart(props: Props) {28const { form, setCartError, router } = props;2930// we make a copy, because otherwise this actually modifies the fields (user sees brief red errors)31const description = {32...form.getFieldsValue(true),33};3435// exclude extra fields that are for UI only. See https://github.com/sagemathinc/cocalc/issues/625836for (const field in description) {37if (!ALL_FIELDS.has(field)) {38delete description[field];39}40}4142// unload the type parameter43switch (description.type) {44case "boost":45description.boost = true;46description.type = "quota";47break;4849case "vm":50for (const k of ["disk-name", "disk-size_gb", "disk-speed"]) {51delete description[k];52}53const machine = description["vm-machine"];54if (PRICES.vms[machine] == null) {55setCartError(`Unknown machine type ${machine}`);56return;57}58description.dedicated_vm = {59machine,60};61delete description["vm-machine"];62description.type = "vm";63break;6465case "disk":66delete description["vm-machine"];6768const diskID = getDedicatedDiskKey({69size_gb: description["disk-size_gb"],70speed: description["disk-speed"],71});72const disk = PRICES.disks[diskID];73if (disk == null) {74setCartError(`Disk ${diskID} not found`);75return;76}77description.dedicated_disk = {78...disk.quota.dedicated_disk,79name: description["disk-name"],80};81for (const k of ["disk-name", "disk-size_gb", "disk-speed"]) {82delete description[k];83}8485description.type = "disk";86break;8788case "regular":89default:90description.type = "quota";91description.boost = false;92}9394try {95setCartError("");96if (router.query.id != null) {97await apiPost("/shopping/cart/edit", {98id: router.query.id,99description,100});101} else {102// we get the project_id from local storage and save it to the new/edited license103const project_id = get_local_storage(LS_KEY_LICENSE_PROJECT);104delete_local_storage(LS_KEY_LICENSE_PROJECT);105106await apiPost("/shopping/cart/add", {107product: "site-license",108description,109project_id,110});111}112router.push("/store/cart");113} catch (err) {114setCartError(err.message);115}116}117118119