简体   繁体   English

如何处理 reactjs 中 api 的删除弹出确认

[英]How to handle delete popup confirmation for an api in reactjs

Could anyone help me out here please, all I'm trying to do here is to show popup modal confirmation for delete action, but every time I clicked on **Yes **btn to confirm my delete action the last product on the list always get deleted instead.任何人都可以在这里帮助我,我在这里要做的就是显示删除操作的弹出模式确认,但每次我点击 **是 **btn 来确认我的删除操作列表中的最后一个产品总是而是被删除。 I need help from anyone please?我需要任何人的帮助吗?

Here is my code for handling the delete popup这是我处理删除弹出窗口的代码

    ```
     //OPEN DELETE MODALS
      const [openDeleteModal, isOpenDeleteModal] = useState(false);
      const closeDeleteModal = () => {
        isOpenDeleteModal(false);
        document.body.style.overflow = "unset";
      };
      const showDeleteModal = () => {
        isOpenDeleteModal(true);
      };
    ```
    

and here is the api这是 api

    ```
    //DELETE PRODUCT
      const deleteHandler = async (product) => {
        try {
          await axios.delete(`/api/products/${product._id}`, {
            headers: { Authorization: `Bearer ${userInfo.token}` },
          });
          toast.success("product deleted successfully", {
            position: "bottom-center",
          });
          dispatch({ type: "DELETE_SUCCESS" });
        } catch (err) {
          toast.error(getError(err), { position: "bottom-center" });
          dispatch({ type: "DELETE_FAIL" });
        }
      };
    ```

down here is my modal for confirmation下面是我的确认模式

    ```
     {/* MODAL */}
                                  {openDeleteModal && (
                                    <div className="delete-modal">
                                      <div className="delete-modal-box">
                                        <div className="delete-modal-content">
                                          <p className="delete-modal-content-p">
                                            Are you sure to delete this product?
                                          </p>
                                          <div className="delete-modal-btn">
                                            <button
                                              onClick={closeDeleteModal}
                                              className="delete-modal-btn-close"
                                            >
                                              Close
                                            </button>
                                            <button
                                              onClick={() => {
                                                deleteHandler(product);
                                                closeDeleteModal();
                                              }}
                                              className="delete-modal-btn-yes"
                                            >
                                              {" "}
                                              Yes
                                            </button>
                                          </div>
                                        </div>
                                      </div>
                                    </div>
                                  )}
    ```
    



All I'm trying to do is to be able to delete any product from the list not the last product every time.

here is the entirety of my productList map looks like这是我的全部产品列表 map 看起来像

 {products?.map((product, index) => (
                        <tr className="product-item-list" key={index}>
                          <tr>
                            <td className="product-item-id">{product._id}</td>
                            <td className="product-item-name">
                              {product.name}
                            </td>
                            <td className="product-item-price">
                              £{product.price}
                            </td>
                            <td className="product-item-category">
                              {product.category?.map((cat, index) => (
                                <span key={index}>{cat}</span>
                              ))}
                            </td>

                            <td className="product-item-size">
                              {product.size?.map((s, index) => (
                                <span key={index}>{s}&nbsp;</span>
                              ))}
                            </td>
                            <td className="product-btn-view">
                              <button
                                className="product-btn"
                                onClick={() =>
                                  navigate(`/admin/productedit/${product._id}`)
                                }
                              >
                                Edit
                              </button>
                              &nbsp;
                              <DeleteOutline
                                className="product-delete"
                                onClick={showDeleteModal}
                              />
                              {/* MODAL */}
                              {openDeleteModal && (
                                <div className="delete-modal">
                                  <div className="delete-modal-box">
                                    <div className="delete-modal-content">
                                      <p className="delete-modal-content-p">
                                        Are you sure to delete this product?
                                      </p>
                                      <div className="delete-modal-btn">
                                        <button
                                          onClick={closeDeleteModal}
                                          className="delete-modal-btn-close"
                                        >
                                          Close
                                        </button>
                                        <button
                                          onClick={() => {
                                            deleteHandler(product);
                                            closeDeleteModal();
                                          }}
                                          className="delete-modal-btn-yes"
                                        >
                                          {" "}
                                          Yes
                                        </button>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              )}
                            </td>
                          </tr>
                          <tr></tr>
                        </tr>
                      ))}

I guess it happens because all of your modals open when you call showDeleteModal And the last one is on the top, so when you click to delete the last closure works.我猜这是因为当你调用showDeleteModal时你所有的模态都打开了,最后一个在最上面,所以当你点击删除最后一个闭包时。 Maybe its nessesary to pass id of product into the openDeleteModal .可能需要将产品 ID 传递到openDeleteModal中。 And than check if product.id equals to openDeleteModal .然后检查 product.id 是否等于openDeleteModal

Can you print to console the product.id when you click on the "Delete" button and check is it the correct id of the clicked product?当您单击“删除”按钮并检查它是否是所单击产品的正确 ID 时,您可以打印以控制 product.id 吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM