This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
home.js
95 lines (80 loc) · 2.25 KB
/
home.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
import React, { Component } from 'react';
import hello from 'hellojs/dist/hello.all.js';
import axios from 'axios';
class Home extends Component {
constructor(props) {
super(props);
const msft = hello('msft').getAuthResponse();
this.state = {
me: "",
successMessage: "",
token: msft.access_token
};
this.onWriteToExcel = this.onWriteToExcel.bind(this);
this.onLogout = this.onLogout.bind(this);
}
componentDidMount() {
const { token } = this.state;
axios.get(
'https://graph.microsoft.com/v1.0/me?$select=displayName,mail,userPrincipalName',
{ headers: { Authorization: `Bearer ${token}` }}
).then(res => {
const me = res.data;
this.setState({ me });
});
}
onWriteToExcel() {
const { token, me } = this.state;
const myEmailAddress = me.mail || me.userPrincipalName;
const values = [];
values.push([me.displayName, myEmailAddress]);
axios
.post('https://graph.microsoft.com/v1.0/me/drive/root:/demo.xlsx:/workbook/tables/Table1/rows/add',
{ index: null, values },
{ headers: { Authorization: `Bearer ${token}` }}
)
.then(res => {
console.log(res);
const successMessage = "Successfully wrote your data to demo.xlsx!";
this.setState ({ successMessage });
})
.catch(err => console.error(err));
}
onLogout() {
hello('msft').logout().then(
() => this.props.history.push('/'),
e => console.error(e.error.message)
);
}
renderMe() {
const { me } = this.state;
const myEmailAddress = me.mail || me.userPrincipalName;
return (
<tr>
<td>{me.displayName}</td>
<td>{myEmailAddress}</td>
</tr>
);
}
render() {
return (
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{this.renderMe()}
</tbody>
</table>
<button onClick={this.onWriteToExcel}>Write to Excel</button>
<button onClick={this.onLogout}>Logout</button>
<p>{this.state.successMessage}</p>
</div>
);
}
}
export default Home;