Path: blob/master/src/packages/frontend/billing/actions.ts
1503 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// COMPLETEY DEPRECATED -- DELETE THIS ?67/*8Billing actions.910These are mainly for interfacing with Stripe. They are11all async (no callbacks!).1213**PRETTY MUCH DEPRECATED**14*/1516import { fromJS, Map } from "immutable";17import { redux, Actions, Store } from "../app-framework";18import { reuse_in_flight_methods } from "@cocalc/util/async-utils";19import { server_days_ago } from "@cocalc/util/misc";20import { getManagedLicenses } from "../account/licenses/util";2122import { BillingStoreState } from "./store";2324require("./store"); // ensure 'billing' store is created so can set this.store below.2526export class BillingActions extends Actions<BillingStoreState> {27private store: Store<BillingStoreState>;2829constructor(name: string, redux: any) {30super(name, redux);31const store = redux.getStore("billing");32if (store == null) throw Error("bug -- billing store should be defined");33this.store = store;34reuse_in_flight_methods(this, ["update_customer"]);35}3637public clear_error(): void {38this.setState({ error: "" });39}4041public async update_customer(): Promise<void> {42return;43}4445public clear_action(): void {46this.setState({ action: "", error: "" });47}4849public clear_coupon_error(): void {50this.setState({ coupon_error: "" });51}5253public remove_all_coupons(): void {54this.setState({ applied_coupons: Map<string, any>(), coupon_error: "" });55}5657public remove_coupon(coupon_id: string): void {58this.setState({59applied_coupons: this.store60.get("applied_coupons", Map<string, any>())61.delete(coupon_id),62});63}6465// Set this while we are paying for the course.66public set_is_paying_for_course(67project_id: string,68is_paying: boolean,69): void {70let course_pay = this.store.get("course_pay");71let continue_first_purchase = this.store.get("continue_first_purchase");72if (is_paying) {73course_pay = course_pay.add(project_id);74} else {75course_pay = course_pay.remove(project_id);76continue_first_purchase = false;77}78this.setState({ course_pay, continue_first_purchase });79}8081public set_selected_plan(plan: string, period?: string): void {82if (period != null) {83if (period.slice(0, 4) == "year") {84plan += "-year";85} else if (period.slice(0, 4) == "week") {86plan += "-week";87}88}89this.setState({ selected_plan: plan });90}9192public async update_managed_licenses(): Promise<void> {93// Update the license state in the frontend94const v = await getManagedLicenses();95const all_managed_license_ids = fromJS(v.map((x) => x.id)) as any;9697const day_ago = server_days_ago(1);98const managed_license_ids = fromJS(99v100.filter((x) => x.expires == null || x.expires >= day_ago)101.map((x) => x.id),102) as any;103104const x: { [license_id: string]: object } = {};105for (const license of v) {106x[license.id] = license;107}108const managed_licenses = fromJS(x) as any;109this.setState({110managed_licenses,111managed_license_ids,112all_managed_license_ids,113});114}115}116117export const actions = redux.createActions("billing", BillingActions);118119120