diff --git a/src/view/App.tsx b/src/view/App.tsx index 9313487..fd8bdff 100644 --- a/src/view/App.tsx +++ b/src/view/App.tsx @@ -36,11 +36,10 @@ class App extends React.Component { sendNotice(message: string){ new Notice(message) } - reloadData() { - getStats(username, credentials) - .then(res => res.json()) - .then( - result => { + async reloadData() { + const result = (await getStats(username, credentials)).json() + result.then( + result => { if(result.success === false){ this.sendNotice("Login Failed, Please check credentials and try again!") } else { @@ -57,50 +56,38 @@ class App extends React.Component { error }) } - ) + ) } componentDidMount() { this.reloadData() } + + async sendScore(id:string , score: string, message: string){ + const result = (await scoreTask(username, credentials, id, score)).json() + result.then( + result => { + if(result.success) { + this.sendNotice(message) + this.reloadData() + } else { + this.sendNotice("Resyncing, please try again") + this.reloadData() + } + }, + (error) => { + this.sendNotice("API Error: Please Check crendentials and try again") + console.log(error) + } + ) + } + handleChangeTodos(event: any){ this.state.tasks.todos.forEach((element: any) => { if(element.id == event.target.id){ if(!element.completed){ - scoreTask(username, credentials, event.target.id, "up") - .then(res => res.json()) - .then( - result => { - if(result.success) { - this.sendNotice("Checked!") - this.reloadData() - } else { - this.sendNotice("Resyncing, please try again") - this.reloadData() - } - }, - (error) => { - this.sendNotice("API Error: Please Check crendentials and try again") - console.log(error) - } - ) + this.sendScore(event.target.id,"up", "Checked!") } else { - scoreTask(username, credentials, event.target.id, "down") - .then(res => res.json()) - .then( - result => { - if(result.success){ - this.sendNotice("Un-checked!") - this.reloadData() - } else { - this.sendNotice("Resyncing, please try again") - this.reloadData() - } - }, - (error) => { - this.sendNotice("API Error: Please Check crendentials and try again") - console.log(error) - } - ) + this.sendScore(event.target.id,"down", "Un-Checked!") } } }) @@ -108,42 +95,12 @@ class App extends React.Component { handleChangeDailys(event: any){ this.state.tasks.dailys.forEach((element: any) => { if(element.id == event.target.id){ - if(!element.completed){ - scoreTask(username, credentials, event.target.id, "up") - .then(res => res.json()) - .then( - result => { - if(result.success) { - this.sendNotice("Checked!") - this.reloadData() - } else { - this.sendNotice("Resyncing, please try again") - this.reloadData() - } - }, - (error) => { - this.sendNotice("API Error: Please Check crendentials and try again") - console.log(error) - } - ) - } else { - scoreTask(username, credentials, event.target.id, "down") - .then(res => res.json()) - .then( - result => { - if(result.success){ - this.sendNotice("Un-checked!") - this.reloadData() - } else { - this.sendNotice("Resyncing, please try again") - this.reloadData() - } - }, - (error) => { - this.sendNotice("API Error: Please Check crendentials and try again") - console.log(error) - } - ) + if(element.id == event.target.id){ + if(!element.completed){ + this.sendScore(event.target.id,"up", "Checked!") + } else { + this.sendScore(event.target.id,"down", "Un-Checked!") + } } } }) @@ -153,46 +110,14 @@ class App extends React.Component { if(event.target.id.slice(0,4) == "plus"){ this.state.tasks.habits.forEach((element: any) => { if(element.id == target_id){ - scoreTask(username, credentials, target_id, "up") - .then(res => res.json()) - .then( - result => { - if(result.success) { - this.sendNotice("Plus!") - this.reloadData() - } else { - this.sendNotice("Resyncing, please try again") - this.reloadData() - } - }, - (error) => { - this.sendNotice("API Error: Please Check crendentials and try again") - console.log(error) - } - ) + this.sendScore(target_id,"up", "Plus!") } }) } else { this.state.tasks.habits.forEach((element: any) => { if(element.id == target_id){ - scoreTask(username, credentials, target_id, "down") - .then(res => res.json()) - .then( - result => { - if(result.success) { - this.sendNotice("Minus :(") - this.reloadData() - } else { - this.sendNotice("Resyncing, please try again") - this.reloadData() - } - }, - (error) => { - this.sendNotice("API Error: Please Check crendentials and try again") - console.log(error) - } - ) + this.sendScore(target_id,"down", "Minus :(") } }) } diff --git a/src/view/habiticaAPI.ts b/src/view/habiticaAPI.ts index 70d7667..36f2b25 100644 --- a/src/view/habiticaAPI.ts +++ b/src/view/habiticaAPI.ts @@ -2,7 +2,7 @@ export async function getStats(username: string, credentials: string){ const url = "https://habitica.com/export/userdata.json" - const response = fetch(url, { + const response = await fetch(url, { method: 'GET', headers: { "Content-Type": "application/json", @@ -11,7 +11,7 @@ export async function getStats(username: string, credentials: string){ "x-api-key": credentials, }, }) - return (response) + return (await response) } export async function scoreTask(username: string, credentials: string, taskID: string, direction: string) {