-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
APIUsage.js
262 lines (237 loc) · 8.23 KB
/
APIUsage.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import React from "react";
import {
Segment,
Header,
Icon,
Dimmer,
Loader,
Image,
Button,
Modal,
Divider
} from "semantic-ui-react";
import Shell from "./Shell";
import SubscribeForm from "./SubscribeForm";
import ShortParagraphIMG from "../../assets/images/short_paragraph.png";
import { authAxios } from "../../utils";
import { cancelSubscriptionURL } from "../../constants";
import { loadStripe } from '@stripe/stripe-js';
import MyChart from "./Chart";
import { stripePublishKey } from "../../constants";
import { billingURL } from "../../constants";
let present_month = new Date();
let p_month = new Date();
let pp_month = new Date();
let ppp_month = new Date();
let pppp_month = new Date();
let ppppp_month = new Date();
let po_month = new Date();
let ppo_month = new Date();
p_month.setMonth(p_month.getMonth() - 1);
pp_month.setMonth(pp_month.getMonth() - 2);
ppp_month.setMonth(ppp_month.getMonth() - 3);
pppp_month.setMonth(pppp_month.getMonth() - 4);
ppppp_month.setMonth(ppppp_month.getMonth() - 5);
po_month.setMonth(po_month.getMonth() + 1);
ppo_month.setMonth(ppo_month.getMonth() + 2);
let present_month_name = present_month
.toLocaleString('default', {
month: 'long',
})
.substring(0, 3);
let p_month_name = p_month
.toLocaleString('default', { month: 'long' })
.substring(0, 3);
let pp_month_name = pp_month
.toLocaleString('default', { month: 'long' })
.substring(0, 3);
let ppp_month_name = ppp_month
.toLocaleString('default', { month: 'long' })
.substring(0, 3);
let pppp_month_name = pppp_month
.toLocaleString('default', { month: 'long' })
.substring(0, 3);
let ppppp_month_name = ppppp_month
.toLocaleString('default', { month: 'long' })
.substring(0, 3);
let po_month_name = po_month
.toLocaleString('default', { month: 'long' })
.substring(0, 3);
let ppo_month_name = ppo_month
.toLocaleString('default', { month: 'long' })
.substring(0, 3);
let present_date = `${present_month_name}-${present_month.getFullYear()}`;
let p_date = `${p_month_name}-${p_month.getFullYear()}`;
let pp_date = `${pp_month_name}-${pp_month.getFullYear()}`;
let ppp_date = `${ppp_month_name}-${ppp_month.getFullYear()}`;
let pppp_date = `${pppp_month_name}-${pppp_month.getFullYear()}`;
let ppppp_date = `${ppppp_month_name}-${ppppp_month.getFullYear()}`;
let po_date = `${po_month_name}-${po_month.getFullYear()}`;
let ppo_date = `${ppo_month_name}-${ppo_month.getFullYear()}`;
let thedates = [
ppppp_date,
pppp_date,
ppp_date,
pp_date,
p_date,
present_date,
po_date,
ppo_date,
];
//*********************************************** */
const stripePromise = loadStripe(stripePublishKey);
const options = {
mode: 'payment',
amount: 1099,
currency: 'usd',
// Fully customizable with appearance API.
appearance: {
/*...*/
},
};
class APIUsage extends React.Component {
state = {
error: null,
loading: false,
billingDetails: {},
open: false
};
componentDidMount() {
this.handleUserDetails();
}
show = size => () => this.setState({ size, open: true });
close = () => this.setState({ open: false });
handleUnsubscribe = () => {
this.setState({
error: null,
loading: true
});
authAxios
.post(cancelSubscriptionURL)
.then(res => {
this.setState({
loading: false
});
this.close();
this.handleUserDetails();
})
.catch(err => {
this.setState({
error: err.response.data.message,
loading: false
});
});
};
handleUserDetails = () => {
this.setState({
loading: true
});
authAxios
.get(billingURL)
.then(res => {
this.setState({
loading: false,
billingDetails: res.data
});
})
.catch(err => {
this.setState({
loading: false,
error: err.response.data.message
});
});
};
renderBillingDetails(details) {
const free_trial = "free_trial";
const member = "member";
const not_member = "not_member";
let mo = details.api_request_count
let p_mo = details.api_request_count_p
let pp_mo = details.api_request_count_pp
let ppp_mo = details.api_request_count_ppp
let pppp_mo = details.api_request_count_pppp
let ppppp_mo = details.api_request_count_ppppp
let thedata = [ppppp_mo, pppp_mo, ppp_mo, pp_mo, p_mo, mo];
let thelabel = thedates;
return (
<div>
<Header as="h3">Monthly Summary</Header>
{details.membershipType === free_trial ? (
<React.Fragment>
<div >
<MyChart thedata={thedata} thelabel={thelabel} />
</div>
</React.Fragment>
) : details.membershipType === member ? (
<React.Fragment>
<p>Next billing date: {details.next_billing_date}</p>
<p>API requests this month: {details.api_request_count}</p>
<p>Amount due: ${details.amount_due}</p>
<div >
<MyChart thedata={thedata} thelabel={thelabel} />
</div>
<Divider />
<Button onClick={this.show("mini")}>Cancel subscription</Button>
</React.Fragment>
) : details.membershipType === not_member ? (
<React.Fragment>
<p>Your free trial has ended</p>
<div >
<MyChart thedata={thedata} thelabel={thelabel} />
</div>
<SubscribeForm handleUserDetails={this.handleUserDetails} />
</React.Fragment>
) : null
}
</div>
);
}
render() {
const { loading, error, billingDetails, open, size } = this.state;
return (
<React.Fragment>
<Shell>
{error && (
<Segment placeholder>
<Header icon>
<Icon name="rocket" />
Could not fetch your account details. Try reloading the page
</Header>
<a href="/account/billing/">
<Button primary>Reload</Button>
</a>
</Segment>
)}
{loading && (
<Segment>
<Dimmer active inverted>
<Loader inverted>Detecting Facial Expressions ......</Loader>
</Dimmer>
<Image src={ShortParagraphIMG} />
</Segment>
)}
{billingDetails && this.renderBillingDetails(billingDetails)}
</Shell>
<Modal size={size} open={open} onClose={this.close}>
<Modal.Header>Cancel Your Subscription</Modal.Header>
<Modal.Content>
<p>Are you sure you want to cancel your subscription?</p>
</Modal.Content>
<Modal.Actions>
<Button onClick={this.close} negative>
No
</Button>
<Button
positive
icon="checkmark"
labelPosition="right"
content="Yes"
onClick={this.handleUnsubscribe}
/>
</Modal.Actions>
</Modal>
</React.Fragment>
);
}
}
export default APIUsage;