habitica-sync/view/App.tsx

122 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-10-15 09:41:43 +05:30
import { Tasks } from "obsidian";
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-10 21:08:13 -07:00
import TodoItem from "./TodoItem"
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,
user_data: {
profile: {
name: "",
},
stats: {
hp: 0,
lvl: 0,
}
},
2021-10-15 09:41:43 +05:30
tasks: []
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
}
sendNotice(message: string){
this.props.plugin.displayNotice(message)
}
2021-10-15 09:41:43 +05:30
reloadData() {
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,
user_data: result,
tasks: result.tasks.todos
})
2021-10-08 10:23:06 +05:30
},
(error) => {
this.setState({
isLoaded: true,
error
})
}
)
2021-10-08 10:23:06 +05:30
}
2021-10-15 09:41:43 +05:30
componentDidMount() {
this.reloadData()
}
handleChange(event: any){
this.state.tasks.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!")
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 => {
if(result.success){
this.sendNotice("unchecked!")
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(){
const { error, isLoaded, tasks } = this.state;
const user_data = this.state.user_data
2021-10-08 10:23:06 +05:30
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
const listItems = tasks.map((tasks: any) =>
2021-10-15 09:41:43 +05:30
<TodoItem key={tasks.id} id={tasks.id} task={tasks} completed={tasks.completed} onChange={this.handleChange}/>
2021-10-08 10:23:06 +05:30
);
return (<div>
<h3>{user_data.profile.name}</h3>{"\n"}
<div>HP: {user_data.stats.hp}</div><div> XP: {user_data.stats.lvl}</div>{"\n"}
2021-10-08 10:23:06 +05:30
<ul>{listItems}</ul>
</div>
2021-10-08 10:23:06 +05:30
);
}
}
}
export default App