-
Notifications
You must be signed in to change notification settings - Fork 1
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathNewBet.js
More file actions
More file actions
Latest commit
131 lines (122 loc) · 4.37 KB
/
Copy pathNewBet.js
File metadata and controls
131 lines (122 loc) · 4.37 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React, { Component } from 'react';
import { Col, Row, Button, Form, FormGroup, Label, Input, InputGroupAddon, InputGroup } from 'reactstrap';
import './NewBet.css';
import axios from 'axios';
import { connect } from 'react-redux';
// TODO share some classes with newquestion
class NewBet extends Component {
constructor(props) {
super(props);
this.state = {
isWaitingResponse: false,
success: false,
error: false,
option: "",
odd_numerator: undefined,
odd_denominator: undefined,
amount: 0.0
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
componentDidMount() {
this.setState({ option: this.props.defaultValue });
}
handleSubmit() {
const { odds_numerator, odds_denominator, option, amount } = this.state;
const { currentUser } = this.props;
this.setState({ isWaitingResponse: true, error: false, success: false });
axios.post(`http://localhost:4000/bet_primary`, {
user_id: currentUser.id,
odds_numerator,
odds_denominator,
option,
amount,
question_id: this.props.question.id
})
.then(res => {
this.setState({ isWaitingResponse: false, success: true });
this.props.reload();
})
.catch(error => {
this.setState({ isWaitingResponse: false, error: true });
});
}
handleChange(e) {
this.setState({ [e.target.name]: e.target.value });
}
render() {
const { defaultValue, question } = this.props;
const { isWaitingResponse, success, error } = this.state;
return (
<div className="NewBetBg">
<div className="NewBet">
{ isWaitingResponse &&
<div>
<h4>Submitting...</h4>
<span className="closeButton" onClick={this.props.close}>x</span>
</div>
}
{ !isWaitingResponse && success &&
<div>
<h4>Success!</h4>
<span className="closeButton" onClick={this.props.close}>x</span>
</div>
}
{ !isWaitingResponse && error &&
<div>
<h4>Error!</h4>
<span className="closeButton" onClick={this.props.close}>x</span>
</div>
}
{ !isWaitingResponse && !error && !success &&
<div>
<Row>
<Col xs={12}>
<h4>{question.title}</h4>
<span className="closeButton" onClick={this.props.close}>x</span>
</Col>
</Row>
<Form onSubmit={this.handleSubmit}>
<Row>
<Col md={5}>
<FormGroup>
<Input type="select" name="option" id="option" defaultValue={defaultValue} onChange={this.handleChange} required>
<option value="yes">Yes</option>
<option value="no">No</option>
</Input>
</FormGroup>
</Col>
<Col md={7}>
<FormGroup>
<Label>Your odds</Label>
<InputGroup>
<Input type="number" min="1" max="100" name="odds_numerator" id="odds_numerator" required onChange={this.handleChange}/>
<span className="odds_to">to</span>
<Input type="number" min="1" max="100" name="odds_denominator" id="odds_denominator" required onChange={this.handleChange}/>
</InputGroup>
</FormGroup>
<FormGroup>
<Label for="amount">Amount</Label>
<InputGroup>
<InputGroupAddon addonType="prepend">$</InputGroupAddon>
<Input type="number" min="1" step="1" name="amount" id="amount" required onChange={this.handleChange}/>
<InputGroupAddon addonType="append">.00</InputGroupAddon>
</InputGroup>
</FormGroup>
</Col>
</Row>
<Button>Place Bet</Button>
</Form>
</div>
}
<div className="clearfix"></div>
</div>
</div>
)
}
}
const mapStateToProps = store => ({
currentUser: store.clickState.currentUser
});
export default connect(mapStateToProps)(NewBet);