new feature:delete task

This commit is contained in:
kkzzhizhou 2021-11-23 23:00:47 +08:00
parent bfb4ae627f
commit 73f4cbd16b
13 changed files with 118 additions and 67 deletions

View file

@ -22,13 +22,14 @@
"css-loader": "^6.4.0", "css-loader": "^6.4.0",
"extract-text-webpack-plugin": "^2.1.2", "extract-text-webpack-plugin": "^2.1.2",
"obsidian": "^0.12.0", "obsidian": "^0.12.0",
"obsidian-plugin-cli": "^0.4.3", "obsidian-plugin-cli": "^0.8.1",
"rollup": "^2.32.1", "rollup": "^2.32.1",
"style-loader": "^3.3.0", "style-loader": "^3.3.0",
"tslib": "^2.2.0", "tslib": "^2.2.0",
"typescript": "^4.2.4" "typescript": "^4.2.4"
}, },
"dependencies": { "dependencies": {
"@mui/icons-material": "^5.2.0",
"i18next": "^21.5.2", "i18next": "^21.5.2",
"i18next-browser-languagedetector": "^6.1.2", "i18next-browser-languagedetector": "^6.1.2",
"node": "^16.10.0", "node": "^16.10.0",

View file

@ -1,6 +1,6 @@
import * as React from "react"; import * as React from "react";
import { Notice } from "obsidian"; import { Notice } from "obsidian";
import { getStats, scoreTask, makeCronReq, costReward, addTask } from "./habiticaAPI" import { getStats, scoreTask, makeCronReq, costReward, addTask, deleteTask } from "./habiticaAPI"
import Statsview from "./Components/Statsview" import Statsview from "./Components/Statsview"
import Taskview from "./Components/Taskview" import Taskview from "./Components/Taskview"
import "../i18n" import "../i18n"
@ -149,16 +149,41 @@ class App extends React.Component<any, any> {
} }
} }
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")
}
}
handleChangeTodos(event: any) { handleChangeTodos(event: any) {
this.state.tasks.todos.forEach((element: any) => { this.state.tasks.todos.forEach((element: any) => {
if (element.id == event.target.id) { if (element.id == event.target.id) {
if (!element.completed) { if ( event.type == "click" ) {
this.sendScore(event.target.id, "up", "Checked!") console.log(event)
if ( event.target.innerText == 'clear' ) {
this.sendDeleteTask(event.target.id, "Deleted!")
}
} else { } else {
this.sendScore(event.target.id, "down", "Un-Checked!") if (!element.completed) {
this.sendScore(event.target.id, "up", "Checked!")
} else {
this.sendScore(event.target.id, "down", "Un-Checked!")
}
} }
} }
}) })
} }
@ -166,8 +191,11 @@ class App extends React.Component<any, any> {
this.state.tasks.dailys.forEach((element: any) => { this.state.tasks.dailys.forEach((element: any) => {
if (element.id == event.target.id) { if (element.id == event.target.id) {
if (element.id == event.target.id) { if (element.id == event.target.id) {
if (!element.completed) { if (!element.completed && event.target.innerText != 'clear') {
this.sendScore(event.target.id, "up", "Checked!") this.sendScore(event.target.id, "up", "Checked!")
}
else if (event.target.innerText == 'clear'){
this.sendDeleteTask(event.target.id, "Deleted!")
} else { } else {
this.sendScore(event.target.id, "down", "Un-Checked!") this.sendScore(event.target.id, "down", "Un-Checked!")
} }
@ -177,30 +205,43 @@ class App extends React.Component<any, any> {
} }
handleChangeHabits(event: any) { handleChangeHabits(event: any) {
const target_id = event.target.id.slice(4) if ( event.type == "click" ) {
if (event.target.id.slice(0, 4) == "plus") { if ( event.target.innerText == 'clear' ) {
this.state.tasks.habits.forEach((element: any) => { this.sendDeleteTask(event.target.id, "Deleted!")
if (element.id == target_id) { }
this.sendScore(target_id, "up", "Plus!") } else {
} const target_id = event.target.id.slice(4)
}) if (event.target.id.slice(0, 4) == "plus") {
} this.state.tasks.habits.forEach((element: any) => {
else { if (element.id == target_id) {
this.state.tasks.habits.forEach((element: any) => { this.sendScore(target_id, "up", "Plus!")
if (element.id == target_id) { }
this.sendScore(target_id, "down", "Minus :(") })
} }
}) else {
this.state.tasks.habits.forEach((element: any) => {
if (element.id == target_id) {
this.sendScore(target_id, "down", "Minus :(")
}
})
}
} }
} }
handleChangeRewards(event: any) { handleChangeRewards(event: any) {
const target_id = event.target.id const target_id = event.target.id
this.state.tasks.rewards.forEach((element: any) => { this.state.tasks.rewards.forEach((element: any) => {
if (element.id == event.target.id) { if (element.id == event.target.id) {
if (element.id == target_id) { if ( event.type == "click" ) {
this.sendReward(target_id, "down", "Cost!") if ( event.target.innerText == 'clear' ) {
} this.sendDeleteTask(event.target.id, "Deleted!")
}
} else {
if (element.id == target_id) {
this.sendReward(target_id, "down", "Cost!")
}
}
} }
}) })
} }

View file

@ -7,6 +7,7 @@ type FormData = {
}; };
export default function Index(props: any) { export default function Index(props: any) {
console.log(props)
const { register, setValue, handleSubmit, formState: { errors } } = useForm<FormData>(); const { register, setValue, handleSubmit, formState: { errors } } = useForm<FormData>();
const onSubmit = handleSubmit(data => { const onSubmit = handleSubmit(data => {
const url = "https://habitica.com/api/v4/tasks/user" const url = "https://habitica.com/api/v4/tasks/user"

View file

@ -2,24 +2,22 @@ import Emoji from "react-emoji-render";
import * as React from "react"; import * as React from "react";
import ReactMarkdown from "react-markdown"; import ReactMarkdown from "react-markdown";
function DailyItem(props: any) { function DailyItem(props: any) {
return ( return (
<div className="todo-item" id={props.id}> <div className="todo-item" id={props.id}>
{console.log(props)} <input type="checkbox" className="checkbox" id={props.id} onChange={props.onChange} checked={props.completed} />
<input type="checkbox" className="checkbox" id={props.id} onChange={props.onChange} checked={props.completed} /> <div className="todo-content">
<div className="todo-content"> <p><Emoji text={props.daily_text}></Emoji></p>
<p><Emoji text={props.daily_text}></Emoji></p> <ReactMarkdown children={props.daily_notes} />
<ReactMarkdown children={props.daily_notes} /> </div>
<button className="task-operation" >
<span className="material-icons md-24" id={props.id} onClick={props.onChange}>create</span>
</button>
<button className="task-operation">
<span className="material-icons md-24" id={props.id} onClick={props.onChange}>clear</span>
</button>
</div> </div>
<button className="modify-todo" id={props.id}>
<span className="material-icons md-24">create</span>
</button>
<button className="delete-todo" id={props.id}>
<span className="material-icons md-24">clear</span>
</button>
</div>
) )
} }

View file

@ -25,7 +25,7 @@ export default function Index(props: any){
<Tab><Trans>Active</Trans></Tab> <Tab><Trans>Active</Trans></Tab>
<Tab><Trans>Completed</Trans></Tab> <Tab><Trans>Completed</Trans></Tab>
</TabList> </TabList>
<AddTask type="daily"></AddTask> <AddTask type="daily" onChange={props.onChange}></AddTask>
<TabPanel> <TabPanel>
<ul>{incompleteDailies}</ul> <ul>{incompleteDailies}</ul>
</TabPanel> </TabPanel>

View file

@ -15,12 +15,12 @@ function HabitItem(props: any) {
<p className="habit-text"><Emoji text={props.habit_text}></Emoji></p> <p className="habit-text"><Emoji text={props.habit_text}></Emoji></p>
<ReactMarkdown children={props.habit_notes} /> <ReactMarkdown children={props.habit_notes} />
</div> </div>
<button className="modify-habit" id={props.id}> <button className="task-operation" >
<span className="material-icons md-24">create</span> <span className="material-icons md-24" id={props.id} onClick={props.onChange}>create</span>
</button>
<button className="delete-habit" id={props.id}>
<span className="material-icons md-24">clear</span>
</button> </button>
<button className="task-operation">
<span className="material-icons md-24" id={props.id} onClick={props.onChange}>clear</span>
</button>
</div> </div>
) )
} }

View file

@ -1,6 +1,7 @@
import * as React from "react"; import * as React from "react";
import HabitItem from "./HabitItem" import HabitItem from "./HabitItem"
import { useTranslation, Trans, Translation } from 'react-i18next' import { useTranslation, Trans, Translation } from 'react-i18next'
import AddTask from "../AddTask";
export default function Index(props: any){ export default function Index(props: any){
if(props.habits == undefined) { if(props.habits == undefined) {
@ -13,11 +14,7 @@ export default function Index(props: any){
return <HabitItem key={habit.id} id={habit.id} habit_text={habit.text} habit_notes={habit.notes} upCount={habit.counterUp} downCount={habit.counterDown} onChange={props.onChange}/> return <HabitItem key={habit.id} id={habit.id} habit_text={habit.text} habit_notes={habit.notes} upCount={habit.counterUp} downCount={habit.counterDown} onChange={props.onChange}/>
}) })
const display = <div id="classDisplay"> const display = <div id="classDisplay">
<div className="add-task-input"> <AddTask type="habit"></AddTask>
<input type="text">
</input>
<button className="submit-button"><Trans>submit</Trans></button>
</div>
<ul>{allHabits}</ul> <ul>{allHabits}</ul>
</div> </div>

View file

@ -9,11 +9,11 @@ function RewardItem(props: any) {
<p className="reward-text"><Emoji text={props.reward_text}></Emoji></p> <p className="reward-text"><Emoji text={props.reward_text}></Emoji></p>
<ReactMarkdown children={props.reward_notes} /> <ReactMarkdown children={props.reward_notes} />
</div> </div>
<button className="modify-reward" id={props.id}> <button className="task-operation" >
<span className="material-icons md-24">create</span> <span className="material-icons md-24" id={props.id} onClick={props.onChange}>create</span>
</button> </button>
<button className="delete-reward" id={props.id}> <button className="task-operation">
<span className="material-icons md-24">clear</span> <span className="material-icons md-24" id={props.id} onClick={props.onChange}>clear</span>
</button> </button>
</div> </div>
) )

View file

@ -1,6 +1,7 @@
import * as React from "react"; import * as React from "react";
import RewardItem from "./RewardItem" import RewardItem from "./RewardItem"
import { useTranslation, Trans, Translation } from 'react-i18next' import { useTranslation, Trans, Translation } from 'react-i18next'
import AddTask from "../AddTask";
export default function Index(props: any){ export default function Index(props: any){
if(props.rewards == undefined) { if(props.rewards == undefined) {
@ -13,11 +14,7 @@ export default function Index(props: any){
return <RewardItem key={reward.id} id={reward.id} reward_text={reward.text} reward_notes={reward.notes} reward_value={reward.value} onChange={props.onChange}/> return <RewardItem key={reward.id} id={reward.id} reward_text={reward.text} reward_notes={reward.notes} reward_value={reward.value} onChange={props.onChange}/>
}) })
const display = <div id="classDisplay"> const display = <div id="classDisplay">
<div className="add-task-input"> <AddTask type="reward"></AddTask>
<input type="text">
</input>
<button className="submit-button"><Trans>submit</Trans></button>
</div>
<ul>{allRewards}</ul> <ul>{allRewards}</ul>
</div> </div>

View file

@ -10,11 +10,11 @@ function TodoItem(props: any) {
<p><Emoji text={props.todo_text}></Emoji></p> <p><Emoji text={props.todo_text}></Emoji></p>
<ReactMarkdown children={props.todo_notes} /> <ReactMarkdown children={props.todo_notes} />
</div> </div>
<button className="modify-todo" id={props.id}> <button className="task-operation" >
<span className="material-icons md-24">create</span> <span className="material-icons md-24" id={props.id} onClick={props.onChange}>create</span>
</button> </button>
<button className="delete-todo" id={props.id}> <button className="task-operation">
<span className="material-icons md-24">clear</span> <span className="material-icons md-24" id={props.id} onClick={props.onChange}>clear</span>
</button> </button>
</div> </div>
) )

View file

@ -2,6 +2,7 @@ import * as React from "react";
import TodoItem from "./TodoItem" import TodoItem from "./TodoItem"
import { Tab, Tabs, TabList, TabPanel } from "react-tabs"; import { Tab, Tabs, TabList, TabPanel } from "react-tabs";
import { useTranslation, Trans, Translation } from 'react-i18next' import { useTranslation, Trans, Translation } from 'react-i18next'
import AddTask from "../AddTask";
export default function Index(props: any){ export default function Index(props: any){
if(props.todos == undefined) { if(props.todos == undefined) {
@ -22,11 +23,7 @@ export default function Index(props: any){
<Tab><Trans>Active</Trans></Tab> <Tab><Trans>Active</Trans></Tab>
<Tab><Trans>Completed</Trans></Tab> <Tab><Trans>Completed</Trans></Tab>
</TabList> </TabList>
<div className="add-task-input"> <AddTask type="todo"></AddTask>
<input type="text">
</input>
<button className="submit-button"><Trans>submit</Trans></button>
</div>
<TabPanel> <TabPanel>
<ul>{incompleteTodos}</ul> <ul>{incompleteTodos}</ul>
</TabPanel> </TabPanel>

View file

@ -69,4 +69,18 @@ export async function addTask(username: string, credentials: string, title: stri
body: JSON.stringify({type: type, text: title}) body: JSON.stringify({type: type, text: title})
}) })
return(response) return(response)
}
export async function deleteTask(username: string, credentials: string, id: string) {
const url = "https://habitica.com/api/v4/tasks/".concat(id)
const response = fetch(url, {
method: 'DELETE',
headers: {
"Content-Type": "application/json",
"x-client": "278e719e-5f9c-43b1-9dba-8b73343dc062-HabiticaSync",
"x-api-user": username,
"x-api-key": credentials,
}
})
return(response)
} }

View file

@ -228,6 +228,11 @@ ul li:not(.task-list-item)::before {
font-weight: bold; font-weight: bold;
text-shadow: 0 0 0.5em transparent; text-shadow: 0 0 0.5em transparent;
} }
.task-operation {
align-self: center;
}
button { button {
margin: auto; margin: auto;
margin-bottom: 5px; margin-bottom: 5px;