Created
March 6, 2021 02:01
-
-
Save rolangom/c1025d599cd41739034d93714afe81a8 to your computer and use it in GitHub Desktop.
HojApp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<!-- <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> --> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<meta name="theme-color" content="#000000" /> | |
<meta | |
name="description" | |
content="Sheet API Test" | |
/> | |
<!-- <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> --> | |
<!-- | |
manifest.json provides metadata used when your web app is installed on a | |
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ | |
--> | |
<!-- <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> --> | |
<!-- | |
Notice the use of %PUBLIC_URL% in the tags above. | |
It will be replaced with the URL of the `public` folder during the build. | |
Only files inside the `public` folder can be referenced from the HTML. | |
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will | |
work correctly both with client-side routing and a non-root public URL. | |
Learn how to configure a non-root public URL by running `npm run build`. | |
--> | |
<title>React App</title> | |
</head> | |
<body> | |
<noscript>You need to enable JavaScript to run this app.</noscript> | |
<div id="root"></div> | |
<!-- | |
This HTML file is a template. | |
If you open it directly in the browser, you will see an empty page. | |
You can add webfonts, meta tags, or analytics to this file. | |
The build step will place the bundled scripts into the <body> tag. | |
To begin the development, run `npm start` or `yarn start`. | |
To create a production bundle, use `npm run build` or `yarn build`. | |
--> | |
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> | |
<!-- Note: when deploying, replace "development.js" with "production.min.js". --> | |
<script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script> | |
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script> | |
<script> | |
// class Subject { | |
// subs = []; | |
// lastValues = null; | |
// unsubscribe(f) { | |
// console.log('unsubscribed'); | |
// this.subs = this.subs.filter(it => f !== it); | |
// } | |
// subscribe(f) { | |
// console.log('subscribed'); | |
// this.subs.push(f); | |
// if (lastValues) { | |
// this.dispatchCallbaks(); | |
// } | |
// return () => this.unsubscribe(f); | |
// } | |
// dispatchCallbaks() { | |
// this.subs.forEach(f => f(...this.lastValues)); | |
// } | |
// onNext(...vals) { | |
// console.log('onNext called'); | |
// this.lastValues = vals; | |
// dispatchCallbaks(); | |
// } | |
// } | |
// const clientLoadedSubject = new Subject(); | |
// clientLoadedSubject.subscribe(() => sessionStorage.setItem('gapi-loaded', '1')); | |
// onload="this.onload=function(){};clientLoadedSubject.onNext()" | |
// onreadystatechange="if (this.readyState === 'complete') this.onload()" | |
</script> | |
<script src="https://apis.google.com/js/api.js"></script> | |
<script src="script.js" type="text/babel"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CLIENT_ID = '972825688380-foim3h1up890vlpq91vclt83bidvm7s3.apps.googleusercontent.com'; | |
const API_KEY = 'AIzaSyB5WhFpENPLUlpDrTjvfm2XH_gx2eebQT0' | |
const DISCOVERY_DOCS = ["https://sheets.googleapis.com/$discovery/rest?version=v4","https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"]; | |
const SCOPES = "https://www.googleapis.com/auth/spreadsheets.readonly https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/drive.file"; | |
const APP_NAME = 'HojApp' | |
class CodedError extends Error { | |
code; | |
constructor(code, message) { | |
super(message); | |
this.code = code; | |
} | |
toString() { | |
return super.toString() + `(${this.code})`; | |
} | |
} | |
function listDriveFiles(name, isFolder=false) { | |
return gapi.client.drive.files.list({ | |
'pageSize': 10, | |
'fields': "nextPageToken, files(id, name)", | |
'q': `mimeType ${isFolder ? '=' : '!='} 'application/vnd.google-apps.folder' and name='${name}'`, | |
}) | |
.then(resp => resp.result.files, response => Promise.reject(Error(response.result.error.message))) | |
} | |
/** | |
* | |
* @param {string} name | |
* @returns {Promise<string>} | |
*/ | |
function getFolderId(name) { | |
return listDriveFiles(name, true) | |
.then(list => list.length > 0 ? list[0].id : Promise.reject(new CodedError(`FOLDER_NOT_EXISTS`, 'Folder no existe.'))); | |
} | |
function getSheetValues(spreadsheetId, range) { | |
return gapi.client.sheets.spreadsheets.values.get({ | |
spreadsheetId, | |
range, | |
}) | |
.then((response) => response.result.values, response => Promise.reject(Error(response.result.error.message))); | |
} | |
function Login() { | |
function handleAuthClick(event) { | |
gapi.auth2.getAuthInstance().signIn(); | |
} | |
return ( | |
<div> | |
<h2>Login</h2> | |
<button onClick={handleAuthClick}>Login</button> | |
</div> | |
); | |
} | |
function Home() { | |
const [majors, setMajors] = React.useState([]); | |
function listMarjors() { | |
console.log('listMarjors TODO'); | |
// getSheetValues('1HCuK2nyVAomMGCRetoTkWpwM8Pv6YGpuSa7fAOArpUA', 'Sheet1!A2:') | |
// .then(setMajors, err => [err.message]); | |
getFolderId(APP_NAME, true) | |
.then(); | |
} | |
function handleSignoutClick() { | |
gapi.auth2.getAuthInstance().signOut(); | |
} | |
return ( | |
<div> | |
<h2>Inicio</h2> | |
<button onClick={handleSignoutClick}>Sign out</button> | |
<button onClick={listMarjors}>List Majors</button> | |
<h3>Majors</h3> | |
<ul> | |
{majors.map(it => <li key={it}>{it}</li>)} | |
</ul> | |
</div> | |
) | |
} | |
function App() { | |
const [isLogged, setLogged] = React.useState(undefined); | |
React.useEffect(() => { | |
function initClient() { | |
console.log('initClient') | |
gapi.client.init({ | |
apiKey: API_KEY, | |
clientId: CLIENT_ID, | |
discoveryDocs: DISCOVERY_DOCS, | |
scope: SCOPES | |
}).then(() => { | |
// Listen for sign-in state changes. | |
gapi.auth2.getAuthInstance().isSignedIn.listen(setLogged); | |
// Handle the initial sign-in state. | |
setLogged(gapi.auth2.getAuthInstance().isSignedIn.get()); | |
}, (error) => { | |
console.error('gapi.client.init error', error); | |
}); | |
} | |
function handleClientLoad() { | |
console.log('handleClientLoad') | |
gapi.load('client:auth2', initClient); | |
} | |
gapi.load('client', handleClientLoad); | |
},[]) | |
return ( | |
<div> | |
<h1>Bienvenido</h1> | |
{isLogged === undefined ? <p>Loading...</p>: null} | |
{isLogged ? <Home /> : <Login />} | |
</div> | |
); | |
} | |
ReactDOM.render(<App />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment