Path: blob/master/src/packages/frontend/account/upgrades/project-upgrades-table.tsx
1503 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Map } from "immutable";6import { rclass, redux, rtypes, Component } from "../../app-framework";7import { UpgradeAdjustor, r_join } from "../../components";8import { PROJECT_UPGRADES } from "@cocalc/util/schema";9import { ProjectTitle } from "../../projects/project-title";10import { Row, Col, Panel } from "../../antd-bootstrap";11import { plural, len, round1 } from "@cocalc/util/misc";1213interface reduxProps {14get_total_upgrades: Function;15help_email: string;16project_map: Map<string, any>;17get_total_upgrades_you_have_applied: Function;18get_upgrades_you_applied_to_project: Function;19get_total_project_quotas: Function;20get_upgrades_to_project: Function;21get_projects_upgraded_by: Function;22}2324interface State {25show_adjustor: Map<string, boolean>; // project_id : bool26}2728class ProjectUpgradesTable extends Component<reduxProps, State> {29static reduxProps() {30return {31account: {32get_total_upgrades: rtypes.func,33},34customize: {35help_email: rtypes.string,36},37projects: {38project_map: rtypes.immutable.Map,39get_total_upgrades_you_have_applied: rtypes.func,40get_upgrades_you_applied_to_project: rtypes.func,41get_total_project_quotas: rtypes.func,42get_upgrades_to_project: rtypes.func,43get_projects_upgraded_by: rtypes.func,44},45};46}4748constructor(props, state) {49super(props, state);50this.state = {51show_adjustor: Map({}),52};53}5455open_project_settings(e, project_id: string) {56redux.getActions("projects").open_project({57project_id,58target: "settings",59switch_to: !(e.which === 2 || e.ctrlKey || e.metaKey),60});61e.preventDefault();62}6364submit_upgrade_quotas({ project_id, new_quotas }) {65redux66.getActions("projects")67.apply_upgrades_to_project(project_id, new_quotas);68this.toggle_adjustor(project_id);69}7071generate_on_click_adjust(project_id: string) {72return (e) => {73e.preventDefault();74return this.toggle_adjustor(project_id);75};76}7778toggle_adjustor(project_id: string) {79const status = this.state.show_adjustor.get(project_id);80const show_adjustor = this.state.show_adjustor.set(project_id, !status);81this.setState({ show_adjustor });82}8384private render_upgrades_to_project(project_id: string, upgrades) {85const v: React.JSX.Element[] = [];86for (let param in upgrades) {87const val = upgrades[param];88if (!val) {89continue;90}91const info = PROJECT_UPGRADES.params[param];92if (info == null) {93console.warn(94`Invalid upgrades database entry for project_id='${project_id}' -- if this problem persists, email ${this.props.help_email} with the project_id: ${param}`,95);96continue;97}98const n = round1(val != null ? info.display_factor * val : 0);99v.push(100<span key={param}>101{info.display}: {n} {plural(n, info.display_unit)}102</span>,103);104}105return r_join(v);106}107108private render_upgrade_adjustor(project_id: string) {109return (110<UpgradeAdjustor111key={`adjustor-${project_id}`}112total_project_quotas={this.props.get_total_project_quotas(project_id)}113upgrades_you_can_use={this.props.get_total_upgrades()}114upgrades_you_applied_to_all_projects={this.props.get_total_upgrades_you_have_applied()}115upgrades_you_applied_to_this_project={this.props.get_upgrades_you_applied_to_project(116project_id,117)}118quota_params={PROJECT_UPGRADES.params}119submit_upgrade_quotas={(new_quotas) =>120this.submit_upgrade_quotas({ new_quotas, project_id })121}122cancel_upgrading={() => this.toggle_adjustor(project_id)}123style={{ margin: "25px 0px 0px 0px" }}124omit_header={true}125/>126);127}128129private render_upgraded_project(project_id: string, upgrades, darker) {130return (131<Row132key={project_id}133style={darker ? { backgroundColor: "#eee" } : undefined}134>135<Col sm={4}>136<ProjectTitle137project_id={project_id}138handle_click={(e) => this.open_project_settings(e, project_id)}139/>140</Col>141<Col sm={8}>142<a onClick={this.generate_on_click_adjust(project_id)} role="button">143{this.render_upgrades_to_project(project_id, upgrades)}144</a>145</Col>146{this.state.show_adjustor.get(project_id)147? this.render_upgrade_adjustor(project_id)148: undefined}149</Row>150);151}152153private render_upgraded_projects_rows(upgraded_projects): React.JSX.Element[] {154let i = -1;155const result: React.JSX.Element[] = [];156for (let project_id in upgraded_projects) {157const upgrades = upgraded_projects[project_id];158i += 1;159result.push(160this.render_upgraded_project(project_id, upgrades, i % 2 === 0),161);162}163return result;164}165166private render_header() {167return (168<div>169<Row>170<Col sm={12} style={{ display: "flex" }}>171<div style={{ flex: "1" }}>172Upgrades you have applied to projects173</div>174</Col>175</Row>176</div>177);178}179180render() {181const upgraded_projects = this.props.get_projects_upgraded_by();182if (!len(upgraded_projects)) {183return null;184}185return (186<Panel header={this.render_header()}>187<Row key="header">188<Col sm={4}>189<strong>Project</strong>190</Col>191<Col sm={8}>192<strong>193Upgrades you have applied to this project (click to edit)194</strong>195</Col>196</Row>197{this.render_upgraded_projects_rows(upgraded_projects)}198</Panel>199);200}201}202203const tmp = rclass(ProjectUpgradesTable);204export { tmp as ProjectUpgradesTable };205206207