form.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withStyles } from '@material-ui/core/styles';
  4. import MenuItem from '@material-ui/core/MenuItem';
  5. import TextField from '@material-ui/core/TextField';
  6. import Select from '@material-ui/core/Select';
  7. import { Button } from '@material-ui/core'
  8. import { FormControl, Input, InputLabel, FormHelperText } from '@material-ui/core'
  9. const styles = theme => ({
  10. container: {
  11. display: 'inline',
  12. },
  13. textField: {
  14. marginLeft: theme.spacing.unit,
  15. marginRight: theme.spacing.unit,
  16. display: 'inline-block',
  17. width: 200,
  18. },
  19. dense: {
  20. marginTop: 19,
  21. },
  22. menu: {
  23. width: 200,
  24. },
  25. fullWidth: {
  26. width: '100%'
  27. },
  28. blackText: {
  29. color: 'black'
  30. },
  31. floatLeft: {
  32. float: 'left',
  33. display: 'inline'
  34. }
  35. });
  36. const cats = [
  37. 'Cool Cat',
  38. 'Bad Kitty',
  39. 'King of the Jungle',
  40. 'Incel cat',
  41. 'C-ch-at-d',
  42. 'Sabretooth Bro',
  43. 'Higher order being',
  44. 'Decadent feline',
  45. 'Trapped in the litter box',
  46. 'All claw and no meow'
  47. ]
  48. const MakeAttendees = (attendees) => {
  49. const items = []
  50. for (let i = 0; i < attendees.length; i++) {
  51. items.push(<div style={{flex: 1}}key={i}>{i+1}: <div><bold>Name:</bold> {attendees[i].name}</div><div><bold>Type: </bold>{attendees[i].cat ? attendees[i].cat : 'Incel, for sure'}</div><div><bold>Lonely: </bold>{attendees[i].guest ? 'Probably' : 'Very!'}</div><br /><br /></div>)
  52. }
  53. return (<div style={{display: 'block', color: 'black', fontSize: '50%'}}>{items}</div>)
  54. }
  55. class TextFields extends React.Component {
  56. constructor (props) {
  57. super(props)
  58. this.state = {
  59. name: 'The Grinch',
  60. cat: '',
  61. significantOther: false,
  62. attendees: undefined
  63. }
  64. }
  65. async componentDidMount () {
  66. this.setState({
  67. attendees: MakeAttendees(await this.fetchAttendees())
  68. })
  69. }
  70. handleChange = name => event => {
  71. this.setState({
  72. [name]: event.target.value,
  73. });
  74. };
  75. handleFoodSelect = (event) => {
  76. this.setState({cat: event.target.value})
  77. }
  78. handleSignificantOther = () => {
  79. this.setState({significantOther: !this.state.significantOther})
  80. }
  81. Cats = () => {
  82. const items = []
  83. for (let i = 0; i < cats.length; i++) {
  84. items.push(<MenuItem key={i} value={cats[i]}>{cats[i]}</MenuItem>)
  85. }
  86. return items
  87. }
  88. submitForm = async () => {
  89. console.log(this.state)
  90. const body = JSON.stringify({
  91. name: this.state.name,
  92. cat: this.state.cat,
  93. guest: this.state.significantOther,
  94. })
  95. const response = await fetch('http://localhost:3002/attendee', {
  96. method: 'post',
  97. body: body,
  98. headers: new Headers({
  99. 'Content-Type': 'application/json'
  100. })
  101. })
  102. if (!response.error) {
  103. console.log('Success')
  104. this.setState({attendees: MakeAttendees(await this.fetchAttendees())})
  105. }
  106. }
  107. fetchAttendees = async () => {
  108. let attendees
  109. await fetch('http://localhost:3002/list', {
  110. method: 'get',
  111. }).then(data => {
  112. data.json().then(finalData => {
  113. attendees = finalData.attendees
  114. })
  115. })
  116. return attendees
  117. }
  118. updateAttendees = async () => {
  119. this.setState({ attendees: MakeAttendees(await this.fetchAttendees())})
  120. }
  121. render() {
  122. const { classes } = this.props;
  123. return (
  124. <div>
  125. <form className={classes.container} noValidate autoComplete="off">
  126. <div className='name-wrap' style={styles.fullWidth}>
  127. <InputLabel id='name-label' htmlFor="attendee-name">Name</InputLabel>
  128. <TextField
  129. id="attendee-name"
  130. label=""
  131. className={classes.textField}
  132. value={this.state.name}
  133. onChange={this.handleChange('name')}
  134. margin="normal"
  135. />
  136. </div>
  137. <br />
  138. <div style={styles.fullWidth}>
  139. <InputLabel htmlFor="significant-other" style={{float: 'left'}}>Bringing someone?</InputLabel>
  140. <input
  141. id="significant-other"
  142. name="Significant Other"
  143. type="checkbox"
  144. className={classes.textField}
  145. style={{verticalAlign: 'top', marginLeft: '-10em'}}
  146. checked={this.state.significantOther}
  147. onChange={this.handleSignificantOther} />
  148. </div>
  149. <FormControl className='food-select'>
  150. <InputLabel htmlFor="food-helper">What sort of cat are you?</InputLabel>
  151. <Select
  152. value={this.state.cat}
  153. onChange={this.handleFoodSelect}
  154. input={<Input name="food" id="food-helper" value={this.state.foodItem}/>}>
  155. <MenuItem value="">
  156. <em>The hell you talking about</em>
  157. </MenuItem>
  158. {this.Cats()}
  159. </Select>
  160. <FormHelperText>Calling all cats and jive turkeys</FormHelperText>
  161. </FormControl>
  162. <br />
  163. <Button style={{marginTop: '1.25em'}}onClick={this.submitForm}> Click to Confirm Attendance </Button>
  164. <br /><br />
  165. <Button style={{marginTop: '1.25em'}}onClick={this.updateAttendees}> Who's coming? </Button>
  166. <br /><br />
  167. </form>
  168. <h3>Such lovely people, these:</h3>
  169. {this.state.attendees}
  170. </div>
  171. );
  172. }
  173. }
  174. TextFields.propTypes = {
  175. classes: PropTypes.object.isRequired,
  176. };
  177. export default withStyles(styles)(TextFields);