Skip to content

Commit

Permalink
fixed bug clicking out of editcard for new topic creates blank topic.…
Browse files Browse the repository at this point in the history
… changing gDrive interface based on need for user interaction on token renewal
  • Loading branch information
tconfrey committed Mar 8, 2024
1 parent 16aac39 commit 57bdea3
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 158 deletions.
4 changes: 2 additions & 2 deletions app/bt.js
Original file line number Diff line number Diff line change
Expand Up @@ -1278,15 +1278,15 @@ function editRow(e) {

$(".editNode").on('input', function() {
// enable update button if one of the texts is edited and title is not empty
// if (!$("#topicName").val()) return;
if (!$("#topicName").val()) return;
$("#update").prop('disabled', false);
});

$("#editOverlay").click(function(e) {
// click on the backdrop closes the dialog
if (e.target.id == 'editOverlay')
{
closeDialog();
closeDialog(cancelEdit);
$("#buttonRow").show(100);
}
});
Expand Down
157 changes: 80 additions & 77 deletions app/gDriveFileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const gDriveFileManager = (() => {
}

const shouldUseGoogleDriveApi = () => {
// or alternative fetch via url
// return false;
return gapi.client.drive !== undefined;
}
Expand Down Expand Up @@ -121,7 +122,7 @@ const gDriveFileManager = (() => {
} catch (err) {
updateSigninStatus(false);
reject(err);
console.log(err);
console.log("Error renewing token: " + JSON.stringify(err));
}
});
}
Expand Down Expand Up @@ -154,7 +155,8 @@ const gDriveFileManager = (() => {
}
}

