justfrenzy

Untitled

Jul 1st, 2023
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const handleDelete = async (itemId, idx) => {
  2.   console.log(`asdsad`, itemId, idx);
  3.   const category = buttons.txt[buttons.is];
  4.   const updatedStates = [...results[category]];
  5.   const index = idx; // Use the provided index instead of findIndex
  6.  
  7.   if (index !== -1) {
  8.     updatedStates.splice(index, 1);
  9.   }
  10.  
  11.   setResults((prevResults) => ({
  12.     ...prevResults,
  13.     [category]: updatedStates,
  14.   }));
  15.  
  16.   const response = await fetch(`http://192.168.0.102:8000/delete/${buttons.is}/${itemId}`)
  17.     .then((res) => res.json())
  18.     .catch((err) => console.error(`errir`, err));
  19.   console.log(`debug1`);
  20.   console.log(`[Response spravka delete]`, response);
  21. };
  22.  
  23. <tbody>
  24.   {results[buttons.txt[buttons.is]] &&
  25.     results[buttons.txt[buttons.is]].map((result, idx) => (
  26.       <tr key={result._id}>
  27.         {console.log(`id`, result)}
  28.         <td>
  29.           <div className='flex items-center space-x-3'>№{idx}</div>
  30.         </td>
  31.         {Object.keys(result).map((e, indx) => {
  32.           if (e !== '_id' && e !== '__v') {
  33.             return (
  34.               <td key={indx}>
  35.                 <div className='flex items-center space-x-3'>{result[e]}</div>
  36.               </td>
  37.             );
  38.           }
  39.           return null;
  40.         })}
  41.         <th>
  42.           <button className="btn btn-ghost btn-xs">edit</button>
  43.           <button className='btn btn-ghost btn-xs' onClick={() => handleDelete(result._id, idx)}>
  44.             delete
  45.           </button>
  46.         </th>
  47.       </tr>
  48.     ))}
  49. </tbody>
  50.  
Advertisement
Add Comment
Please, Sign In to add comment