Removed repeated code and added reusability for scoring tasks

This commit is contained in:
Leoh 2021-10-23 18:06:04 +05:30
parent 977cacb8f6
commit a8a4e32f7b
2 changed files with 37 additions and 112 deletions

View file

@ -36,10 +36,9 @@ class App extends React.Component<any,any> {
sendNotice(message: string){ sendNotice(message: string){
new Notice(message) new Notice(message)
} }
reloadData() { async reloadData() {
getStats(username, credentials) const result = (await getStats(username, credentials)).json()
.then(res => res.json()) result.then(
.then(
result => { result => {
if(result.success === false){ if(result.success === false){
this.sendNotice("Login Failed, Please check credentials and try again!") this.sendNotice("Login Failed, Please check credentials and try again!")
@ -62,88 +61,46 @@ class App extends React.Component<any,any> {
componentDidMount() { componentDidMount() {
this.reloadData() 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){ handleChangeTodos(event: any){
this.state.tasks.todos.forEach((element: any) => { this.state.tasks.todos.forEach((element: any) => {
if(element.id == event.target.id){ if(element.id == event.target.id){
if(!element.completed){ if(!element.completed){
scoreTask(username, credentials, event.target.id, "up") this.sendScore(event.target.id,"up", "Checked!")
.then(res => res.json())
.then(
result => {
if(result.success) {
this.sendNotice("Checked!")
this.reloadData()
} else { } else {
this.sendNotice("Resyncing, please try again") this.sendScore(event.target.id,"down", "Un-Checked!")
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)
}
)
} }
} }
}) })
} }
handleChangeDailys(event: any){ handleChangeDailys(event: any){
this.state.tasks.dailys.forEach((element: any) => { this.state.tasks.dailys.forEach((element: any) => {
if(element.id == event.target.id){
if(element.id == event.target.id){ if(element.id == event.target.id){
if(!element.completed){ if(!element.completed){
scoreTask(username, credentials, event.target.id, "up") this.sendScore(event.target.id,"up", "Checked!")
.then(res => res.json())
.then(
result => {
if(result.success) {
this.sendNotice("Checked!")
this.reloadData()
} else { } else {
this.sendNotice("Resyncing, please try again") this.sendScore(event.target.id,"down", "Un-Checked!")
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)
}
)
} }
} }
}) })
@ -153,46 +110,14 @@ class App extends React.Component<any,any> {
if(event.target.id.slice(0,4) == "plus"){ if(event.target.id.slice(0,4) == "plus"){
this.state.tasks.habits.forEach((element: any) => { this.state.tasks.habits.forEach((element: any) => {
if(element.id == target_id){ if(element.id == target_id){
scoreTask(username, credentials, target_id, "up") this.sendScore(target_id,"up", "Plus!")
.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)
}
)
} }
}) })
} }
else { else {
this.state.tasks.habits.forEach((element: any) => { this.state.tasks.habits.forEach((element: any) => {
if(element.id == target_id){ if(element.id == target_id){
scoreTask(username, credentials, target_id, "down") this.sendScore(target_id,"down", "Minus :(")
.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)
}
)
} }
}) })
} }

View file

@ -2,7 +2,7 @@
export async function getStats(username: string, credentials: string){ export async function getStats(username: string, credentials: string){
const url = "https://habitica.com/export/userdata.json" const url = "https://habitica.com/export/userdata.json"
const response = fetch(url, { const response = await fetch(url, {
method: 'GET', method: 'GET',
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -11,7 +11,7 @@ export async function getStats(username: string, credentials: string){
"x-api-key": credentials, "x-api-key": credentials,
}, },
}) })
return (response) return (await response)
} }
export async function scoreTask(username: string, credentials: string, taskID: string, direction: string) { export async function scoreTask(username: string, credentials: string, taskID: string, direction: string) {