Components Extracted

This commit is contained in:
Leoh 2021-10-15 17:33:32 +05:30
parent 98a15c0c9e
commit d4c2d25586
5 changed files with 35 additions and 20 deletions

View file

@ -1,7 +1,7 @@
import { Tasks } from "obsidian";
import * as React from "react"; import * as React from "react";
import { getStats, scoreTask } from "./habiticaAPI" import { getStats, scoreTask } from "./habiticaAPI"
import TodoItem from "./TodoItem" import Statsview from "./Components/Statsview"
import Taskview from "./Components/Taskview"
let username = "" let username = ""
let credentials = "" let credentials = ""
@ -22,7 +22,7 @@ class App extends React.Component<any,any> {
lvl: 0, lvl: 0,
} }
}, },
tasks: [] todos: []
} }
this.handleChange = this.handleChange.bind(this) this.handleChange = this.handleChange.bind(this)
} }
@ -39,7 +39,7 @@ class App extends React.Component<any,any> {
this.setState({ this.setState({
isLoaded: true, isLoaded: true,
user_data: result, user_data: result,
tasks: result.tasks.todos todos: result.tasks.todos
}) })
}, },
(error) => { (error) => {
@ -54,7 +54,7 @@ class App extends React.Component<any,any> {
this.reloadData() this.reloadData()
} }
handleChange(event: any){ handleChange(event: any){
this.state.tasks.forEach((element: any) => { this.state.todos.forEach((element: any) => {
if(element.id == event.target.id){ if(element.id == event.target.id){
if(!element.completed){ if(!element.completed){
scoreTask(username, credentials, event.target.id, "up") scoreTask(username, credentials, event.target.id, "up")
@ -100,20 +100,15 @@ class App extends React.Component<any,any> {
} }
render(){ render(){
const { error, isLoaded, tasks } = this.state; // if(this.state.error)
const user_data = this.state.user_data // return(<div className="loading">Loading....</div>)
if (error) { // else if(this.state.isLoaded)
return <div>Error: {error.message}</div>; if (!this.state.isLoaded)
} else if (!isLoaded) { return <div className="loading">Loading....</div>
return <div>Loading...</div>; else {
} else {
const listItems = tasks.map((tasks: any) =>
<TodoItem key={tasks.id} id={tasks.id} task={tasks} completed={tasks.completed} onChange={this.handleChange}/>
);
return (<div> return (<div>
<h3>{user_data.profile.name}</h3>{"\n"} <Statsview user_data={this.state.user_data} />
<div>HP: {user_data.stats.hp}</div><div> XP: {user_data.stats.lvl}</div>{"\n"} <Taskview todos={this.state.todos} onChange={this.handleChange} />
<ul>{listItems}</ul>
</div> </div>
); );
} }

View file

View file

@ -0,0 +1,11 @@
import * as React from 'react';
export default function Index(props: any) {
return(
<div id="stats">
<div id="profile-name">{props.user_data.profile.name}</div>
<div id="hp">HP: {props.user_data.stats.hp}</div>
<div id="lvl">LVL: {props.user_data.stats.lvl}</div>
</div>
);
}

View file

@ -3,8 +3,8 @@
function TodoItem(props: any) { function TodoItem(props: any) {
return ( return (
<div className="todo-item" id={props.id}> <div className="todo-item" id={props.id}>
<input type="checkbox" id={props.id} onChange={props.onChange} checked={props.completed}/> <input type="checkbox" className="checkbox" id={props.id} onChange={props.onChange} checked={props.completed}/>
<p>{props.task.text}</p> <p>{props.todo_text}</p>
</div> </div>
) )
} }

View file

@ -0,0 +1,9 @@
import * as React from "react";
import TodoItem from "./TodoItem"
export default function Index(props: any){
const listItems = props.todos.map((todo: any) => {
return <TodoItem key={todo.id} id={todo.id} todo_text={todo.text} onChange={props.onChange} completed={todo.completed}/>
})
return(<ul>{listItems}</ul>);
}