Guest User

Untitled

a guest
Sep 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import AskForm from '../../components/AskForm.js';
  3. import fire from '../../config/Fire.js';
  4. import { Link, withRouter } from 'react-router-dom'
  5.  
  6. class Ask extends Component {
  7. constructor(props){
  8. super(props);
  9. this.addTicket = this.addTicket.bind(this);
  10. this.database = fire.database().ref().child('tickets');
  11.  
  12. this.state = {
  13. tickets: [],
  14. userId: this.props.user.uid
  15. }
  16. }
  17.  
  18. componentDidMount(){
  19. fire.database().ref('/users/' + this.props.user.uid).once('value').then(function(snapshot) {
  20. var FirstName = (snapshot.val() && snapshot.val().userFirstName);
  21. // ...
  22. console.log(FirstName);
  23. });
  24. }
  25.  
  26. addTicket(title, body){
  27. this.database.push().set({ ticketUserId: this.props.user.uid, ticketTitle: title, ticketBody: body, ticketStatus: 'pending', ticketTime: fire.database.ServerValue.TIMESTAMP});
  28. alert("Your question has been submitted.")
  29. this.props.history.push('/pending')
  30. }
  31.  
  32. render() {
  33. return (
  34. <div>
  35. <div className="m-container">
  36. </div>
  37. <div>
  38. <AskForm addTicket={this.addTicket} />
  39. </div>
  40. </div>
  41. );
  42. }
  43. }
  44.  
  45. export default withRouter(Ask);
  46.  
  47. import React, { Component } from 'react';
  48.  
  49. class AskForm extends Component{
  50. constructor(props){
  51. super(props);
  52. this.state = {
  53. ticketBody: '',
  54. ticketTitle: ''
  55. };
  56. this.handleChange = this.handleChange.bind(this);
  57. this.writeTicket = this.writeTicket.bind(this);
  58. }
  59.  
  60. // When the user input changes, set the ticketTitle or ticketBody
  61. // to the value of what's in the input box.
  62.  
  63. handleChange(e) {
  64. this.setState({ [e.target.name]: e.target.value });
  65. }
  66.  
  67. writeTicket(){
  68. if(this.state.ticketTitle === '' || this.state.ticketBody === ''){
  69. alert('Please complete all fields.')
  70. } else {
  71. // Call a method that sets the ticketTitle and ticketBody for a ticket to
  72. // the value of the input
  73. this.props.addTicket(this.state.ticketTitle, this.state.ticketBody);
  74. // Set inputs back to an empty string
  75. this.setState({
  76. ticketBody: '',
  77. ticketTitle: ''
  78. })
  79. }
  80.  
  81. }
  82.  
  83. render(){
  84. return(
  85. <div class="s-container">
  86. <form>
  87. <label for="ticketTitle">Title: </label>
  88. <input
  89. id="ticketTitle"
  90. name="ticketTitle"
  91. type="text"
  92. placeholder="A short sentence to identify your issue"
  93. value={this.state.ticketTitle}
  94. onChange={this.handleChange}
  95. />
  96. <br/>
  97. <br/>
  98. <label for="ticketBody">Description: </label>
  99. <textarea
  100. id="ticketBody"
  101. name="ticketBody"
  102. placeholder="Placeholder"
  103. value={this.state.ticketBody}
  104. onChange={this.handleChange}
  105. /> &nbsp;
  106. <button
  107. className="m-btn"
  108. onClick={this.writeTicket}>
  109. Submit
  110. </button>
  111. </form>
  112. </div>
  113. )
  114. }
  115. }
  116.  
  117. export default AskForm;
Add Comment
Please, Sign In to add comment