Single row value change is reflected in all the drawers

Viewed 30

I have two drawer components and inside those drawer user can select maximum 3 rows. Each row has a Accessory Qty property which cannot be greater than Max Qty. Two Drawers, each with clickHandler which triggers inner row sections
Two Drawers, each with clickHandler which triggers inner row sections

Rows inside a drawer, user can add 1 or more rows
Each row has Accessory Qty and Max Qty. as mentioned above. selecting all
Selecting all the rows and clicking Associate to all which adds the selected rows in the other drawers. You can see in the breadcrumbs that currently I'm in SET F. Now after going back to Image one, when I click the 2nd drawer which is SET H I can see that the selected Items are added here. set H
Now if I change any value in any of the rows of SET H the same change is reflected in the SET F enter image description here
enter image description here

This is the whole problem, I want the changes not to reflect in other drawers.

For this data is getting fetched from the API. I'm setting a copy of the data in the backupdata. Then with the index and values I'm changing the values in the corresponding rows.

handleQtyChange

  const handleQtyChange = (event, i) => {
    console.log("Set Index QTY", index, i);
    const backupdata = [...billOfMaterials];
    var data = event.target.value;
    if (backupdata[index].accessory_details[i].accessory[0].code !== "") {

      var accesory_code =
        backupdata[index].accessory_details[i].accessory[0].code;
      var max_qty = backupdata[index].accessory_details[i].max_qty;
      var updated_qty = event.target.value;

      if (parseFloat(updated_qty) > parseFloat(max_qty)) {
        setMessageDialog({
          isOpen: true,
          message: "Qty must be less than or equal to Max qty",
          severity: "error",
        });
      } else {
        if (data == "" || (!isNaN(+data) && parseFloat(data) >= 0)) {
          console.log("AccessoryChildIndex", i);
          console.log("Max qty", accessoryListWithMaxQty);
          console.log("Set Index", index);
          setMessageDialog({
            isOpen: false,
            message: "",
            severity: "error",
          });
          var accessory_total_qty_index = backupdata[
            index
          ].accessory_total_qty.findIndex(
            (x) =>
              x.code == backupdata[index].accessory_details[i].accessory[0].code
          );
          console.log("accessory_total_qty Index", accessory_total_qty_index);
          backupdata[index].accessory_total_qty[accessory_total_qty_index].qty =
            updated_qty;
          backupdata[index].accessory_details[i].accessory_qty = updated_qty;
          backupdata[index].accessory_details[i].total = (
            parseFloat(
              event.target.value == undefined || event.target.value == ""
                ? 0
                : event.target.value
            ) * parseFloat(backupdata[index].accessory_details[i].rate)
          ).toFixed(2);
          backupdata[index].grand_total = grandTotal();
          setBillOfMaterials(backupdata);
          if (!checkBomAccessoryWiseTotalQty(accesory_code, max_qty)) {
            setMessageDialog({
              isOpen: true,
              message: "Qty limit exceed max qty would be " + max_qty,
              severity: "error",
            });
            backupdata[index].accessory_total_qty[
              accessory_total_qty_index
            ].qty = 0;
            backupdata[index].accessory_details[i].accessory_qty = 0;
            backupdata[index].accessory_details[i].total = 0;
            backupdata[index].grand_total = grandTotal();
            setBillOfMaterials(backupdata);
          }
          console.log("Change BOM", billOfMaterials);
        }
      }
    }
  };

handleAssociateToAll

  const handleAssociateToAll = () => {
    //var data = [...billOfMaterials];
    var backupdata = [...billOfMaterials];
    //var backupdata = JSON.parse(JSON.stringify([...billOfMaterials]));

    var selectedData = backupdata[index].accessory_details.filter((el) => {
      return el.is_selected == true;
    });
    console.log(selectedData);
    console.log(index);
    backupdata.map((el, i) => {
      if (index != i) {
        console.log(selectedData);
        selectedData.map((dt) => {
          dt.accessory_qty = "0";
          dt.is_selected = false;
          let checkAccessory = backupdata[i].accessory_details.find((data) => {
            return (
              data.accessory[0].code != "" &&
              data.accessory[0].code == dt.accessory[0].code
            );
          });
          console.log("Check", checkAccessory);
          if (checkAccessory == undefined) {
            backupdata[i].accessory_details = [
              dt,
              ...backupdata[i].accessory_details,
            ];
          }
        });
        backupdata[index].accessory_total_qty &&
          backupdata[index].accessory_total_qty.map((qty) => {
            qty.qty = "0";
            let checkTotalQty = backupdata[i].accessory_total_qty.find(
              (data) => {
                return data.code == qty.code;
              }
            );
            if (checkTotalQty == undefined) {
              backupdata[i].accessory_total_qty = [
                ...backupdata[i].accessory_total_qty,
                qty,
              ];
            }
          });
      }
    });
    setBillOfMaterials(backupdata);
    checkAllSelected();
  };
  console.log("bill", billOfMaterials);

I guess I've explained well. So how do I keep the other drawer's data unchanged and change only the selected drawers rows.

The api response After clicking Associate to all: enter image description here

The overall bill, the selected rows are added in accessory_total_qty: enter image description here

0 Answers
Related