Removed repeated code and added reusability for scoring tasks
This commit is contained in:
parent
977cacb8f6
commit
a8a4e32f7b
2 changed files with 37 additions and 112 deletions
145
src/view/App.tsx
145
src/view/App.tsx
|
|
@ -36,11 +36,10 @@ 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!")
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -57,50 +56,38 @@ class App extends React.Component<any,any> {
|
||||||
error
|
error
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
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 {
|
|
||||||
this.sendNotice("Resyncing, please try again")
|
|
||||||
this.reloadData()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
(error) => {
|
|
||||||
this.sendNotice("API Error: Please Check crendentials and try again")
|
|
||||||
console.log(error)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
scoreTask(username, credentials, event.target.id, "down")
|
this.sendScore(event.target.id,"down", "Un-Checked!")
|
||||||
.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)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -108,42 +95,12 @@ class App extends React.Component<any,any> {
|
||||||
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.completed){
|
if(element.id == event.target.id){
|
||||||
scoreTask(username, credentials, event.target.id, "up")
|
if(!element.completed){
|
||||||
.then(res => res.json())
|
this.sendScore(event.target.id,"up", "Checked!")
|
||||||
.then(
|
} else {
|
||||||
result => {
|
this.sendScore(event.target.id,"down", "Un-Checked!")
|
||||||
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)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -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)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue