fixed stats display, and fixed react warning for keys
This commit is contained in:
parent
f77466b8a2
commit
c95890ead4
3 changed files with 42 additions and 29 deletions
56
view/App.tsx
56
view/App.tsx
|
|
@ -12,34 +12,45 @@ 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(res => res.json())
|
||||||
.then(
|
// .then(
|
||||||
result => {
|
// result => {
|
||||||
this.setState({
|
// this.setState({
|
||||||
isLoaded: true,
|
// isLoaded: true,
|
||||||
tasks: result.data
|
// tasks: result.data
|
||||||
})
|
// })
|
||||||
},
|
// },
|
||||||
(error) => {
|
// (error) => {
|
||||||
this.setState({
|
// this.setState({
|
||||||
isLoaded: true,
|
// isLoaded: true,
|
||||||
error
|
// error
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
)
|
// )
|
||||||
|
|
||||||
getStats(username, credentials)
|
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,
|
||||||
stats: result.data
|
user_data: result,
|
||||||
|
tasks: result.tasks.todos
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
|
|
@ -52,7 +63,8 @@ class App extends React.Component<any,any> {
|
||||||
|
|
||||||
}
|
}
|
||||||
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>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue