0

i have a issue, i have two inputs then and i can't permit the user save this edit if the two values is equals.

my state who contains a data from db

const [doctorRegisters, setDoctorResgisters] = useState<any>([]);

inside this array i contain this

[{"__typename": "DoctorMedicalRegister", "counsil": "CRM", "id": "141", "isMainRegister": true, "register": "1234567/RS"}, {"__typename": "DoctorMedicalRegister", "counsil": "CRM", "id": "153", "isMainRegister": false, "register": "1234567/RS"}]

and i need compare two register who if is equal, i cant permit user save he save just is different

here is a code who i try fix this

  const isEquals = () => {
    doctorRegisters.map((item: any) => {
      if (item.register) {
        doctorRegisters.map((item2: any) => {
          if (item2.register) {
            if (item.register === item2.register) {
              console.log('iguais')
            }
          }
        });
      }
    });
  };

but this work only for dont edited values i need verify when this value is changed in input in this case i only verify in db this values is equals

here is my handle change

  const handleEditRegisterCrm = (crm: string, id: number) => {
    setDoctorResgisters(
      doctorRegisters.map((item: any) => {
        if (item && Number(item.id) == id) {
          item.register = `${crm}/${item.register?.split('/')[1] || ''}`;
        }
        return item;
      }),
    );
  };
5
  • What does "dont edited values" means ? Commented Oct 26, 2022 at 18:53
  • Array.prototype.indexOf() can also work as a way to check if the value exists within an array. In this case, you can do something like if (doctorRegisters.indexOf(value) !==-1) ... Commented Oct 26, 2022 at 18:53
  • @ElenaAlexeenko Probably unedited values/checks once and doesnt care for edited values Commented Oct 26, 2022 at 18:54
  • @ElenaAlexeenko "dont edited values" is a values when i change this inside my input when i edit this values and save again this dont validate the new value just a value who have before this Commented Oct 26, 2022 at 19:09
  • @Archigan yes i tried use indexOf but didn't work in this case i don't know why. Commented Oct 26, 2022 at 19:11

1 Answer 1

1

You could do something like:

const handleEditRegisterCrm = (crm: string, id: number) => {
  if (!doctorRegisters.some((doctorRegister) => doctorRegister.register.includes(registerToCompare)) {
    setDoctorRegisters(
      doctorRegisters.map((item: any) => {
        if (item && Number(item.id) == id) {
          item.register = `${crm}/${item.register?.split('/')[1] || ''}`;
        }
        return item;
      }),
    );
  } 
};

Remember you should keep track of the registerToCompare in order to find if it's already inserted in the doctorRegisters list. I'm assuming you can obtain that value from the your handleChange function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.