javascript – How do I keep a single incrementCounter for the 2 form submissions?

Everything works perfectly fine. However, I just want to know if there’s a way for these codes to be shorter and non-repetitive and how? Thank you.

I have these increment counters to update the stocks after submitting them.

The selectedItem:

<SelectItem
     value={selectedItem}
     onChange={handleChange}
     items={items}
  />

The first submit:

function incrementCounter() {
    const collRef = firestore
      .collection("items")
      .where("item", "==", selectedItem);

    collRef.get().then(async (qSnap) => {
      const batch = firestore.batch();

      qSnap.docs.forEach((doc) => {
        batch.update(doc.ref, {
          stocks: firebase.firestore.FieldValue.increment(-1),
        });
      });

      await batch.commit();
    });
  }

  const handleSubmit = (e) => {
    e.preventDefault();
    try {
      const userRef = firestore.collection("users").doc(id);
      const ref = userRef.set(
        {
         ...data here
        },

        { merge: true }
      );

      console.log(" saved");
      incrementCounter();
    } catch (err) {
      console.log(err);
    }
  };

Now at the second submit. I have created another increment counter since the selectedItem will be empty since I’m only showing the data of the selectedItem in the screen rather than having a value of it. So, instead of a similar select from above. I just used this textfield in which value is taken from users:

const (users, setUsers) = useState(()); 

  useEffect(() => {
    const unsubscribe = firestore
      .collection("users")
      .doc(id)
      .onSnapshot((snapshot) => {
        const user = ();
        user.push({
          ...snapshot.data(),
        });

        setUsers(user);
      });

    return () => {
      unsubscribe();
    };
  }, ());


{users && users.map((user) => {
  <TextField
  type="text"
  value={user.items.selectedItem}
  disabled={true}
/>
})}

The 2nd submit:

let ar2 = ();
  const userItems2 = users(0)?.items?.selectedItem;
  if (userItems2) {
    for (const (key, value) of Object.entries(userItems2)) {
      ar2.push(value);
    }
  }

  const selected2 = ar2.join("");

  function incrementCounter2() {
    const collRef = firestore
      .collection("items")
      .where("item", "==", selected2);

    collRef.get().then(async (qSnap) => {
      const batch = firestore.batch();

      qSnap.docs.forEach((doc) => {
        batch.update(doc.ref, {
          stocks: firebase.firestore.FieldValue.increment(-1),
        });
      });

      await batch.commit();
    });
  }

  const handleSubmit2 = (e) => {
    e.preventDefault();
    try {
      const userRef = firestore.collection("users").doc(id);
      const ref = userRef.set(
        {
          ...data here
        },
        { merge: true }
      );

      console.log(" saved");
      incrementCounter2();
    } catch (err) {
      console.log(err);
    }
  };