Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/billing/actions.ts
1503 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// COMPLETEY DEPRECATED -- DELETE THIS ?
7
8
/*
9
Billing actions.
10
11
These are mainly for interfacing with Stripe. They are
12
all async (no callbacks!).
13
14
**PRETTY MUCH DEPRECATED**
15
*/
16
17
import { fromJS, Map } from "immutable";
18
import { redux, Actions, Store } from "../app-framework";
19
import { reuse_in_flight_methods } from "@cocalc/util/async-utils";
20
import { server_days_ago } from "@cocalc/util/misc";
21
import { getManagedLicenses } from "../account/licenses/util";
22
23
import { BillingStoreState } from "./store";
24
25
require("./store"); // ensure 'billing' store is created so can set this.store below.
26
27
export class BillingActions extends Actions<BillingStoreState> {
28
private store: Store<BillingStoreState>;
29
30
constructor(name: string, redux: any) {
31
super(name, redux);
32
const store = redux.getStore("billing");
33
if (store == null) throw Error("bug -- billing store should be defined");
34
this.store = store;
35
reuse_in_flight_methods(this, ["update_customer"]);
36
}
37
38
public clear_error(): void {
39
this.setState({ error: "" });
40
}
41
42
public async update_customer(): Promise<void> {
43
return;
44
}
45
46
public clear_action(): void {
47
this.setState({ action: "", error: "" });
48
}
49
50
public clear_coupon_error(): void {
51
this.setState({ coupon_error: "" });
52
}
53
54
public remove_all_coupons(): void {
55
this.setState({ applied_coupons: Map<string, any>(), coupon_error: "" });
56
}
57
58
public remove_coupon(coupon_id: string): void {
59
this.setState({
60
applied_coupons: this.store
61
.get("applied_coupons", Map<string, any>())
62
.delete(coupon_id),
63
});
64
}
65
66
// Set this while we are paying for the course.
67
public set_is_paying_for_course(
68
project_id: string,
69
is_paying: boolean,
70
): void {
71
let course_pay = this.store.get("course_pay");
72
let continue_first_purchase = this.store.get("continue_first_purchase");
73
if (is_paying) {
74
course_pay = course_pay.add(project_id);
75
} else {
76
course_pay = course_pay.remove(project_id);
77
continue_first_purchase = false;
78
}
79
this.setState({ course_pay, continue_first_purchase });
80
}
81
82
public set_selected_plan(plan: string, period?: string): void {
83
if (period != null) {
84
if (period.slice(0, 4) == "year") {
85
plan += "-year";
86
} else if (period.slice(0, 4) == "week") {
87
plan += "-week";
88
}
89
}
90
this.setState({ selected_plan: plan });
91
}
92
93
public async update_managed_licenses(): Promise<void> {
94
// Update the license state in the frontend
95
const v = await getManagedLicenses();
96
const all_managed_license_ids = fromJS(v.map((x) => x.id)) as any;
97
98
const day_ago = server_days_ago(1);
99
const managed_license_ids = fromJS(
100
v
101
.filter((x) => x.expires == null || x.expires >= day_ago)
102
.map((x) => x.id),
103
) as any;
104
105
const x: { [license_id: string]: object } = {};
106
for (const license of v) {
107
x[license.id] = license;
108
}
109
const managed_licenses = fromJS(x) as any;
110
this.setState({
111
managed_licenses,
112
managed_license_ids,
113
all_managed_license_ids,
114
});
115
}
116
}
117
118
export const actions = redux.createActions("billing", BillingActions);
119
120