From 04f207db5dc53fb0281bf0a118494b76ce590ee4 Mon Sep 17 00:00:00 2001 From: Zain Date: Sat, 30 Oct 2021 22:34:41 +0530 Subject: [PATCH 1/6] Implemented Cron - untested --- src/view/App.tsx | 33 +++++++++++++++++++++++++++++++-- src/view/habiticaAPI.ts | 2 +- styles.css | 20 ++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/view/App.tsx b/src/view/App.tsx index 3b1882d..ea5e9a9 100644 --- a/src/view/App.tsx +++ b/src/view/App.tsx @@ -1,6 +1,6 @@ import * as React from "react"; import { Notice } from "obsidian"; -import { getStats, scoreTask } from "./habiticaAPI" +import { getStats, scoreTask, makeCronReq } from "./habiticaAPI" import Statsview from "./Components/Statsview" import Taskview from "./Components/Taskview" @@ -12,6 +12,7 @@ class App extends React.Component { this.username = this.props.plugin.settings.userID this.credentials = this.props.plugin.settings.apiToken this.state = { + needCron: false, isLoaded: false, user_data: { profile: { @@ -20,7 +21,8 @@ class App extends React.Component { stats: { hp: 0, lvl: 0, - } + }, + lastCron: "", }, todos: [], dailys: [], @@ -32,6 +34,31 @@ class App extends React.Component { } + CheckCron(props: any) { + let cronDate = new Date(props.lastCron).getTime(); + let now = new Date().getTime(); + if (now > cronDate + 86400000) { + return( +
+
Welcome back! Please check your tasks for the last day and hit continue to get your daily rewards.
+ +
+ ); + } + else { + console.log("Cron is up to date"); + return null + }; + } + runCron() { + makeCronReq(this.username, this.credentials) + .then(res => { + this.setState({ + needCron: false + }) + }); + this.reloadData(); + } async reloadData() { try { let response = await getStats(this.username, this.credentials); @@ -40,6 +67,7 @@ class App extends React.Component { new Notice('Login Failed, Please check credentials and try again!'); } else { + console.log(result); this.setState({ isLoaded: true, user_data: result, @@ -124,6 +152,7 @@ class App extends React.Component { + ); } diff --git a/src/view/habiticaAPI.ts b/src/view/habiticaAPI.ts index db4e2a8..142a923 100644 --- a/src/view/habiticaAPI.ts +++ b/src/view/habiticaAPI.ts @@ -27,7 +27,7 @@ export async function scoreTask(username: string, credentials: string, taskID: s }) return(response) } -export async function makeCronReq(username: string, credentials: string, taskID: string){ +export async function makeCronReq(username: string, credentials: string){ const url = "https://habitica.com/api/v3/cron"; const response = fetch(url, { method: 'POST', diff --git a/styles.css b/styles.css index 1f12342..bdb8bd1 100644 --- a/styles.css +++ b/styles.css @@ -158,4 +158,24 @@ ul li:not(.task-list-item)::before { padding: 0; font-weight: bold; text-shadow: 0 0 0.5em transparent; +} +body > div.app-container.is-left-sidedock-collapsed.is-right-sidedock-collapsed > div.horizontal-main-container > div > div.workspace-split.mod-horizontal.mod-right-split > div.workspace-tabs > div.workspace-leaf > div > div.view-content > div > div.cron > button { + margin: auto; + margin-bottom: 5px; +} +.cron { + display: inline-grid; + justify-content: center; + text-align: center;; + margin-top: 5px; + margin-left: 10%; + margin-right: 10%; + margin-bottom: 10px; + background-color: var(--background-secondary-alt); + border-radius: 10px; +} +#cronMessage { + margin: 20px; + margin-bottom: 10px; + color: var(--text-normal) } \ No newline at end of file From af3b2fe53beab32e8b28b009538dd7fc3159c304 Mon Sep 17 00:00:00 2001 From: Zain Date: Sun, 31 Oct 2021 16:33:58 +0530 Subject: [PATCH 2/6] Cron implementation 2: Untested --- src/view/App.tsx | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/view/App.tsx b/src/view/App.tsx index ea5e9a9..72f4a31 100644 --- a/src/view/App.tsx +++ b/src/view/App.tsx @@ -28,20 +28,20 @@ class App extends React.Component { dailys: [], habits: [], } - this.handleChangeTodos = this.handleChangeTodos.bind(this) - this.handleChangeDailys = this.handleChangeDailys.bind(this) - this.handleChangeHabits = this.handleChangeHabits.bind(this) - + this.handleChangeTodos = this.handleChangeTodos.bind(this); + this.handleChangeDailys = this.handleChangeDailys.bind(this); + this.handleChangeHabits = this.handleChangeHabits.bind(this); + this.runCron = this.runCron.bind(this); } - CheckCron(props: any) { - let cronDate = new Date(props.lastCron).getTime(); - let now = new Date().getTime(); - if (now > cronDate + 86400000) { + CheckCron(lastCron: string) { + let cronDate = new Date(lastCron).getDate(); + let now = new Date().getDate(); + if (cronDate < now) { return(
Welcome back! Please check your tasks for the last day and hit continue to get your daily rewards.
- +
); } @@ -51,6 +51,7 @@ class App extends React.Component { }; } runCron() { + console.log("running cron"); makeCronReq(this.username, this.credentials) .then(res => { this.setState({ @@ -143,6 +144,7 @@ class App extends React.Component { } render(){ + let content = this.CheckCron(this.state.user_data.lastCron); if(this.state.error) return(
Loading....
) else if(!this.state.isLoaded) @@ -152,7 +154,7 @@ class App extends React.Component { - + {content} ); } From b3282a360c12c7534d336d046d82c0ee8cd02f6a Mon Sep 17 00:00:00 2001 From: Zain Date: Sun, 31 Oct 2021 16:39:47 +0530 Subject: [PATCH 3/6] Cron Implementation - tested --- src/view/App.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/view/App.tsx b/src/view/App.tsx index 72f4a31..b8f8814 100644 --- a/src/view/App.tsx +++ b/src/view/App.tsx @@ -41,7 +41,7 @@ class App extends React.Component { return(
Welcome back! Please check your tasks for the last day and hit continue to get your daily rewards.
- +
); } @@ -55,7 +55,7 @@ class App extends React.Component { makeCronReq(this.username, this.credentials) .then(res => { this.setState({ - needCron: false + needCron: false, }) }); this.reloadData(); From e0f400b761df61ba08be65c01e7f7862c7ad0d33 Mon Sep 17 00:00:00 2001 From: Zain Date: Mon, 1 Nov 2021 11:41:31 +0530 Subject: [PATCH 4/6] CronImplementation 3: tested --- src/view/App.tsx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/view/App.tsx b/src/view/App.tsx index b8f8814..c49cae7 100644 --- a/src/view/App.tsx +++ b/src/view/App.tsx @@ -37,7 +37,8 @@ class App extends React.Component { CheckCron(lastCron: string) { let cronDate = new Date(lastCron).getDate(); let now = new Date().getDate(); - if (cronDate < now) { + console.log(cronDate, now); + if (cronDate != now) { return(
Welcome back! Please check your tasks for the last day and hit continue to get your daily rewards.
@@ -50,14 +51,17 @@ class App extends React.Component { return null }; } - runCron() { + async runCron() { console.log("running cron"); - makeCronReq(this.username, this.credentials) - .then(res => { + try { + let response = await makeCronReq(this.username, this.credentials); this.setState({ needCron: false, }) - }); + } catch (error) { + console.log(error); + new Notice("There was an error running the cron. Please try again later."); + } this.reloadData(); } async reloadData() { From 412f9ed431ea192bbf68b182845af5f3747614b3 Mon Sep 17 00:00:00 2001 From: Zain Date: Mon, 1 Nov 2021 11:53:35 +0530 Subject: [PATCH 5/6] MAdded getter and setter methods for username and credentials --- src/view/App.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/view/App.tsx b/src/view/App.tsx index c49cae7..a36d771 100644 --- a/src/view/App.tsx +++ b/src/view/App.tsx @@ -5,8 +5,20 @@ import Statsview from "./Components/Statsview" import Taskview from "./Components/Taskview" class App extends React.Component { - username = "" - credentials = "" + 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; + } constructor(props: any) { super(props) this.username = this.props.plugin.settings.userID From 25a1505fd06f5ad2505ec502325e0abceadc4eb0 Mon Sep 17 00:00:00 2001 From: Zain Date: Tue, 16 Nov 2021 18:29:31 +0530 Subject: [PATCH 6/6] Cron-Implementation: Tested! --- src/view/App.tsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/view/App.tsx b/src/view/App.tsx index a36d771..6dda418 100644 --- a/src/view/App.tsx +++ b/src/view/App.tsx @@ -47,10 +47,9 @@ class App extends React.Component { } CheckCron(lastCron: string) { - let cronDate = new Date(lastCron).getDate(); - let now = new Date().getDate(); - console.log(cronDate, now); - if (cronDate != now) { + let cronDate = new Date(lastCron); + let now = new Date(); + if (cronDate.getDate() != now.getDate() || (cronDate.getMonth() != now.getMonth() || cronDate.getFullYear() != now.getFullYear())) { return(
Welcome back! Please check your tasks for the last day and hit continue to get your daily rewards.