-
Notifications
You must be signed in to change notification settings - Fork 746
/
App.tsx
176 lines (152 loc) · 6.01 KB
/
App.tsx
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
import * as React from 'react';
import { Spinner, SpinnerType } from 'office-ui-fabric-react';
import Header from './Header';
import { HeroListItem } from './HeroList';
import Progress from './Progress';
import StartPageBody from './StartPageBody';
import GetDataPageBody from './GetDataPageBody';
import SuccessPageBody from './SuccessPageBody';
import OfficeAddinMessageBar from './OfficeAddinMessageBar';
import { getGraphData } from '../../utilities/microsoft-graph-helpers';
import { writeFileNamesToWorksheet, logoutFromO365, signInO365 } from '../../utilities/office-apis-helpers';
export interface AppProps {
title: string;
isOfficeInitialized: boolean;
}
export interface AppState {
authStatus?: string;
fileFetch?: string;
headerMessage?: string;
errorMessage?: string;
}
export default class App extends React.Component<AppProps, AppState> {
constructor(props, context) {
super(props, context);
this.state = {
authStatus: 'notLoggedIn',
fileFetch: 'notFetched',
headerMessage: 'Welcome',
errorMessage: ''
};
// Bind the methods that we want to pass to, and call in, a separate
// module to this component. And rename setState to boundSetState
// so code that passes boundSetState is more self-documenting.
this.boundSetState = this.setState.bind(this);
this.setToken = this.setToken.bind(this);
this.displayError = this.displayError.bind(this);
this.login = this.login.bind(this);
}
/*
Properties
*/
// The access token is not part of state because React is all about the
// UI and the token is not used to affect the UI in any way.
accessToken: string;
listItems: HeroListItem[] = [
{
icon: 'PlugConnected',
primaryText: 'Connects to OneDrive for Business.'
},
{
icon: 'ExcelDocument',
primaryText: 'Gets the names of the first three workbooks in OneDrive for Business.'
},
{
icon: 'AddNotes',
primaryText: 'Adds the names to the current document.'
}
];
/*
Methods
*/
boundSetState: () => {};
setToken = (accesstoken: string) => {
this.accessToken = accesstoken;
}
displayError = (error: string) => {
this.setState({ errorMessage: error });
}
// Runs when the user clicks the X to close the message bar where
// the error appears.
errorDismissed = () => {
this.setState({ errorMessage: '' });
// If the error occured during a "in process" phase (logging in or getting files),
// the action didn't complete, so return the UI to the preceding state/view.
this.setState((prevState) => {
if (prevState.authStatus === 'loginInProcess') {
return {authStatus: 'notLoggedIn'};
}
else if (prevState.fileFetch === 'fetchInProcess') {
return {fileFetch: 'notFetched'};
}
return null;
});
}
login = async () => {
await signInO365(this.boundSetState, this.setToken, this.displayError);
}
logout = async () => {
await logoutFromO365(this.boundSetState, this.displayError);
}
getFileNames = async () => {
this.setState({ fileFetch: 'fetchInProcess' });
getGraphData(
// Get the `name` property of the first 3 Excel workbooks in the user's OneDrive.
"https://graph.microsoft.com/v1.0/me/drive/root/microsoft.graph.search(q = '.xlsx')?$select=name&top=3",
this.accessToken
)
.then( async (response) => {
await writeFileNamesToWorksheet(response, this.displayError);
this.setState({ fileFetch: 'fetched',
headerMessage: 'Success' });
})
.catch ( (requestError) => {
// If this runs, then the `then` method did not run, so this error must be
// from the Axios request in getGraphData, not the Office.js in
// writeFileNamesToWorksheet
this.displayError(requestError);
});
}
render() {
const { title, isOfficeInitialized } = this.props;
if (!isOfficeInitialized) {
return (
<Progress
title={title}
logo='assets/Onedrive_Charts_icon_80x80px.png'
message='Please sideload your add-in to see app body.'
/>
);
}
// Set the body of the page based on where the user is in the workflow.
let body;
if (this.state.authStatus === 'notLoggedIn') {
body = ( <StartPageBody login={this.login} listItems={this.listItems}/> );
}
else if (this.state.authStatus === 'loginInProcess') {
body = ( <Spinner className='spinner' type={SpinnerType.large} label='Please sign-in on the pop-up window.' /> );
}
else {
if (this.state.fileFetch === 'notFetched') {
body = ( <GetDataPageBody getFileNames={this.getFileNames} logout={this.logout}/> );
}
else if (this.state.fileFetch === 'fetchInProcess') {
body = ( <Spinner className='spinner' type={SpinnerType.large} label='We are getting the data for you.' /> );
}
else {
body = ( <SuccessPageBody getFileNames={this.getFileNames} logout={this.logout} /> );
}
}
return (
<div>
{ this.state.errorMessage ?
(<OfficeAddinMessageBar onDismiss={this.errorDismissed} message={this.state.errorMessage + ' '} />)
: null }
<div className='ms-welcome'>
<Header logo='assets/Onedrive_Charts_icon_80x80px.png' title={this.props.title} message={this.state.headerMessage} />
{body}
</div>
</div>
);
}
}