/
KeyListItem.jsx
180 lines (160 loc) · 5.71 KB
/
KeyListItem.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import React, { Component, Fragment } from 'react'
import _ from 'lodash'
import {
Badge,
UncontrolledTooltip,
DropdownMenu,
DropdownItem,
DropdownToggle,
UncontrolledButtonDropdown
} from 'reactstrap'
import api from '../../../api'
export default class KeyListItem extends Component {
state = {
isCancelled: false,
isLoading: false
}
componentDidMount() {
this.calculateCost()
}
componentDidUpdate(prevProps) {
if (prevProps.license !== this.props.license) {
this.calculateCost()
}
}
calculateCost() {
const nodePrice = _.get(this.props, 'license.limits.nodes', 0) * this.getPrice('full-time-node')
const goldSupport = _.get(this.props, 'license.quantities.isGoldSupport', false) ? this.getPrice('gold-support') : 0
this.setState({ cost: this.getPrice('pro') + nodePrice + goldSupport })
}
getPrice(productName) {
if (!this.props.products) {
return
}
const product = this.props.products.find(p => p.product === productName)
return product && product.price
}
disableAutoRenew = async () => {
const { license } = this.props
if (window.confirm('Are you sure you want to cancel auto-renew for this license?')) {
try {
const licensing = await api.getLicensing()
await licensing.delete(`/me/keys/${license.stripeSubscriptionId}`)
this.props.onLicenseUpdated({ ...license, canceled: true })
} catch (error) {
console.error('error canceling license')
}
}
}
revealLicense = () => this.props.onRevealActivate(this.props.license)
updateLicense = () => this.props.onLicenseUpdated({ ...this.props.license, cost: this.state.cost })
assignFingerprint = () => this.props.onRevealActivate({ ...this.props.license, assigned: false })
useOnServer = async () => {
this.setState({ isLoading: true })
try {
const key = await this.activateWithServerFingerprint()
await this.updateServerKey(key)
this.props.onLicenseUpdated({ ...this.props.license, fingerprint: this.props.clusterFingerprint, assigned: true })
} catch (error) {
console.log('error while setting up license')
}
this.setState({ isLoading: false })
}
updateServerKey = async licenseKey => {
await api.getSecured().post(
'admin/license/update',
{
licenseKey
},
{
timeout: 10 * 1000
}
)
}
activateWithServerFingerprint = async () => {
const licensing = await api.getLicensing()
const res = await licensing.post(`/me/keys/${this.props.license.stripeSubscriptionId}/activate`, {
fingerprint: this.props.clusterFingerprint
})
return res.data.key
}
renderActiveBadge() {
const badgeId = this.props.license.stripeSubscriptionId
return (
<Fragment>
<Badge id={`active${badgeId}`} color="primary" style={{ float: 'right', marginRight: '2px' }}>
Active
</Badge>
<UncontrolledTooltip placement="right" target={`active${badgeId}`}>
This license matches the fingerprint of the current server
</UncontrolledTooltip>
</Fragment>
)
}
renderCancelledBadge() {
const badgeId = this.props.license.stripeSubscriptionId
const expiry = new Date(this.props.license.paidUntil).toLocaleDateString()
return (
<Fragment>
<Badge id={`cancelled${badgeId}`} color="danger" style={{ float: 'right', marginRight: '2px' }}>
Cancelled
</Badge>
<UncontrolledTooltip placement="right" target={`cancelled${badgeId}`}>
Auto-Renew is disabled for this license and it will expire on {expiry}
</UncontrolledTooltip>
</Fragment>
)
}
renderActions() {
const { license } = this.props
return (
<UncontrolledButtonDropdown>
<DropdownToggle caret size="sm" outline disabled={this.state.isLoading}>
Actions
</DropdownToggle>
<DropdownMenu>
{license.assigned && <DropdownItem onClick={this.revealLicense}>Reveal License Key</DropdownItem>}
<DropdownItem onClick={this.assignFingerprint}>Assign Fingerprint</DropdownItem>
{this.props.clusterFingerprint && <DropdownItem onClick={this.useOnServer}>Use on this Server</DropdownItem>}
<DropdownItem onClick={this.updateLicense}>Update License</DropdownItem>
{!license.canceled && <DropdownItem onClick={this.disableAutoRenew}>Disable Auto-Renew</DropdownItem>}
</DropdownMenu>
</UncontrolledButtonDropdown>
)
}
render() {
const { license } = this.props
const assignedClass = license.assigned ? 'assigned' : 'not-assigned'
const isActive = this.props.clusterFingerprint && this.props.clusterFingerprint === license.fingerprint
return (
<tr disabled={license.canceled}>
<td>
<span className="table--keys__users">
{license.label}
{license.canceled && this.renderCancelledBadge()}
{isActive && this.renderActiveBadge()}
</span>
</td>
<td>
<span className="table--keys__users">{license.limits && Number(license.limits.nodes) + 1}</span>
</td>
<td>
<span className="table--keys__users">{license.quantities.isGoldSupport ? 'Gold' : 'Standard'}</span>
</td>
<td>
<span className={`table--keys__assigned ${assignedClass}`}>{license.assigned ? 'Yes' : 'No'}</span>
</td>
<td>
<span className="table--keys__users">
{license.canceled ? 'Disabled' : new Date(license.paidUntil).toLocaleDateString()}
</span>
</td>
<td>
<span className="table--keys__cost">{this.state.cost}$ / month</span>
</td>
<td>{this.renderActions()}</td>
</tr>
)
}
}