From bf0e97a926c6cc36e4ec733b26dd58e8e02811fd Mon Sep 17 00:00:00 2001 From: Todor Imreorov Date: Mon, 15 Apr 2024 21:01:42 +0100 Subject: [PATCH] move more code to storage api --- src/js/classes/data.js | 2 +- src/js/classes/storage.js | 50 +++++++++++++++++++++---------------- src/public/plugins/index.js | 2 +- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/js/classes/data.js b/src/js/classes/data.js index f5d686e0..ee9fca97 100644 --- a/src/js/classes/data.js +++ b/src/js/classes/data.js @@ -1172,7 +1172,7 @@ export const data = { console.log(gistFiles); data.promptFileNameAndFormat(({ editingName, yarnData }) => { data.editingName(editingName); - gists.edit(gists.file, editingName, yarnData); + gists.editGist(gists.file, editingName, yarnData); Swal.fire( 'Saved!', `The Yarn has been saved to gist ${gists.file}`, diff --git a/src/js/classes/storage.js b/src/js/classes/storage.js index 7d9c3eff..297f1b8a 100644 --- a/src/js/classes/storage.js +++ b/src/js/classes/storage.js @@ -1,20 +1,19 @@ -export const StorageJs = (type= 'gist', credentials) => { +export const StorageJs = (type = 'gist', credentials) => { if (type === 'gist') { return { - testing: 123, - setTesting: function(newVal){ - this.testing = newVal + lastStorageHost: 'GIST', // or LOCAL + setLastStorageHost: function(newHost) { + this.lastStorageHost = newHost; }, token: credentials.token, file: credentials.file, filesInGist: {}, - getFilesInGist: function(fileKey){ - if(!fileKey) return this.filesInGist; - if (fileKey in this.filesInGist) return this.filesInGist[fileKey] - console.error(`${fileKey} not found in gist`, this.filesInGist) + getFilesInGist: function(fileKey) { + if (!fileKey) return this.filesInGist; + if (fileKey in this.filesInGist) return this.filesInGist[fileKey]; + console.error(`${fileKey} not found in gist`, this.filesInGist); }, - test: function(){ console.log("CRED",this.file); return this.testing}, - get: function(gistId){ + getGist: function(gistId) { return fetch('https://api.github.com/gists/' + gistId, { method: 'GET', headers: { @@ -34,14 +33,20 @@ export const StorageJs = (type= 'gist', credentials) => { Object.keys(this.filesInGist).forEach(key => { inputOptions[key] = key; }); - return { body: content, filesInGist: this.filesInGist, inputOptions, fileList: Object.keys(this.filesInGist) }; + this.setLastStorageHost('GIST'); + return { + body: content, + filesInGist: this.filesInGist, + inputOptions, + fileList: Object.keys(this.filesInGist), + }; }); }, - hasGistSettings: function(){ - return this.file && this.file.length > 0 + hasGistSettings: function() { + return this.file && this.file.length > 0; }, - getGistFile: function(){ - return this.get(this.file); + getGistFile: function() { + return this.getGist(this.file); }, getContentOrRaw: function(content, rawUrl) { // sometimes github comes back empty handed for content, but has raw_url @@ -57,7 +62,7 @@ export const StorageJs = (type= 'gist', credentials) => { } }); }, - edit: function(gistId, fileName, content) { + editGist: function(gistId, fileName, content) { console.log({ gistId, fileName, content, credentials }); return fetch('https://api.github.com/gists/' + gistId, { method: 'POST', @@ -71,11 +76,14 @@ export const StorageJs = (type= 'gist', credentials) => { public: false, files: { [fileName]: { content } }, }), - }).then(res => res.json()); + }).then(res => { + this.setLastStorageHost('GIST'); + return res.json(); + }); + }, + editGistFile: function(fileName, content) { + return this.editGist(this.file, fileName, content); }, - editGistFile: function(fileName, content){ - return this.edit(this.file, fileName, content) - } }; } else if (type === 'github') { // todo implement @@ -113,4 +121,4 @@ export const StorageJs = (type= 'gist', credentials) => { // return { getFile, setFile, credentials, setCredentials }; } return { getFile: () => {}, setFile: () => {}, credentials, setCredentials }; -} \ No newline at end of file +}; diff --git a/src/public/plugins/index.js b/src/public/plugins/index.js index 50406bcc..7a55db01 100644 --- a/src/public/plugins/index.js +++ b/src/public/plugins/index.js @@ -266,7 +266,7 @@ export var Plugins = function(app) { // register plugins stored on a gist - todo cache all this if (app.settings.gistPluginsFile() !== null) { - app.gists.get(app.settings.gistPluginsFile()).then(({fileList}) => { + app.gists.getGist(app.settings.gistPluginsFile()).then(({fileList}) => { console.log({ fileList }); fileList.forEach(gistFile => { if (gistFile.language === 'JavaScript') {