3

https://codesandbox.io/s/l2qkywz7xl?from-embed=&file=/index.js

Hi referring to above sandbox, I would like to do something to my table as well, how can I add the undo function in my code?

import { toast, ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
toast.configure()
  const deleteCustomer = (ID) => {
    if (window.confirm("Are you sure you want to delete?")) {
      Axios.delete(`http://localhost:3001/stats/delete/${ID}`).then(
        (response) => {
    toast("row deleted");
          setCustomerList(
            customerList.filter((val) => {
              toast("notify");
              console.log("toast notify run");
              return val.ID !== ID;
            })
          );
        }
      );
    }
  };
const displayCustomers = customerList
    .slice(pagesVisited, pagesVisited + customersPerPage)
    .map((val, key) => {
      const dateStr = new Date(val.latest_time_of_visit).toLocaleDateString(
        "en-CA"
      );
      const timeStr = new Date(val.latest_time_of_visit).toLocaleTimeString();
      const dateTime = `${dateStr} ${timeStr}`;
      const my_serial = key + pageNumber * customersPerPage;

      return (
        <tr>
          {/*}
          <td>{val.ID}</td>
      
          <td>{my_serial + 1}</td>
      */}
          <td>{val.name}</td>
          <td>{val.email}</td>
          <td>{val.company_name}</td>
          <td>{val.counts_of_visit}</td>
          <td>{dateTime}</td>
          <td>{val.contacted}</td>
          <td>
            <select
              onChange={(event) => {
                setNewContacted(event.target.value);
              }}
            >
              <option value="" selected disabled hidden>
                Select Yes/No
              </option>
              <option value="Yes">Yes</option>
              <option value="No">No</option>
            </select>
            <button
              className="btn btn-primary"
              onClick={() => {
                updateCustomerContacted(val.ID);
              }}
            >
              Update
            </button>
          </td>
          <td>
            <button
              className="btn btn-danger"
              onClick={() => {
                deleteCustomer(val.ID);
              }}
            >
              Delete
            </button>

So after I press the delete button which calls the deleteCustomer, my toast notfication row deleted comes out, but how can Iadd an undo button to it?

fyceheist
  • 125
  • 4

1 Answers1

1

You forgot to put <ToastContainer /> as stated in the docs for the component to be mounted.

Then you need to put a component in the toast() function as an undo button to put your logic in.

Your code, because its not complete, should be somewhere around like this

import { toast, ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
toast.configure()
  const deleteCustomer = (ID) => {
    if (window.confirm("Are you sure you want to delete?")) {
      Axios.delete(`http://localhost:3001/stats/delete/${ID}`).then(
        (response) => {
    toast("row deleted");
          setCustomerList(
            customerList.filter((val) => {
              toast(<button onClick={() => console.log("your undo logic here"}>UNDO</button>);
              return val.ID !== ID;
            })
          );
        }
      );
    }
  };
const displayCustomers = customerList
    .slice(pagesVisited, pagesVisited + customersPerPage)
    .map((val, key) => {
      const dateStr = new Date(val.latest_time_of_visit).toLocaleDateString(
        "en-CA"
      );
      const timeStr = new Date(val.latest_time_of_visit).toLocaleTimeString();
      const dateTime = `${dateStr} ${timeStr}`;
      const my_serial = key + pageNumber * customersPerPage;

      return (
        <tr>
          {/*}
          <td>{val.ID}</td>
      
          <td>{my_serial + 1}</td>
      */}
          <td>{val.name}</td>
          <td>{val.email}</td>
          <td>{val.company_name}</td>
          <td>{val.counts_of_visit}</td>
          <td>{dateTime}</td>
          <td>{val.contacted}</td>
          <td>
            <select
              onChange={(event) => {
                setNewContacted(event.target.value);
              }}
            >
              <option value="" selected disabled hidden>
                Select Yes/No
              </option>
              <option value="Yes">Yes</option>
              <option value="No">No</option>
            </select>
            <button
              className="btn btn-primary"
              onClick={() => {
                updateCustomerContacted(val.ID);
              }}
            >
              Update
            </button>
          </td>
          <td>
            <button
              className="btn btn-danger"
              onClick={() => {
                deleteCustomer(val.ID);
              }}
            >
              Delete
            </button>
          </td>
          <ToastContainer closeOnClick={false} closeButton={false} />

Robert Tirta
  • 2,593
  • 3
  • 18
  • 37