2021-10-17 18:18:25 +05:30
|
|
|
import * as React from "react";
|
2021-10-23 16:41:11 +05:30
|
|
|
import { Notice } from "obsidian";
|
2021-11-28 22:47:54 +08:00
|
|
|
import { getStats, scoreTask, makeCronReq, costReward, addTask, deleteTask, updateTask } from "./habiticaAPI"
|
2021-10-17 18:18:25 +05:30
|
|
|
import Statsview from "./Components/Statsview"
|
|
|
|
|
import Taskview from "./Components/Taskview"
|
2021-11-21 22:13:19 +08:00
|
|
|
import "../i18n"
|
2021-10-17 18:18:25 +05:30
|
|
|
|
2021-11-18 14:16:20 +08:00
|
|
|
class App extends React.Component<any, any> {
|
2021-11-01 11:53:35 +05:30
|
|
|
private _username = "";
|
|
|
|
|
public get username() {
|
|
|
|
|
return this._username;
|
|
|
|
|
}
|
|
|
|
|
public set username(value) {
|
|
|
|
|
this._username = value;
|
|
|
|
|
}
|
|
|
|
|
private _credentials = "";
|
|
|
|
|
public get credentials() {
|
|
|
|
|
return this._credentials;
|
|
|
|
|
}
|
|
|
|
|
public set credentials(value) {
|
|
|
|
|
this._credentials = value;
|
|
|
|
|
}
|
2021-10-17 18:18:25 +05:30
|
|
|
constructor(props: any) {
|
|
|
|
|
super(props)
|
2021-10-28 21:53:15 -07:00
|
|
|
this.username = this.props.plugin.settings.userID
|
|
|
|
|
this.credentials = this.props.plugin.settings.apiToken
|
2021-10-17 18:18:25 +05:30
|
|
|
this.state = {
|
2021-10-30 22:34:41 +05:30
|
|
|
needCron: false,
|
2021-10-17 18:18:25 +05:30
|
|
|
isLoaded: false,
|
|
|
|
|
user_data: {
|
|
|
|
|
profile: {
|
|
|
|
|
name: "",
|
|
|
|
|
},
|
|
|
|
|
stats: {
|
|
|
|
|
hp: 0,
|
|
|
|
|
lvl: 0,
|
2021-10-30 22:34:41 +05:30
|
|
|
},
|
|
|
|
|
lastCron: "",
|
2021-10-17 18:18:25 +05:30
|
|
|
},
|
2021-10-17 21:58:59 -07:00
|
|
|
todos: [],
|
|
|
|
|
dailys: [],
|
|
|
|
|
habits: [],
|
2021-10-17 18:18:25 +05:30
|
|
|
}
|
2021-10-31 16:33:58 +05:30
|
|
|
this.handleChangeTodos = this.handleChangeTodos.bind(this);
|
|
|
|
|
this.handleChangeDailys = this.handleChangeDailys.bind(this);
|
|
|
|
|
this.handleChangeHabits = this.handleChangeHabits.bind(this);
|
2021-11-18 14:16:20 +08:00
|
|
|
this.handleChangeRewards = this.handleChangeRewards.bind(this);
|
2021-10-31 16:33:58 +05:30
|
|
|
this.runCron = this.runCron.bind(this);
|
2021-10-19 10:54:06 +05:30
|
|
|
|
2021-10-17 18:18:25 +05:30
|
|
|
}
|
2021-10-31 16:33:58 +05:30
|
|
|
CheckCron(lastCron: string) {
|
2021-11-16 18:29:31 +05:30
|
|
|
let cronDate = new Date(lastCron);
|
|
|
|
|
let now = new Date();
|
|
|
|
|
if (cronDate.getDate() != now.getDate() || (cronDate.getMonth() != now.getMonth() || cronDate.getFullYear() != now.getFullYear())) {
|
2021-11-18 14:16:20 +08:00
|
|
|
return (
|
2021-11-23 20:11:19 +08:00
|
|
|
<div className="cron">
|
|
|
|
|
<button onClick={this.runCron}>Refresh</button>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-10-30 22:34:41 +05:30
|
|
|
}
|
|
|
|
|
else {
|
2021-11-25 16:49:05 +08:00
|
|
|
return <div className="cron"></div>
|
2021-10-30 22:34:41 +05:30
|
|
|
};
|
|
|
|
|
}
|
2021-11-01 11:41:31 +05:30
|
|
|
async runCron() {
|
2021-10-31 16:33:58 +05:30
|
|
|
console.log("running cron");
|
2021-11-01 11:41:31 +05:30
|
|
|
try {
|
|
|
|
|
let response = await makeCronReq(this.username, this.credentials);
|
2021-10-30 22:34:41 +05:30
|
|
|
this.setState({
|
2021-10-31 16:39:47 +05:30
|
|
|
needCron: false,
|
2021-10-30 22:34:41 +05:30
|
|
|
})
|
2021-11-01 11:41:31 +05:30
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
new Notice("There was an error running the cron. Please try again later.");
|
|
|
|
|
}
|
2021-10-30 22:34:41 +05:30
|
|
|
this.reloadData();
|
2021-11-18 14:16:20 +08:00
|
|
|
}
|
2021-10-23 18:06:04 +05:30
|
|
|
async reloadData() {
|
2021-10-30 10:04:59 +05:30
|
|
|
try {
|
2021-11-18 14:16:20 +08:00
|
|
|
let response = await getStats(this.username, this.credentials);
|
|
|
|
|
let result = await response.json();
|
|
|
|
|
if (result.success === false) {
|
|
|
|
|
new Notice('Login Failed, Please check credentials and try again!');
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this.setState({
|
|
|
|
|
isLoaded: true,
|
|
|
|
|
user_data: result,
|
|
|
|
|
tasks: result.tasks,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2021-10-30 10:04:59 +05:30
|
|
|
console.log(e);
|
|
|
|
|
new Notice("API Error: Please check credentials")
|
2021-11-18 14:16:20 +08:00
|
|
|
}
|
2021-10-17 18:18:25 +05:30
|
|
|
}
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.reloadData()
|
|
|
|
|
}
|
2021-11-18 14:16:20 +08:00
|
|
|
|
|
|
|
|
async sendScore(id: string, score: string, message: string) {
|
|
|
|
|
try {
|
|
|
|
|
let response = await scoreTask(this.username, this.credentials, id, score);
|
|
|
|
|
let result = await response.json();
|
|
|
|
|
if (result.success === true) {
|
|
|
|
|
new Notice(message);
|
|
|
|
|
this.reloadData();
|
|
|
|
|
} else {
|
|
|
|
|
new Notice("Resyncing, please try again");
|
|
|
|
|
this.reloadData();
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
new Notice("API Error: Please check credentials")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendReward(id: string, score: string, message: string) {
|
2021-10-30 10:04:59 +05:30
|
|
|
try {
|
2021-11-18 14:16:20 +08:00
|
|
|
let response = await costReward(this.username, this.credentials, id, score);
|
|
|
|
|
let result = await response.json();
|
|
|
|
|
if (result.success === true) {
|
2021-10-30 10:04:59 +05:30
|
|
|
new Notice(message);
|
|
|
|
|
this.reloadData();
|
|
|
|
|
} else {
|
|
|
|
|
new Notice("Resyncing, please try again");
|
|
|
|
|
this.reloadData();
|
2021-10-23 18:06:04 +05:30
|
|
|
}
|
2021-11-18 14:16:20 +08:00
|
|
|
} catch (e) {
|
2021-10-30 10:04:59 +05:30
|
|
|
console.log(e);
|
|
|
|
|
new Notice("API Error: Please check credentials")
|
2021-11-18 14:16:20 +08:00
|
|
|
}
|
2021-10-23 18:06:04 +05:30
|
|
|
}
|
|
|
|
|
|
2021-11-25 16:49:05 +08:00
|
|
|
async sendAddTask(type: string, title: string, message: string) {
|
2021-11-23 20:11:19 +08:00
|
|
|
try {
|
|
|
|
|
let response = await addTask(this.username, this.credentials, title, type);
|
|
|
|
|
let result = await response.json();
|
|
|
|
|
if (result.success === true) {
|
|
|
|
|
new Notice(message);
|
|
|
|
|
this.reloadData();
|
|
|
|
|
} else {
|
|
|
|
|
new Notice("Resyncing, please try again");
|
|
|
|
|
this.reloadData();
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
new Notice("API Error: Please check credentials")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 23:00:47 +08:00
|
|
|
async sendDeleteTask(id: string, message: string) {
|
|
|
|
|
try {
|
|
|
|
|
let response = await deleteTask(this.username, this.credentials, id);
|
|
|
|
|
let result = await response.json();
|
|
|
|
|
if (result.success === true) {
|
|
|
|
|
new Notice(message);
|
|
|
|
|
this.reloadData();
|
|
|
|
|
} else {
|
|
|
|
|
new Notice("Resyncing, please try again");
|
|
|
|
|
this.reloadData();
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
new Notice("API Error: Please check credentials")
|
2021-11-25 16:49:05 +08:00
|
|
|
}
|
2021-11-23 23:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
2021-11-28 23:55:21 +08:00
|
|
|
async sendUpdateTask(id: string, type: string, message: string, title: string, notes: string) {
|
2021-11-28 22:47:54 +08:00
|
|
|
try {
|
|
|
|
|
let response = await updateTask(this.username, this.credentials, id, type, title, notes);
|
|
|
|
|
let result = await response.json();
|
|
|
|
|
if (result.success === true) {
|
|
|
|
|
new Notice(message);
|
|
|
|
|
this.reloadData();
|
|
|
|
|
} else {
|
|
|
|
|
new Notice("Resyncing, please try again");
|
|
|
|
|
this.reloadData();
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
new Notice("API Error: Please check credentials")
|
|
|
|
|
}
|
|
|
|
|
// console.log(id, type,title,notes)
|
2021-11-25 22:30:58 +08:00
|
|
|
}
|
|
|
|
|
|
2021-11-18 14:16:20 +08:00
|
|
|
handleChangeTodos(event: any) {
|
2021-11-25 16:49:05 +08:00
|
|
|
if (event.target.id == "add-todo") {
|
|
|
|
|
const title = event.target.name
|
|
|
|
|
this.sendAddTask("todo", title, "Add!")
|
|
|
|
|
} else {
|
|
|
|
|
this.state.tasks.todos.forEach((element: any) => {
|
|
|
|
|
if (element.id == event.target.id) {
|
|
|
|
|
if (event.type == "click") {
|
|
|
|
|
console.log(event)
|
|
|
|
|
if (event.target.innerText == 'clear') {
|
|
|
|
|
this.sendDeleteTask(event.target.id, "Deleted!")
|
|
|
|
|
}
|
2021-11-23 23:00:47 +08:00
|
|
|
} else {
|
2021-11-25 16:49:05 +08:00
|
|
|
if (!element.completed) {
|
|
|
|
|
this.sendScore(event.target.id, "up", "Checked!")
|
|
|
|
|
} else {
|
|
|
|
|
this.sendScore(event.target.id, "down", "Un-Checked!")
|
|
|
|
|
}
|
2021-11-23 23:00:47 +08:00
|
|
|
}
|
2021-10-17 18:18:25 +05:30
|
|
|
}
|
2021-11-25 16:49:05 +08:00
|
|
|
})
|
|
|
|
|
}
|
2021-10-19 10:54:06 +05:30
|
|
|
}
|
2021-11-18 14:16:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
handleChangeDailys(event: any) {
|
2021-11-25 16:49:05 +08:00
|
|
|
if (event.target.id == "add-daily") {
|
|
|
|
|
const title = event.target.name
|
|
|
|
|
this.sendAddTask("daily", title, "Add!")
|
|
|
|
|
} else {
|
|
|
|
|
this.state.tasks.dailys.forEach((element: any) => {
|
2021-11-18 14:16:20 +08:00
|
|
|
if (element.id == event.target.id) {
|
2021-11-25 16:49:05 +08:00
|
|
|
if (element.id == event.target.id) {
|
2021-11-28 23:55:21 +08:00
|
|
|
if (event.target.attributes.title.value == 'submit') {
|
|
|
|
|
const task_title = event.target.attributes['data-title'].value ? event.target.attributes['data-title'].value : element.text
|
|
|
|
|
const task_notes = event.target.attributes['data-notes'].value ? event.target.attributes['data-notes'].value : element.notes
|
|
|
|
|
this.sendUpdateTask(event.target.id, 'daily', "Update!", task_title, task_notes)
|
2021-11-28 22:47:54 +08:00
|
|
|
} else if (event.target.attributes.title.value == 'delete') {
|
2021-11-25 16:49:05 +08:00
|
|
|
this.sendDeleteTask(event.target.id, "Deleted!")
|
2021-11-28 23:55:21 +08:00
|
|
|
} else if (!element.completed) {
|
2021-11-25 22:30:58 +08:00
|
|
|
this.sendScore(event.target.id, "up", "Checked!")
|
2021-11-25 16:49:05 +08:00
|
|
|
} else {
|
|
|
|
|
this.sendScore(event.target.id, "down", "Un-Checked!")
|
|
|
|
|
}
|
2021-10-23 18:06:04 +05:30
|
|
|
}
|
2021-10-17 21:58:59 -07:00
|
|
|
}
|
2021-11-25 16:49:05 +08:00
|
|
|
})
|
|
|
|
|
}
|
2021-10-19 10:54:06 +05:30
|
|
|
}
|
2021-11-23 20:11:19 +08:00
|
|
|
|
2021-11-18 14:16:20 +08:00
|
|
|
handleChangeHabits(event: any) {
|
2021-11-25 16:49:05 +08:00
|
|
|
if (event.target.id == "add-habit") {
|
|
|
|
|
const title = event.target.name
|
|
|
|
|
this.sendAddTask("habit", title, "Add!")
|
2021-11-23 23:00:47 +08:00
|
|
|
} else {
|
2021-11-25 16:49:05 +08:00
|
|
|
if (event.target.innerText == 'clear') {
|
|
|
|
|
this.sendDeleteTask(event.target.id, "Deleted!")
|
|
|
|
|
} else {
|
|
|
|
|
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) {
|
|
|
|
|
this.sendScore(target_id, "up", "Plus!")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this.state.tasks.habits.forEach((element: any) => {
|
|
|
|
|
if (element.id == target_id) {
|
|
|
|
|
this.sendScore(target_id, "down", "Minus :(")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-11-23 23:00:47 +08:00
|
|
|
}
|
2021-10-19 10:54:06 +05:30
|
|
|
}
|
2021-10-17 18:18:25 +05:30
|
|
|
}
|
2021-11-25 16:49:05 +08:00
|
|
|
|
2021-11-18 14:16:20 +08:00
|
|
|
handleChangeRewards(event: any) {
|
2021-11-25 22:30:58 +08:00
|
|
|
console.log(event)
|
2021-11-25 16:49:05 +08:00
|
|
|
if (event.target.id == "add-reward") {
|
|
|
|
|
const title = event.target.name
|
|
|
|
|
this.sendAddTask("reward", title, "Add!")
|
|
|
|
|
} else {
|
|
|
|
|
const target_id = event.target.id
|
|
|
|
|
this.state.tasks.rewards.forEach((element: any) => {
|
|
|
|
|
if (element.id == target_id) {
|
|
|
|
|
if (event.target.innerText == 'clear') {
|
2021-11-23 23:00:47 +08:00
|
|
|
this.sendDeleteTask(event.target.id, "Deleted!")
|
2021-11-25 22:30:58 +08:00
|
|
|
} else if (event.target.innerText == 'create') {
|
2021-11-28 23:55:21 +08:00
|
|
|
this.sendUpdateTask(event.target.id, 'reward', "Edit!", "1", "1")
|
2021-11-25 22:30:58 +08:00
|
|
|
} else {
|
2021-11-23 23:00:47 +08:00
|
|
|
this.sendReward(target_id, "down", "Cost!")
|
|
|
|
|
}
|
2021-11-25 16:49:05 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-11-18 14:16:20 +08:00
|
|
|
}
|
|
|
|
|
render() {
|
2021-10-31 16:33:58 +05:30
|
|
|
let content = this.CheckCron(this.state.user_data.lastCron);
|
2021-11-18 14:16:20 +08:00
|
|
|
if (this.state.error)
|
|
|
|
|
return (<div className="loading">Loading....</div>)
|
|
|
|
|
else if (!this.state.isLoaded)
|
2021-10-17 18:18:25 +05:30
|
|
|
return <div className="loading">Loading....</div>
|
|
|
|
|
else {
|
|
|
|
|
return (<div className="plugin-root">
|
|
|
|
|
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
|
2021-11-23 20:11:19 +08:00
|
|
|
<Taskview data={this.state.tasks} handleChangeTodos={this.handleChangeTodos} handleChangeDailys={this.handleChangeDailys} handleChangeHabits={this.handleChangeHabits} handleChangeRewards={this.handleChangeRewards} />
|
2021-10-31 16:33:58 +05:30
|
|
|
{content}
|
2021-11-18 14:16:20 +08:00
|
|
|
<Statsview user_data={this.state.user_data} />
|
|
|
|
|
</div>
|
2021-10-17 18:18:25 +05:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default App
|