Browse Source

further fixes

master
Bernd-René Predota 4 years ago
parent
commit
13f2c309e6
  1. 40
      src/App.js

40
src/App.js

@ -21,8 +21,7 @@ class App extends Component {
"1TT4oJ7B_Lq4quyizxK2DxGR_qhYtaG4Mn40j2MNpB6E"; "1TT4oJ7B_Lq4quyizxK2DxGR_qhYtaG4Mn40j2MNpB6E";
this.state = { this.state = {
signedIn: undefined,
token: undefined,
signedIn: false,
accounts: [], accounts: [],
categories: [], categories: [],
expenses: [], expenses: [],
@ -35,31 +34,32 @@ class App extends Component {
} }
componentDidMount() { componentDidMount() {
window.google.accounts.oauth2.initTokenClient({
client_id: this.clientId,
scope: 'https://www.googleapis.com/auth/spreadsheets.readonly',
callback: (tokenResponse) => {
this.setState({token: tokenResponse.access_token});
this.setState({signedIn: true});
if (this.state.signedIn) {
this.load();
}
},
}).requestAccessToken();
window.gapi.load("client", () => { window.gapi.load("client", () => {
window.gapi.client.init({ window.gapi.client.init({
}).then(function () { }).then(function () {
window.gapi.client.load('sheets', 'v4'); window.gapi.client.load('sheets', 'v4');
}).then(function (response) {
}).then(function () {
console.log('discovery document loaded'); console.log('discovery document loaded');
}, function (reason) { }, function (reason) {
console.log('Error: ' + reason.result.error.message); console.log('Error: ' + reason.result.error.message);
}); });
}); });
this.client = window.google.accounts.oauth2.initTokenClient({
client_id: this.clientId,
scope: 'https://www.googleapis.com/auth/spreadsheets',
callback: (tokenResponse) => {
if (tokenResponse && tokenResponse.access_token) {
this.setState({signedIn: tokenResponse});
window.gapi.auth.setToken({access_token: tokenResponse.access_token})
// window.gapi.client.load('sheets', 'v4');
this.load();
}
},
});
} }
revokeToken() { revokeToken() {
window.google.accounts.oauth2.revoke(this.state.token, () => {console.log('access token revoked')});
window.google.accounts.oauth2.revoke(this.state.signedIn.access_token, () => {console.log('access token revoked')});
} }
@ -193,6 +193,7 @@ class App extends Component {
} }
load() { load() {
console.log('am i here? 1')
window.gapi.client.sheets.spreadsheets.values window.gapi.client.sheets.spreadsheets.values
.batchGet({ .batchGet({
spreadsheetId: this.spreadsheetId, spreadsheetId: this.spreadsheetId,
@ -205,12 +206,14 @@ class App extends Component {
] ]
}) })
.then(response => { .then(response => {
console.log(response)
const accounts = response.result.valueRanges[0].values.map( const accounts = response.result.valueRanges[0].values.map(
items => items[0] items => items[0]
); );
const categories = response.result.valueRanges[1].values.map( const categories = response.result.valueRanges[1].values.map(
items => items[0] items => items[0]
); );
console.log('am i here? 2')
this.setState({ this.setState({
accounts: accounts, accounts: accounts,
categories: categories, categories: categories,
@ -244,7 +247,7 @@ class App extends Component {
alt="Sign in" alt="Sign in"
onClick={e => { onClick={e => {
e.preventDefault(); e.preventDefault();
window.gapi.auth2.getAuthInstance().signIn();
this.client.requestAccessToken();
}} }}
> >
perm_identity perm_identity
@ -256,7 +259,7 @@ class App extends Component {
alt="Sign out" alt="Sign out"
onClick={e => { onClick={e => {
e.preventDefault(); e.preventDefault();
window.gapi.auth2.getAuthInstance().signOut();
this.revokeToken();
}} }}
> >
exit_to_app exit_to_app
@ -265,6 +268,7 @@ class App extends Component {
</div> </div>
</header> </header>
<div className="toolbar-adjusted-content"> <div className="toolbar-adjusted-content">
{console.log(this.state.signedIn)}
{this.state.signedIn === undefined && <LoadingBar />} {this.state.signedIn === undefined && <LoadingBar />}
{this.state.signedIn === false && {this.state.signedIn === false &&
<div className="center"> <div className="center">
@ -272,7 +276,7 @@ class App extends Component {
className="mdc-button sign-in" className="mdc-button sign-in"
aria-label="Sign in" aria-label="Sign in"
onClick={() => { onClick={() => {
window.gapi.auth2.getAuthInstance().signIn();
this.client.requestAccessToken();
}} }}
> >
Sign In Sign In

Loading…
Cancel
Save