Skip to content

Commit

Permalink
move more code to storage api
Browse files Browse the repository at this point in the history
  • Loading branch information
blurymind committed Apr 15, 2024
1 parent 08358a2 commit bf0e97a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/js/classes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
50 changes: 29 additions & 21 deletions src/js/classes/storage.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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
Expand All @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -113,4 +121,4 @@ export const StorageJs = (type= 'gist', credentials) => {
// return { getFile, setFile, credentials, setCredentials };
}
return { getFile: () => {}, setFile: () => {}, credentials, setCredentials };
}
};
2 changes: 1 addition & 1 deletion src/public/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit bf0e97a

Please sign in to comment.