diff --git a/src/js/classes/storage.js b/src/js/classes/storage.js index a03da76e..f1c10100 100644 --- a/src/js/classes/storage.js +++ b/src/js/classes/storage.js @@ -248,7 +248,7 @@ export const StorageJs = (type = 'gist') => { // try to fetch without authorisation return fetch(fetchAddress).then(data=> { console.warn("Got data without authorisation") - this.isTokenInvalid = true; + // this.isTokenInvalid = true; return data.json() }) } @@ -318,7 +318,7 @@ export const StorageJs = (type = 'gist') => { }).then(result=> result.json()).then(response => { this.setLastStorageHost('GIST'); const file = response.files && fileName in response.files ? response.files[fileName] : undefined; - return { response, file }; + return { response, file, gistId: this.gistId }; }); }, editGistFile: function(fileName, content) { diff --git a/src/public/plugins/resources-editor.js b/src/public/plugins/resources-editor.js index 8a275be0..6d402d5c 100644 --- a/src/public/plugins/resources-editor.js +++ b/src/public/plugins/resources-editor.js @@ -24,9 +24,9 @@ export var ResourcesEditor = function({ return true; }) this.getFromGist = () => { - return new Promise(resolve => { + return new Promise((resolve, reject) => { // todo copypasta - app.data.storage.getGistFiles(app.ui.openSettingsDialog).then(({ filesInGist }) => { + app.data.storage.getGistFiles(reject).then(({ filesInGist }) => { const fileFound = Object.values(filesInGist).find( item => item.filename === 'resources.json' ); @@ -56,12 +56,9 @@ export var ResourcesEditor = function({ }) } // resource in local or at gist - this.initResourcesFile = () => { - return new Promise(resolve => { + this.initResourcesFile = (createVolatile = false) => { + return new Promise((resolve, reject) => { this.getVloatileResource().then(volatile => { - const volatileData = {}; - console.log('------------', {volatile, volatileData}) - if(volatile && volatile.content) { ToastWc.show({ type: 'success', @@ -71,48 +68,63 @@ export var ResourcesEditor = function({ console.log({volatile}) resolve(volatile); return - } else { - app.data.storage.getGistFiles(app.ui.openSettingsDialog).then(({ filesInGist }) => { - const fileFound = Object.values(filesInGist).find( - item => item.filename === 'resources.json' - ); - if (!fileFound) { - app.data.storage.editGistFile('resources.json', '{}').then(({file})=>{ - console.log({result}) - ToastWc.show({ - type: 'success', - content: `Created a resources.json file`, - time: 3000, - }); - this.resourcesFileUrl = file.raw_url; - this.setVolatileResource(file); - resolve(file); - }); - } else { - this.resourcesFileUrl = fileFound.raw_url; - return app.data.storage - .getContentOrRaw(fileFound.content, fileFound.raw_url) - .then(content => { - const fileWithContent = { ...fileFound, content }; - this.setVolatileResource(fileWithContent); - resolve(fileWithContent) - }); - } - }); } + if(createVolatile) { + const newFile = { + filename:'resources.json', + content: '{}', + } + this.setVolatileResource(newFile); + resolve(newFile); + return + } + app.data.storage.getGistFiles(reject).then(({ filesInGist }) => { + const fileFound = Object.values(filesInGist).find( + item => item.filename === 'resources.json' + ); + if (!fileFound) { + app.data.storage.editGistFile('resources.json', '{}').then(({file})=>{ + ToastWc.show({ + type: 'success', + content: `Created a resources.json file`, + time: 3000, + }); + this.resourcesFileUrl = file.raw_url; + this.setVolatileResource(file); + resolve(file); + }); + } else { + this.resourcesFileUrl = fileFound.raw_url; + return app.data.storage + .getContentOrRaw(fileFound.content, fileFound.raw_url) + .then(content => { + const fileWithContent = { ...fileFound, content }; + this.setVolatileResource(fileWithContent); + resolve(fileWithContent) + }); + } + }); }) }); }; this.onCommitResourceFiles = newContent => { this.isBusy('Uploading changes to gist...'); - app.data.storage.editGistFile('resources.json', newContent).then(() => { - ToastWc.show({ - type: 'info', - content: 'Saved resources on gist', - time: 1000, - }); - document.querySelector('resources-component').setIsNew(false) + app.data.storage.editGistFile('resources.json', newContent).then(({response, gistId}) => { + if(response.ok){ + ToastWc.show({ + type: 'info', + content: 'Saved resources on gist', + time: 1000, + }); + document.querySelector('resources-component').setIsNew(false); + } else { + ToastWc.show({ + type: 'error', + content: `Could not save changes to ${gistId}!`, + time: 3000, + }); + } this.isBusy('') }); }; @@ -164,6 +176,13 @@ export var ResourcesEditor = function({ time: 3000, }); this.isBusy(''); + }).catch(() => { + ToastWc.show({ + type: 'error', + content: `Could not Load changed from Gist file!`, + time: 3000, + }); + this.isBusy(''); }) } if(action === 'push'){ @@ -184,6 +203,24 @@ export var ResourcesEditor = function({ darkMode: app.settings.theme() === 'dracula', headerButtons: [{title: 'Load from gist', action: 'pull'}, {title: 'Save to gist', action: 'push'}] }) + }). catch(error=> { + ToastWc.show({ + type: 'error', + content: error, + time: 2000 + }).then(()=> { + ToastWc.show({ + type: 'info', + content: 'Yarn will store resources in cache for now', + time: 2000 + }) + }); + this.isBusy(''); + this.initResourcesFile(true); + // this.setVolatileResource({ + // filename:'resources.json', + // content: '', // todo not having raw_url can be used to detect if new file + // }); }); }, preConfirm: () => {