Advertisement
Grossos

React POST/DELETE with useEffect & useState

Sep 17th, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useEffect, useState } from "react"
  2.  
  3. export default function App() {
  4.     const [data, setData] = useState(null);
  5.     const [title, setTitle] = useState('');
  6.     const [desc, setDesc] = useState('');
  7.     const [category, setCategory] = useState('');
  8.     const [shouldFetch, setShouldFetch] = useState(false);
  9.     const body = { title, desc, category };
  10.  
  11.     const handleSubmit = async (e) => {
  12.         e.preventDefault();
  13.         const response = await fetch('https://crudapi.co.uk/api/v1/servers', {
  14.             method: 'POST',
  15.             headers: {
  16.                 'Content-Type': 'application/json',
  17.                 'Authorization': 'Bearer s7O03_9dOLr4OsDUvnr9ncxfSt5ETbHW3saBVJxFMCJsMAq2bg'
  18.             },
  19.             body: JSON.stringify([body])
  20.         })
  21.         const result = await response.json();
  22.         console.log('regiser successfully');
  23.         document.querySelectorAll('input').forEach(curr => curr.value = '');
  24.  
  25.         setShouldFetch(true);
  26.     }
  27.  
  28.     const handleDelete = async (e) => {
  29.         try {
  30.             const response = await fetch('https://crudapi.co.uk/api/v1/servers', {
  31.                 method: 'DELETE',
  32.                 headers: {
  33.                     'Content-Type': 'application/json',
  34.                     'Authorization': 'Bearer s7O03_9dOLr4OsDUvnr9ncxfSt5ETbHW3saBVJxFMCJsMAq2bg'
  35.                 },
  36.                 body: JSON.stringify([{ "_uuid": `${e.currentTarget.id}` }])
  37.             })
  38.             const result = await response.json();
  39.             console.log(result);
  40.             setShouldFetch(true);
  41.         } catch (err) {
  42.             console.log(err);
  43.         }
  44.     }
  45.  
  46.     useEffect(() => {
  47.         async function fetchData() {
  48.             try {
  49.                 const response = await fetch('https://crudapi.co.uk/api/v1/servers', {
  50.                     method: 'GET',
  51.                     headers: {
  52.                         'Content-Type': 'application/json',
  53.                         'Authorization': 'Bearer s7O03_9dOLr4OsDUvnr9ncxfSt5ETbHW3saBVJxFMCJsMAq2bg'
  54.                     }
  55.                 })
  56.                 const result = await response.json();
  57.                 setData(result.items);
  58.                 //  console.log(result.items);
  59.             } catch (err) {
  60.                 console.log(err);
  61.             }
  62.         };
  63.  
  64.         fetchData();
  65.         setShouldFetch(false);
  66.     }, [shouldFetch]);
  67.  
  68.     useEffect(() => {
  69.         setShouldFetch(true);
  70.     }, [])
  71.  
  72.     return (
  73.         <>
  74.             <form>
  75.                 <input type="text" onChange={(e) => setTitle(e.target.value)} placeholder="title" required />
  76.                 <input type="text" onChange={(e) => setDesc(e.target.value)} placeholder="desc" required />
  77.                 <input type="text" onChange={(e) => setCategory(e.target.value)} placeholder="category" required />
  78.                 <button onClick={handleSubmit}>Register</button>
  79.             </form>
  80.  
  81.             {data ? data.map((curr, idx) => {
  82.                 return <div className={`wrapper ${idx}`} key={curr._uuid} >
  83.                     <span>{curr.title}</span>
  84.                     <span>{curr.desc}</span>
  85.                     <span>{curr.category}</span>
  86.                     <div className="actions">
  87.                         <button onClick={handleDelete} id={curr._uuid} >DELETE</button>
  88.                     </div>
  89.                 </div>
  90.             }) : <div>Loading....</div>}
  91.         </>
  92.     );
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement