fixed stats display, and fixed react warning for keys

This commit is contained in:
Leoh 2021-10-13 14:10:54 +05:30
parent f77466b8a2
commit c95890ead4
3 changed files with 42 additions and 29 deletions

View file

@ -12,47 +12,59 @@ class App extends React.Component<any,any> {
credentials = this.props.apiToken credentials = this.props.apiToken
this.state = { this.state = {
isLoaded: false, 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() { 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(res => res.json())
.then( .then(
result => { result => {
console.log(result) //yup this prints out correctly! since the promise is handled by .then
this.setState({ this.setState({
isLoaded: true, isLoaded: true,
tasks: result.data user_data: result,
}) tasks: result.tasks.todos
})
}, },
(error) => { (error) => {
this.setState({ this.setState({
isLoaded: true, isLoaded: true,
error error
})
}
)
getStats(username, credentials)
.then(res => res.json())
.then(
result => {
this.setState({
isLoaded: true,
stats: result.data
})
},
(error) => {
this.setState({
isLoaded: true,
error
}) })
} }
) )
} }
render(){ render(){
const { error, isLoaded, tasks, stats } = this.state; const { error, isLoaded, tasks } = this.state;
const user_data = this.state.user_data
if (error) { if (error) {
return <div>Error: {error.message}</div>; return <div>Error: {error.message}</div>;
} else if (!isLoaded) { } else if (!isLoaded) {
@ -61,11 +73,13 @@ class App extends React.Component<any,any> {
const listItems = tasks.map((tasks: any) => const listItems = tasks.map((tasks: any) =>
<div> <div>
<TodoItem key={tasks.id} task={tasks}/> <TodoItem key={tasks.id} task={tasks}/>
<p>{stats}</p>
</div> </div>
); );
return ( return (<div>
<h3>{user_data.profile.name}</h3>{"\n"}
<div>HP: {user_data.stats.hp}</div><div> XP: {user_data.stats.lvl}</div>{"\n"}
<ul>{listItems}</ul> <ul>{listItems}</ul>
</div>
); );
} }
} }

View file

@ -2,7 +2,7 @@
function TodoItem(props: any) { function TodoItem(props: any) {
return ( return (
<div className="todo-item"> <div className="todo-item" key = {props.key}>
<input type="checkbox" /> <input type="checkbox" />
<p>{props.task.text}</p> <p>{props.task.text}</p>
</div> </div>

View file

@ -26,7 +26,6 @@ export async function getStats(username: string, credentials: string){
"x-api-key": credentials, "x-api-key": credentials,
}, },
}) })
console.log(response) console.log("stats") //can't print stats from here since the response is still an unresolved promise
console.log("stats above")
return (response) return (response)
} }