From f77466b8a207fd4c63112f0e345e5c25697bbf34 Mon Sep 17 00:00:00 2001 From: ransurf Date: Tue, 12 Oct 2021 19:48:33 -0700 Subject: [PATCH 1/2] cleaned more date code, setup testing for stats, renamed plugin and info cleaned more date code, setup testing for stats, renamed plugin and info --- main.ts | 4 +--- manifest.json | 8 ++++---- package.json | 4 ++-- view/App.tsx | 25 ++++++++++++++++++++++--- view/habiticaAPI.ts | 16 ++++++++++++++++ 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/main.ts b/main.ts index 16aa5b7..9d871c8 100644 --- a/main.ts +++ b/main.ts @@ -3,12 +3,10 @@ import { ExampleSettingsTab } from "./settings"; import { ExampleView, VIEW_TYPE_EXAMPLE} from "./view" interface ExamplePluginSettings { - dateFormat: string userID: string apiToken: string } const DEFAULT_SETTINGS: Partial = { - dateFormat: "YYYY-MM-DD", userID: "", apiToken: "" } @@ -23,7 +21,7 @@ export default class ExamplePlugin extends Plugin { VIEW_TYPE_EXAMPLE, (leaf) => (this.view = new ExampleView(leaf, this)) ); - this.addRibbonIcon("dice", "Activate view", () => { //activate view + this.addRibbonIcon("dice", "Open Habitica Pane", () => { //activate view this.activateView(); }); } diff --git a/manifest.json b/manifest.json index 4915397..c29e0fa 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,10 @@ { - "id": "test-plugin", - "name": "Test Plugin", + "id": "obsidian-habitica-integration", + "name": "AAA Obsidian Habitica Integration", "version": "0.0.1", "minAppVersion": "0.9.12", - "description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.", - "author": "Leoh", + "description": "This plugin helps integrate Habitica user tasks and stats into Obsidian", + "author": "Leoh and Ran", "authorUrl": "", "isDesktopOnly": false } diff --git a/package.json b/package.json index 27eb3d7..8c9260e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "test-plugin", + "name": "obsidian-habitica-integration", "version": "0.12.0", - "description": "This is a sample plugin for Obsidian (https://obsidian.md)", + "description": "This plugin allows for Habitica integration into Obsidian", "main": "main.js", "scripts": { "dev": "rollup --config rollup.config.js -w", diff --git a/view/App.tsx b/view/App.tsx index 9bf1f4d..2cb1878 100644 --- a/view/App.tsx +++ b/view/App.tsx @@ -1,5 +1,5 @@ import * as React from "react"; -import { getTasks } from "./habiticaAPI" +import { getTasks, getStats } from "./habiticaAPI" import TodoItem from "./TodoItem" let username = "" @@ -32,18 +32,37 @@ class App extends React.Component { }) } ) + + getStats(username, credentials) + .then(res => res.json()) + .then( + result => { + this.setState({ + isLoaded: true, + stats: result.data + }) + }, + (error) => { + this.setState({ + isLoaded: true, + error + }) + } + ) } render(){ - const { error, isLoaded, tasks } = this.state; + const { error, isLoaded, tasks, stats } = this.state; if (error) { return
Error: {error.message}
; } else if (!isLoaded) { return
Loading...
; } else { const listItems = tasks.map((tasks: any) => +
- +

{stats}

+
); return (
    {listItems}
diff --git a/view/habiticaAPI.ts b/view/habiticaAPI.ts index 39b9eaf..1e80149 100644 --- a/view/habiticaAPI.ts +++ b/view/habiticaAPI.ts @@ -14,3 +14,19 @@ export async function getTasks(username: string, credentials: string){ }) return (response) } + +export async function getStats(username: string, credentials: string){ + const url = "https://habitica.com/export/userdata.json" + const response = fetch(url, { + method: 'GET', + headers: { + "Content-Type": "application/json", + "x-client": username.concat("-testAPI"), + "x-api-user": username, + "x-api-key": credentials, + }, + }) + console.log(response) + console.log("stats above") + return (response) +} \ No newline at end of file From c95890ead471341984456ecad53122bd4967506e Mon Sep 17 00:00:00 2001 From: Leoh Date: Wed, 13 Oct 2021 14:10:54 +0530 Subject: [PATCH 2/2] fixed stats display, and fixed react warning for keys --- view/App.tsx | 64 +++++++++++++++++++++++++++------------------ view/TodoItem.tsx | 4 +-- view/habiticaAPI.ts | 3 +-- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/view/App.tsx b/view/App.tsx index 2cb1878..cc7c1b4 100644 --- a/view/App.tsx +++ b/view/App.tsx @@ -12,47 +12,59 @@ class App extends React.Component { credentials = this.props.apiToken this.state = { isLoaded: false, - tasks: "" + user_data: { + profile: { + name: "", + }, + stats: { + hp: 0, + lvl: 0, + } + }, + tasks: [] //gave an error if the the tasks thing was string so better keep it an array for .map to work :) } } componentDidMount() { - getTasks(username, credentials) + // getTasks(username, credentials) + // .then(res => res.json()) + // .then( + // result => { + // this.setState({ + // isLoaded: true, + // tasks: result.data + // }) + // }, + // (error) => { + // this.setState({ + // isLoaded: true, + // error + // }) + // } + // ) + + getStats(username, credentials) .then(res => res.json()) .then( result => { + console.log(result) //yup this prints out correctly! since the promise is handled by .then this.setState({ isLoaded: true, - tasks: result.data - }) + user_data: result, + tasks: result.tasks.todos + }) }, (error) => { this.setState({ isLoaded: true, error - }) - } - ) - - getStats(username, credentials) - .then(res => res.json()) - .then( - result => { - this.setState({ - isLoaded: true, - stats: result.data - }) - }, - (error) => { - this.setState({ - isLoaded: true, - error }) } - ) + ) } render(){ - const { error, isLoaded, tasks, stats } = this.state; + const { error, isLoaded, tasks } = this.state; + const user_data = this.state.user_data if (error) { return
Error: {error.message}
; } else if (!isLoaded) { @@ -61,11 +73,13 @@ class App extends React.Component { const listItems = tasks.map((tasks: any) =>
-

{stats}

); - return ( + return (
+

{user_data.profile.name}

{"\n"} +
HP: {user_data.stats.hp}
XP: {user_data.stats.lvl}
{"\n"}
    {listItems}
+
); } } diff --git a/view/TodoItem.tsx b/view/TodoItem.tsx index 84a5415..6566dcc 100644 --- a/view/TodoItem.tsx +++ b/view/TodoItem.tsx @@ -1,8 +1,8 @@ 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 1e80149..e0ab591 100644 --- a/view/habiticaAPI.ts +++ b/view/habiticaAPI.ts @@ -26,7 +26,6 @@ export async function getStats(username: string, credentials: string){ "x-api-key": credentials, }, }) - console.log(response) - console.log("stats above") + console.log("stats") //can't print stats from here since the response is still an unresolved promise return (response) } \ No newline at end of file