Guest User

Untitled

a guest
Sep 15th, 2026
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. import { useDispatch, useSelector } from "react-redux";
  2. import { useState, useEffect } from "react";
  3. import axios from 'axios';
  4. import {useDropzone} from "react-dropzone";
  5. import { useForm } from "react-hook-form";
  6.  
  7.  
  8. function MyField() {
  9. const { register, setValue, handleFormSubmit} = useForm();
  10. const [files, setFiles] = useState([]);
  11. const {getRootProps, getInputProps} = useDropzone({
  12. accept: {"image/*": []},
  13. onDrop: acceptedFiles => {
  14. setFiles(
  15. acceptedFiles.map(file =>
  16. Object.assign(file, {
  17. preview: URL.createObjectURL(file)
  18. })
  19. )
  20. );
  21. },
  22. });
  23.  
  24. const thumbs = files.map(file => (
  25. <div key={file.name}>
  26. <img style={{ maxWidth: "100%", maxHeight: "100%" }} src={file.preview} onLoad={() => URL.revokeObjectURL(file.preview)} alt={file.name} />
  27. </div>
  28. ));
  29.  
  30. const firstNameStr = 'firstName';
  31.  
  32. useEffect(() => {
  33. register({ name: firstNameStr.toString() }, { required: true });
  34. }, [register]);
  35.  
  36. useEffect(() => {
  37. return () => files.forEach(file => URL.revokeObjectURL(file.preview));
  38. }, [files]);
  39. const [editableValue, setEditableValue] = useState('');
  40. const [formData, setFormData] = useState({
  41. name: '',
  42. email: '',
  43. password: '',
  44. shipping: '',
  45. prod_description: ''
  46. });
  47. const handleChange = (e) => setEditableValue(e.target.value);
  48. const dispatch = useDispatch()
  49. function handleSubmit(e) {
  50.  
  51. e.preventDefault();
  52. const form = e.target;
  53. const formData = new FormData(form);
  54.  
  55. const formErrors = e.formErrors;
  56.  
  57.  
  58. // You can pass formData as a fetch body directly:
  59. //fetch('https://jsonplaceholder.typicode.com', { method: form.method, body: formData });
  60.  
  61. // Or you can work with it as a plain object:
  62. const formJson = Object.fromEntries(formData.entries());
  63. console.log(formJson);
  64. }
  65.  
  66. const registerForm = (formData) => async (dispatch) => {
  67. try {
  68. dispatch({ type: 'REGISTER_FORM_REQUEST' });
  69.  
  70. const config = {
  71. headers: {
  72. 'Content-Type': 'application/json'
  73. }
  74. };
  75. const {data} = await axios.post('https://jsonplaceholder.typicode.com', formData, config);
  76. dispatch({ type: 'REGISTER_FORM_SUCCESS', payload: data });
  77. } catch (error) {
  78. console.log('Error submitting form:', error);
  79. //console.error('Error submitting form:', error);
  80. dispatch({ type: 'REGISTER_FORM_FAILURE', payload: error.message });
  81. }
  82. };
  83. dispatch(registerForm(formData));
  84.  
  85. if(Object.keys(files).length === 0){
  86. console.log("file size 0");
  87. //this.setState({ hasError: true });
  88. } else {
  89. dispatch();
  90. }
  91.  
  92.  
  93. return (
  94. <>
  95. <form >
  96. <label>
  97. <form>
  98. <input
  99. name="firstName"
  100. onChange={(e) => {
  101. setValue("firstName", e.target.value);
  102. handleFormSubmit((data) => console.log("data", data));
  103. }}
  104. />
  105. </form>
  106. </label>
  107. <hr />
  108. <p>
  109. Radio buttons:
  110. <label>
  111. <input type="radio" name="myRadio" value="option1" />
  112. Option 1
  113. </label>
  114. <label>
  115. <input
  116. type="radio"
  117. name="myRadio"
  118. value="option2"
  119. defaultChecked={true}
  120. />
  121. Option 2
  122. </label>
  123. <label>
  124. <input type="radio" name="myRadio" value="option3" />
  125. Option 3
  126. </label>
  127. </p>
  128. <label>
  129. <hr />
  130. <button type="reset">Reset form</button>
  131. <button type="submit">Submit form</button>
  132. </label>
  133. </form>
  134. <section className="containerDragAndDrop">
  135. <div {...getRootProps({className: "dropzone"})}>
  136. <input {...getInputProps()} />
  137. <p>Drag 'n' drop some files here, or click to select files</p>
  138. </div>
  139. <aside>{thumbs}</aside>
  140. </section>
  141. </>
  142. );
  143. }
  144.  
  145. export default MyField;
Advertisement
Add Comment
Please, Sign In to add comment