Skip to content

Commit

Permalink
add callBack to AddDuelForm
Browse files Browse the repository at this point in the history
  • Loading branch information
myslak71 authored and myslak71 committed Jan 28, 2019
1 parent e0bd9ac commit 7d8edec
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
2 changes: 2 additions & 0 deletions duels/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ def test_proper_value(self):
duel = Duel.objects.get(pk=1)
algorithm = Algorithm.objects.get(pk=1)
self.assertEqual(count_percentage(duel, algorithm), 99.074)


25 changes: 7 additions & 18 deletions src/components/AddDuelForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,20 @@ class AddDuelForm extends React.Component {
dataset: this.state.dataset,
rounds: []
};
console.log(postObj)
axios.defaults.xsrfHeaderName = "X-CSRFTOKEN";
axios.defaults.xsrfCookieName = "csrftoken";
axios.defaults.headers = {
"Content-Type": "application/json",
Authorization: `Token ${this.props.token}`,
};

axios.post("http://127.0.0.1:8000/api/duel/create/", postObj)
.then(res => {
if (res.status === 201) {
this.props.callBack()
}
})

if (requestType === "post") {
await axios.post("http://127.0.0.1:8000/api/duel/create/", postObj)
.then(res => {
if (res.status === 201) {
this.props.history.push(`/`);
}
})
} else if (requestType === "put") {
await axios.put(`http://127.0.0.1:8000/api/duel/${duelID}/update/`, postObj)
.then(res => {
if (res.status === 200) {
this.props.history.push(`/`);
}
})
}
};

handleUserChange(value) {
Expand All @@ -66,8 +57,6 @@ class AddDuelForm extends React.Component {
onSubmit={event =>
this.handleFormSubmit(
event,
this.props.requestType,
this.props.articleID
)
}
>
Expand Down
11 changes: 9 additions & 2 deletions src/containers/DuelListView.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class DuelList extends React.Component {
datasets: []
};

update = () => {
const promises = [this.fetchDuels(), this.fetchUsers(), this.fetchDatasets()]
Promise.all(promises).then(([duelsResponse, usersResponse, datasetsResponse]) => {
this.setState({duels: duelsResponse.data, users: usersResponse.data, datasets: datasetsResponse.data});
});
}

fetchDuels = () => {
return axios.get("http://127.0.0.1:8000/api/duel/user/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
Expand All @@ -25,7 +32,7 @@ class DuelList extends React.Component {
return axios.get("http://127.0.0.1:8000/api/dataset/",
{'headers': {'Authorization': `Token ${localStorage.getItem('token')}`}})
};

componentDidMount() {
const promises = [this.fetchDuels(), this.fetchUsers(), this.fetchDatasets()]
Promise.all(promises).then(([duelsResponse, usersResponse, datasetsResponse]) => {
Expand All @@ -40,7 +47,7 @@ class DuelList extends React.Component {
<Duels data={this.state.duels}/> <br/>
<h2> Add duel </h2>
<AddDuelForm users={this.state.users} datasets={this.state.datasets} requestType="post" articleID={null}
btnText="Challenge"/>
btnText="Challenge" callBack={this.update}/>
</div>
);
}
Expand Down
4 changes: 0 additions & 4 deletions src/containers/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ class CustomLayout extends React.Component {
</Menu>
</Header>
<Content style={{padding: '0 50px'}}>
<Breadcrumb style={{margin: '16px 0'}}>
<Breadcrumb.Item><Link to="/">Home</Link></Breadcrumb.Item>
<Breadcrumb.Item><Link to="/">List</Link></Breadcrumb.Item>
</Breadcrumb>
<div style={{background: '#fff', padding: 24, minHeight: 280}}>
{this.props.children}
</div>
Expand Down

0 comments on commit 7d8edec

Please sign in to comment.