-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from cxumol/refactor
Refactor: misc decoupling and comments
- Loading branch information
Showing
6 changed files
with
159 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,66 @@ | ||
const myheaders = { | ||
Origin: window.location.origin, | ||
'Content-Type': 'text/plain' | ||
}; | ||
|
||
async function getDefaultData(){ | ||
const response = await fetch(`/data_default.json`); | ||
if (!response.ok){ | ||
throw new Error(response); | ||
}else{ | ||
return await response.json(); | ||
} | ||
} | ||
|
||
function dataKeysUpdate(template, toAlign){ | ||
for (const k of Object.keys(template)) { | ||
if (!toAlign.hasOwnProperty(k)) { | ||
console.log(`data field updated implicitly, updated key:${k}`); | ||
toAlign[k] = template[k]; | ||
} | ||
} | ||
return toAlign; | ||
} | ||
|
||
class DB { | ||
constructor(cf_workers, username){ | ||
this.cf_workers = cf_workers; | ||
this.username = username; | ||
this.needToken = null; | ||
} | ||
async getData() { | ||
const defaultData = getDefaultData(); | ||
const response = await fetch(`https://${this.cf_workers}/get/${this.username}`, { headers: myheaders }); | ||
if (!response.ok) { | ||
// use default data | ||
return getDefaultData(); | ||
} | ||
if (response.headers.get('x-need-token')) { | ||
this.needToken = true; | ||
console.log('A token is required to edit this page'); | ||
} else { | ||
this.needToken = false; | ||
} | ||
const result = await response.json(); | ||
const cloudData = dataKeysUpdate(defaultData, result); | ||
return cloudData; | ||
} | ||
|
||
async uploadData(myDataStr) { | ||
|
||
const response = await fetch(`https://${this.cf_workers}/set/${this.username}`, { | ||
headers: myheaders, | ||
method: 'POST', | ||
body: myDataStr | ||
}) | ||
if (!response.ok) { | ||
response.text().then((msg) => alert(`unsuccessful data uploading \nReason: ${msg}`)); | ||
return {uploadingState: 'bad'} | ||
} | ||
return {uploadingState: 'ok'}; | ||
} | ||
} | ||
|
||
|
||
export {DB}; | ||
const myheaders = { | ||
Origin: window.location.origin, | ||
'Content-Type': 'text/plain' | ||
}; | ||
|
||
async function getDefaultData() { | ||
const response = await fetch(`/data_default.json`); | ||
if (!response.ok) { | ||
throw new Error(response); | ||
} else { | ||
return await response.json(); | ||
} | ||
} | ||
|
||
function dataKeysUpdate(template, toAlign) { | ||
for (const k of Object.keys(template)) { | ||
if (!toAlign.hasOwnProperty(k)) { | ||
console.log(`data field updated implicitly, updated key:${k}`); | ||
toAlign[k] = template[k]; | ||
} | ||
} | ||
return toAlign; | ||
} | ||
|
||
class DB { | ||
constructor(cf_workers, username) { | ||
this.cf_workers = cf_workers; | ||
this.username = username; | ||
this.needToken = null; | ||
} | ||
|
||
async getData() { | ||
const defaultData = getDefaultData(); | ||
const response = await fetch(`https://${this.cf_workers}/get/${this.username}`, { | ||
headers: myheaders | ||
}); | ||
if (!response.ok) { | ||
// use default data | ||
return getDefaultData(); | ||
} | ||
if (response.headers.get('x-need-token')) { | ||
this.needToken = true; | ||
console.log('A token is required to edit this page'); | ||
} else { | ||
this.needToken = false; | ||
} | ||
const result = await response.json(); | ||
const cloudData = dataKeysUpdate(defaultData, result); | ||
return cloudData; | ||
} | ||
|
||
async uploadData(myDataStr) { | ||
const response = await fetch(`https://${this.cf_workers}/set/${this.username}`, { | ||
headers: myheaders, | ||
method: 'POST', | ||
body: myDataStr | ||
}); | ||
if (!response.ok) { | ||
response.text().then((msg) => alert(`unsuccessful data uploading \nReason: ${msg}`)); | ||
return { uploadingState: 'bad' }; | ||
} | ||
return { uploadingState: 'ok' }; | ||
} | ||
} | ||
|
||
export { DB }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function newRandomID() { | ||
let result = Math.random().toString(36).slice(-8); | ||
while (result.length < 8) { | ||
result = Math.random().toString(36).slice(-8); | ||
} | ||
return result; | ||
} | ||
export { newRandomID }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.