2021-10-17 18:18:25 +05:30
|
|
|
import * as React from "react";
|
|
|
|
|
import { getStats, scoreTask } from "./habiticaAPI"
|
|
|
|
|
import Statsview from "./Components/Statsview"
|
|
|
|
|
import Taskview from "./Components/Taskview"
|
|
|
|
|
|
|
|
|
|
let username = ""
|
|
|
|
|
let credentials = ""
|
|
|
|
|
|
|
|
|
|
class App extends React.Component<any,any> {
|
|
|
|
|
constructor(props: any) {
|
|
|
|
|
super(props)
|
2021-10-19 19:08:05 +05:30
|
|
|
username = this.props.plugin.settings.userID
|
|
|
|
|
credentials = this.props.plugin.settings.apiToken
|
2021-10-17 18:18:25 +05:30
|
|
|
this.state = {
|
|
|
|
|
isLoaded: false,
|
|
|
|
|
user_data: {
|
|
|
|
|
profile: {
|
|
|
|
|
name: "",
|
|
|
|
|
},
|
|
|
|
|
stats: {
|
|
|
|
|
hp: 0,
|
|
|
|
|
lvl: 0,
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-10-17 21:58:59 -07:00
|
|
|
todos: [],
|
|
|
|
|
dailys: [],
|
|
|
|
|
habits: [],
|
2021-10-17 18:18:25 +05:30
|
|
|
}
|
2021-10-19 10:54:06 +05:30
|
|
|
this.handleChangeTodos = this.handleChangeTodos.bind(this)
|
|
|
|
|
this.handleChangeDailys = this.handleChangeDailys.bind(this)
|
|
|
|
|
this.handleChangeHabits = this.handleChangeHabits.bind(this)
|
|
|
|
|
|
|
|
|
|
|
2021-10-17 18:18:25 +05:30
|
|
|
}
|
|
|
|
|
sendNotice(message: string){
|
|
|
|
|
this.props.plugin.displayNotice(message)
|
|
|
|
|
}
|
|
|
|
|
reloadData() {
|
|
|
|
|
getStats(username, credentials)
|
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(
|
|
|
|
|
result => {
|
|
|
|
|
if(result.success === false){
|
|
|
|
|
this.sendNotice("Login Failed, Please check credentials and try again!")
|
|
|
|
|
} else {
|
|
|
|
|
this.setState({
|
|
|
|
|
isLoaded: true,
|
|
|
|
|
user_data: result,
|
2021-10-17 21:58:59 -07:00
|
|
|
tasks: result.tasks,
|
2021-10-17 18:18:25 +05:30
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
isLoaded: true,
|
|
|
|
|
error
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.reloadData()
|
|
|
|
|
}
|
2021-10-19 10:54:06 +05:30
|
|
|
handleChangeTodos(event: any){
|
|
|
|
|
this.state.tasks.todos.forEach((element: any) => {
|
2021-10-17 18:18:25 +05:30
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-10-19 10:54:06 +05:30
|
|
|
}
|
|
|
|
|
handleChangeDailys(event: any){
|
|
|
|
|
this.state.tasks.dailys.forEach((element: any) => {
|
2021-10-17 21:58:59 -07:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-10-19 10:54:06 +05:30
|
|
|
}
|
|
|
|
|
handleChangeHabits(event: any){
|
|
|
|
|
const target_id = event.target.id.slice(4)
|
|
|
|
|
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")
|
2021-10-17 21:58:59 -07:00
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(
|
|
|
|
|
result => {
|
|
|
|
|
if(result.success) {
|
2021-10-19 10:54:06 +05:30
|
|
|
this.sendNotice("Plus!")
|
2021-10-17 21:58:59 -07:00
|
|
|
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-19 10:54:06 +05:30
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this.state.tasks.habits.forEach((element: any) => {
|
|
|
|
|
if(element.id == target_id){
|
|
|
|
|
scoreTask(username, credentials, target_id, "down")
|
2021-10-17 21:58:59 -07:00
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(
|
|
|
|
|
result => {
|
2021-10-19 10:54:06 +05:30
|
|
|
if(result.success) {
|
|
|
|
|
this.sendNotice("Minus :(")
|
2021-10-17 21:58:59 -07:00
|
|
|
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-19 10:54:06 +05:30
|
|
|
})
|
|
|
|
|
}
|
2021-10-17 18:18:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render(){
|
|
|
|
|
if(this.state.error)
|
|
|
|
|
return(<div className="loading">Loading....</div>)
|
|
|
|
|
else if(!this.state.isLoaded)
|
|
|
|
|
return <div className="loading">Loading....</div>
|
|
|
|
|
else {
|
|
|
|
|
return (<div className="plugin-root">
|
|
|
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
|
|
|
|
|
<Statsview user_data={this.state.user_data} />
|
2021-10-19 10:54:06 +05:30
|
|
|
<Taskview data={this.state.tasks} handleChangeTodos={this.handleChangeTodos} handleChangeDailys={this.handleChangeDailys} handleChangeHabits={this.handleChangeHabits}/>
|
2021-10-17 18:18:25 +05:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default App
|