2021-10-08 10:23:06 +05:30
|
|
|
import * as React from "react";
|
2021-10-15 09:41:43 +05:30
|
|
|
import { getStats, scoreTask } from "./habiticaAPI"
|
2021-10-15 17:33:32 +05:30
|
|
|
import Statsview from "./Components/Statsview"
|
|
|
|
|
import Taskview from "./Components/Taskview"
|
2021-10-08 10:23:06 +05:30
|
|
|
|
2021-10-10 21:08:13 -07:00
|
|
|
let username = ""
|
|
|
|
|
let credentials = ""
|
2021-10-08 10:23:06 +05:30
|
|
|
|
|
|
|
|
class App extends React.Component<any,any> {
|
|
|
|
|
constructor(props: any) {
|
|
|
|
|
super(props)
|
2021-10-10 21:08:13 -07:00
|
|
|
username = this.props.username
|
|
|
|
|
credentials = this.props.apiToken
|
2021-10-08 10:23:06 +05:30
|
|
|
this.state = {
|
|
|
|
|
isLoaded: false,
|
2021-10-13 14:10:54 +05:30
|
|
|
user_data: {
|
|
|
|
|
profile: {
|
|
|
|
|
name: "",
|
|
|
|
|
},
|
|
|
|
|
stats: {
|
|
|
|
|
hp: 0,
|
|
|
|
|
lvl: 0,
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-10-15 17:33:32 +05:30
|
|
|
todos: []
|
2021-10-08 10:23:06 +05:30
|
|
|
}
|
2021-10-15 09:41:43 +05:30
|
|
|
this.handleChange = this.handleChange.bind(this)
|
2021-10-08 10:23:06 +05:30
|
|
|
}
|
2021-10-15 10:30:25 +05:30
|
|
|
sendNotice(message: string){
|
|
|
|
|
this.props.plugin.displayNotice(message)
|
|
|
|
|
}
|
2021-10-15 09:41:43 +05:30
|
|
|
reloadData() {
|
2021-10-13 14:10:54 +05:30
|
|
|
getStats(username, credentials)
|
2021-10-08 10:23:06 +05:30
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(
|
|
|
|
|
result => {
|
2021-10-15 09:41:43 +05:30
|
|
|
console.log(result)
|
|
|
|
|
console.log("data reloaded")
|
2021-10-08 10:23:06 +05:30
|
|
|
this.setState({
|
|
|
|
|
isLoaded: true,
|
2021-10-13 14:10:54 +05:30
|
|
|
user_data: result,
|
2021-10-15 17:33:32 +05:30
|
|
|
todos: result.tasks.todos
|
2021-10-13 14:10:54 +05:30
|
|
|
})
|
2021-10-08 10:23:06 +05:30
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
isLoaded: true,
|
|
|
|
|
error
|
2021-10-12 19:48:33 -07:00
|
|
|
})
|
|
|
|
|
}
|
2021-10-13 14:10:54 +05:30
|
|
|
)
|
2021-10-08 10:23:06 +05:30
|
|
|
}
|
2021-10-15 09:41:43 +05:30
|
|
|
componentDidMount() {
|
|
|
|
|
this.reloadData()
|
|
|
|
|
}
|
|
|
|
|
handleChange(event: any){
|
2021-10-15 17:33:32 +05:30
|
|
|
this.state.todos.forEach((element: any) => {
|
2021-10-15 09:41:43 +05:30
|
|
|
if(element.id == event.target.id){
|
|
|
|
|
if(!element.completed){
|
|
|
|
|
scoreTask(username, credentials, event.target.id, "up")
|
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(
|
|
|
|
|
result => {
|
2021-10-15 10:30:25 +05:30
|
|
|
if(result.success) {
|
|
|
|
|
this.sendNotice("Checked!")
|
|
|
|
|
console.log(result)
|
|
|
|
|
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)
|
2021-10-15 09:41:43 +05:30
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
scoreTask(username, credentials, event.target.id, "down")
|
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(
|
|
|
|
|
result => {
|
2021-10-15 10:30:25 +05:30
|
|
|
if(result.success){
|
2021-10-15 17:42:39 +05:30
|
|
|
this.sendNotice("Un-checked!")
|
2021-10-15 10:30:25 +05:30
|
|
|
console.log(result)
|
|
|
|
|
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)
|
2021-10-15 09:41:43 +05:30
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 10:23:06 +05:30
|
|
|
render(){
|
2021-10-15 17:38:57 +05:30
|
|
|
if(this.state.error)
|
|
|
|
|
return(<div className="loading">Loading....</div>)
|
|
|
|
|
else if(!this.state.isLoaded)
|
2021-10-15 17:33:32 +05:30
|
|
|
return <div className="loading">Loading....</div>
|
|
|
|
|
else {
|
2021-10-16 23:33:38 +05:30
|
|
|
return (<div className="plugin-root">
|
2021-10-17 00:00:39 +05:30
|
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
|
2021-10-15 17:33:32 +05:30
|
|
|
<Statsview user_data={this.state.user_data} />
|
|
|
|
|
<Taskview todos={this.state.todos} onChange={this.handleChange} />
|
2021-10-13 14:10:54 +05:30
|
|
|
</div>
|
2021-10-08 10:23:06 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default App
|