Skip to content

Commit

Permalink
Update index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
momentollogy committed Apr 13, 2023
1 parent 239e92b commit 97a3d93
Showing 1 changed file with 26 additions and 39 deletions.
65 changes: 26 additions & 39 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>
<div id="recording"></div>
<!-- ... existing script tag ... -->

<div class="g-signin2" data-onsuccess="onSignIn"></div>
<div class="g-signin2" data-onsuccess="onSignIn" data-onfailure="handleSignInFailure"></div>
<button onclick="signOut()">Sign out</button>


Expand Down Expand Up @@ -131,18 +131,30 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>

// API STUFF
function onSignIn(googleUser) {
const token = googleUser.getAuthResponse().id_token;
gapi.load('client', () => initClient(token));
// Show the sign-out button
const signOutButton = document.createElement('button');
signOutButton.textContent = 'Sign Out';
signOutButton.onclick = signOut;
document.body.appendChild(signOutButton);
}

function onSignInFailure(error) {
console.error('Error signing in:', error);
}

function signOut() {
const auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(() => {
console.log('User signed out.');
gapi.client = null;
// Remove the sign-out button
const signOutButton = document.querySelector('button[onclick="signOut()"]');
if (signOutButton) {
document.body.removeChild(signOutButton);
}
});
}


function initClient(token) {
gapi.client.init({
apiKey: 'AIzaSyA90ftyrgjmJPfzKeXYypzBdO9IJlbHU78',
Expand All @@ -158,6 +170,9 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>
});
}

function handleSignInFailure(error) {
console.error('Error during sign-in:', error);
}



Expand Down Expand Up @@ -599,10 +614,6 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>



async function handleClientLoad() {
await gapi.load('client:auth2', initClient);
}

async function initClient() {
try {
await gapi.client.init({
Expand All @@ -616,8 +627,6 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>
}




document.getElementById('postSongButton').addEventListener('click', saveSong);
document.getElementById('loadSongButton').addEventListener('click', loadSong);

Expand Down Expand Up @@ -674,41 +683,13 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>
}


async function saveSongToSheet(songData) {
const sheetId = '1C9cqSvijRkJepxErd_jL0HQ4PCrjC0h7arOYj41DDOg';
const range = 'Sheet1!A:C';
const sheetsApi = 'https://sheets.googleapis.com/v4/spreadsheets';

const requestBody = {
values: [songData],
};

const response = await fetch(`${sheetsApi}/${sheetId}/values/${range}:append?valueInputOption=RAW&insertDataOption=INSERT_ROWS`, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + gapi.auth.getToken().access_token,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});

const result = await response.json();

if (result.updates) {
alert('Song saved successfully.');
} else {
alert('An error occurred while saving the song.');
}
}




async function fetchSongsFromSheet() {
const sheetId = '1C9cqSvijRkJepxErd_jL0HQ4PCrjC0h7arOYj41DDOg';
const range = 'A:B';
const sheetsApi = 'https://sheets.googleapis.com/v4/spreadsheets';
const apiKey = 'AIzaSyA90ftyrgjmJPfzKeXYypzBdO9IJlbHU78'; // Replace this with your API key
const apiKey = 'AIzaSyA90ftyrgjmJPfzKeXYypzBdO9IJlbHU78';

const response = await fetch(`${sheetsApi}/${sheetId}/values/${range}?key=${apiKey}`, {
headers: {
Expand All @@ -730,6 +711,12 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>

</script>

<script>
async function handleClientLoad() {
await gapi.load('client:auth2', initClient);
}
</script>

<script async defer src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()">
Expand Down

0 comments on commit 97a3d93

Please sign in to comment.