async function saveBT(BTFileText) {
async function saveBT(fileText) {
BTFileText = fileText;
try {
// Save org version of BT Tree to gdrive.
await getAccessToken();
Expand Down Expand Up @@ -190,7 +192,7 @@ const gDriveFileManager = (() => {
/**
* Find or initialize BT file at gdrive
*/
var BTFileID;
var BTFileID, BTFileText;
async function findOrCreateBTFile(userInitiated) {
// on launch or explicit user 'connect to Gdrive' action (=> userInitiated)

Expand Down Expand Up @@ -337,7 +339,7 @@ const gDriveFileManager = (() => {
return SaveUnderway || UnwrittenChangesTimer;
}

async function writeBTFile(BTFileText) {
async function writeBTFile() {
// Notification of change to save. Don't write more than once every 15 secs.
// If timer is already set then we're waiting for 15 secs so just return.
// If a save is not underway and its been 15 secs call _write to save
Expand All @@ -362,89 +364,89 @@ const gDriveFileManager = (() => {
console.log("Holding BT file write");
}
}

async function _writeBTFile() {
// Write file contents into BT.org file on GDrive
// NB Have to be careful to keep SaveUnderway up to date on all exit paths
console.log("Writing BT file to gdrive");
UnwrittenChangesTimer = null;

BTFileID = BTFileID || configManager.getProp('BTFileID');
if (!BTFileID) {
alert("BTFileID not set, not saving to GDrive");
return;
}

try {
const accessToken = await getAccessToken();
if (!accessToken) throw new Error("Access token is not available");
}

async function _writeBTFile() {
// Write file contents into BT.org file on GDrive
// NB Have to be careful to keep SaveUnderway up to date on all exit paths
console.log("Writing BT file to gdrive");
UnwrittenChangesTimer = null;

BTFileID = BTFileID || configManager.getProp('BTFileID');
if (!BTFileID) {
alert("BTFileID not set, not saving to GDrive");
return;
}

try {
const accessToken = await getAccessToken();
if (!accessToken) throw new Error("Access token is not available");

// check we're not overwriting remote file
const warn = await checkBTFileVersion();
if (warn && !confirm("There's a newer BrainTool.org file on GDrive. Overwrite it?\nNB changes have been made locally either way."))
return;

// go about saving the file
SaveUnderway = true;
const metadata = {
'name': 'BrainTool.org', // Filename at Google Drive
'mimeType': 'text/plain' // mimeType at Google Drive
};
let form = new FormData();
console.log("writing BT file. accessToken = ", accessToken);
form.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' }));
form.append('file', new Blob([BTFileText], {type: 'text/plain'}));

await fetch('https://www.googleapis.com/upload/drive/v3/files/'
+ encodeURIComponent(BTFileID)
+ '?uploadType=multipart&fields=id,version,modifiedTime',
{
method: 'PATCH',
headers: new Headers({ 'Authorization': 'Bearer ' + accessToken }),
body: form
}).then((res) => {
SaveUnderway = false;
if (!res.ok) {
console.error("BT - error writing to GDrive");
console.log("GAPI response:\n", JSON.stringify(res));
return;
}
return res.json();
}).then(function(val) {
console.log(val);
const mt = Date.parse(val.modifiedTime);
configManager.setProp('BTTimestamp', mt);
updateStatsRow(mt); // update stats when we know successful save
}).catch(function(err) {
SaveUnderway = false;
alert("BT - Error accessing GDrive.");
console.log("Error in writeBTFile: ", JSON.stringify(err));
_renewTokenAndRetryWrite();
// check we're not overwriting remote file
const warn = await checkBTFileVersion();
if (warn && !confirm("There's a newer BrainTool.org file on GDrive. Overwrite it?\nNB changes have been made locally either way."))
return;

// go about saving the file
SaveUnderway = true;
const metadata = {
'name': 'BrainTool.org', // Filename at Google Drive
'mimeType': 'text/plain' // mimeType at Google Drive
};
let form = new FormData();
console.log("writing BT file. accessToken = ", accessToken);
form.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' }));
form.append('file', new Blob([BTFileText], {type: 'text/plain'}));

await fetch('https://www.googleapis.com/upload/drive/v3/files/'
+ encodeURIComponent(BTFileID)
+ '?uploadType=multipart&fields=id,version,modifiedTime',
{
method: 'PATCH',
headers: new Headers({ 'Authorization': 'Bearer ' + accessToken }),
body: form
}).then((res) => {
SaveUnderway = false;
if (!res.ok) {
console.error("BT - error writing to GDrive");
console.log("GAPI response:\n", JSON.stringify(res));
return;
});
}
catch(err) {
}
return res.json();
}).then(function(val) {
console.log(val);
const mt = Date.parse(val.modifiedTime);
configManager.setProp('BTTimestamp', mt);
updateStatsRow(mt); // update stats when we know successful save
}).catch(function(err) {
SaveUnderway = false;
alert("BT - Error saving to GDrive.");
console.log("Error in _writeBTFile: ", JSON.stringify(err));
console.log("Error in writeBTFile: ", JSON.stringify(err));
renewTokenAndRetry(_writeBTFile);
return;
}
});
}
catch(err) {
SaveUnderway = false;
alert("BT - Error saving to GDrive.");
console.log("Error in _writeBTFile: ", JSON.stringify(err));
return;
}
}

function _renewTokenAndRetryWrite() {
function renewTokenAndRetry(cb) {
// The access token is missing, invalid, or expired, or not yet existed, prompt for user consent to obtain one.
if (confirm("BT - Error accessing GDrive. Would you like to renew the token and try again?")) {
renewToken().then(() => {
// Retry the current function here.
_writeBTFile();
}).catch((error) => {
// Clean up aisle five!!! Tell user to reconnect or something.
console.error("Failed to renew token: ", error);
});
}
if (confirm("BT - Error accessing GDrive. Security token expired. Renew the token and try again?")) {
renewToken().then(() => {
// Retry the current function here.
cb();
}).catch((error) => {
// Clean up aisle five!!! Tell user to reconnect or something.
console.error("Failed to renew token: ", error);
});
}
}


async function getBTModifiedTime() {
// query Drive for last modified time
if (!BTFileID || !GDriveConnected) return 0;
Expand All @@ -463,6 +465,7 @@ const gDriveFileManager = (() => {
return Date.parse(response.modifiedTime);
} catch (e) {
console.error('Error reading BT file version from GDrive:', JSON.stringify(e));
renewTokenAndRetry(checkBTFileVersion);
return 0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions versions/Release-Candidate/app/bt.js
Original file line number Diff line number Diff line change
Expand Up @@ -1278,15 +1278,15 @@ function editRow(e) {

$(".editNode").on('input', function() {
// enable update button if one of the texts is edited and title is not empty
// if (!$("#topicName").val()) return;
if (!$("#topicName").val()) return;
$("#update").prop('disabled', false);
});

$("#editOverlay").click(function(e) {
// click on the backdrop closes the dialog
if (e.target.id == 'editOverlay')
{
closeDialog();
closeDialog(cancelEdit);
$("#buttonRow").show(100);
}
});
Expand Down
Loading

0 comments on commit 57bdea3

Please sign in to comment.