From a60eade0aeba466fa576426207dcf5bddf2c329e Mon Sep 17 00:00:00 2001 From: Leoh Date: Fri, 15 Oct 2021 09:41:43 +0530 Subject: [PATCH] Checkboxes sync enabled --- view/App.tsx | 64 +++++++++++++++++++++++++++------------------ view/TodoItem.tsx | 6 ++--- view/habiticaAPI.ts | 30 ++++++++++----------- 3 files changed, 56 insertions(+), 44 deletions(-) diff --git a/view/App.tsx b/view/App.tsx index cc7c1b4..ed6fb2c 100644 --- a/view/App.tsx +++ b/view/App.tsx @@ -1,5 +1,6 @@ +import { Tasks } from "obsidian"; import * as React from "react"; -import { getTasks, getStats } from "./habiticaAPI" +import { getStats, scoreTask } from "./habiticaAPI" import TodoItem from "./TodoItem" let username = "" @@ -21,32 +22,17 @@ class App extends React.Component { lvl: 0, } }, - tasks: [] //gave an error if the the tasks thing was string so better keep it an array for .map to work :) + tasks: [] } + this.handleChange = this.handleChange.bind(this) } - componentDidMount() { - // getTasks(username, credentials) - // .then(res => res.json()) - // .then( - // result => { - // this.setState({ - // isLoaded: true, - // tasks: result.data - // }) - // }, - // (error) => { - // this.setState({ - // isLoaded: true, - // error - // }) - // } - // ) - + reloadData() { getStats(username, credentials) .then(res => res.json()) .then( result => { - console.log(result) //yup this prints out correctly! since the promise is handled by .then + console.log(result) + console.log("data reloaded") this.setState({ isLoaded: true, user_data: result, @@ -60,8 +46,38 @@ class App extends React.Component { }) } ) - } + 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 => { + console.log("Checked!") + console.log(result) + this.reloadData() + } + ) + } else { + scoreTask(username, credentials, event.target.id, "down") + .then(res => res.json()) + .then( + result => { + console.log("unchecked!") + console.log(result) + this.reloadData() + } + ) + } + } + }) + } + render(){ const { error, isLoaded, tasks } = this.state; const user_data = this.state.user_data @@ -71,9 +87,7 @@ class App extends React.Component { return
Loading...
; } else { const listItems = tasks.map((tasks: any) => -
- -
+ ); return (

{user_data.profile.name}

{"\n"} diff --git a/view/TodoItem.tsx b/view/TodoItem.tsx index 6566dcc..bf681c7 100644 --- a/view/TodoItem.tsx +++ b/view/TodoItem.tsx @@ -1,9 +1,9 @@ import * as React from "react"; -function TodoItem(props: any) { +function TodoItem(props: any) { return ( -
- +
+

{props.task.text}

) diff --git a/view/habiticaAPI.ts b/view/habiticaAPI.ts index e0ab591..70d7667 100644 --- a/view/habiticaAPI.ts +++ b/view/habiticaAPI.ts @@ -1,20 +1,5 @@ // import fetch from "node-fetch"; - -export async function getTasks(username: string, credentials: string){ - const url = "https://habitica.com/api/v3/tasks/user?type=todos" - const response = fetch(url, { - method: 'GET', - headers: { - "Content-Type": "application/json", - "x-client": username.concat("-testAPI"), - "x-api-user": username, - "x-api-key": credentials, - }, - }) - return (response) -} - export async function getStats(username: string, credentials: string){ const url = "https://habitica.com/export/userdata.json" const response = fetch(url, { @@ -26,6 +11,19 @@ export async function getStats(username: string, credentials: string){ "x-api-key": credentials, }, }) - console.log("stats") //can't print stats from here since the response is still an unresolved promise return (response) +} + +export async function scoreTask(username: string, credentials: string, taskID: string, direction: string) { + const url = "https://habitica.com/api/v3/tasks/".concat(taskID).concat("/score/").concat(direction) + const response = fetch(url, { + method: 'POST', + headers: { + "Content-Type": "application/json", + "x-client": username.concat("-testAPI"), + "x-api-user": username, + "x-api-key": credentials, + } + }) + return(response) } \ No newline at end of file