Advertisement
Guest User

Untitled

a guest
May 20th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import React, { useEffect, useState } from 'react';
  2. import './SMSForm.css';
  3.  
  4. const SMSForm = () => {
  5. const [message, setMessage] = useState({
  6. to: '',
  7. body: ''
  8. });
  9. const [error, setError] = useState(false);
  10. const [submitting, setSubmitting] = useState(false);
  11.  
  12. const setInitialValues = () => {
  13. setMessage({to: '', body: ''});
  14. setError(false);
  15. setSubmitting(false);
  16. }
  17.  
  18. const onHandleChange = e => {
  19. const name = e.target.getAttribute('name');
  20. setMessage({...message, [name]: e.target.value});
  21. }
  22.  
  23. const onSubmit = e => {
  24. setSubmitting(true);
  25. fetch("http://localhost:3001/api/messages", {
  26. method: 'POST',
  27. headers: {
  28. 'Content-Type': 'application/json'
  29. },
  30. body: JSON.stringify(message)
  31. }).then(res => res.json())
  32. .then(data => {
  33. if (data.success) {
  34. setInitialValues();
  35. }
  36. else {
  37. setError(true);
  38. setSubmitting(false);
  39. }
  40. })
  41. e.preventDefault();
  42. }
  43.  
  44. useEffect(() => {
  45.  
  46. }, [submitting]);
  47.  
  48.  
  49. return (
  50. <div>
  51. <form
  52. onSubmit={onSubmit}
  53. className={error ? 'error sms-form' : 'sms-form'}>
  54. <div>
  55. <label htmlFor="to">To:</label>
  56. <input
  57. type="tel"
  58. name="to"
  59. id="to"
  60. //value={state.message.to}
  61. onChange={onHandleChange}
  62. />
  63. </div>
  64. <div>
  65. <label htmlFor="body">Body:</label>
  66. <textarea
  67. name="body"
  68. id="body"
  69. //value={state.message.body}
  70. onChange={onHandleChange}
  71. />
  72. </div>
  73. <button type="submit" disabled={submitting}>
  74. Send message
  75. </button>
  76. </form>
  77. </div>
  78. )
  79. }
  80. export default SMSForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement