Witam,
Działam w React native i mam problem z sprawdzeniem, czy wprowadzana wartość email nie istnieje już czasem w tabeli, jeśli tak to nie dodawać i wyświetlić powiadomienie. Zacząłem powoli kombinować, ale niestety bez większych skutków. Poniżej dodaje to co mam aktualnie.
addSingleUser = (e) => {
e.preventDefault();
let name = this.firstNameInp.value;
let email = this.emailInp.value;
let errorCont = document.getElementById("errorSm");
let errorSmall = document.getElementById("errorContentSm");
let submitBtn = document.getElementById("submitBtn");
let i = this.state.contacts.length - 1;
let emailUp = this.emailInp.value.toUpperCase();
for (i; i >= 0; i--) {
let arrUp = this.state.contacts[i].email.toUpperCase();
if (arrUp.includes(emailUp)) {
document.getElementById("error").style.display = "flex";
document.querySelector(".alertICont").style.border = "2px solid #f00";
document.querySelector(".errorCont .alertICont span").style.color = "#f00";
document.getElementById("errorContent").innerText = "E-mail already on list";
this.setState({
isDoubled: 1
});
this.inputToggle();
break;
} else {
}
}
if (this.state.contacts.length < 10) {
this.firstNameInp.value = '';
this.emailInp.value = '';
document.getElementById('btnR').style.display = 'none';
document.getElementById("emptyInfo").style.display = "none";
const users = Object.assign([], this.state.contacts);
users.push({
name: name,
email: email
});
this.setState({
contacts: users
});
document.getElementById("add").style.animation = "btnAdd .3s";
//show error
document.getElementById("error").style.display = "flex";
document.querySelector(".alertICont").style.border = "2px solid #04ab6b";
document.querySelector(".errorCont .alertICont span").style.color = "#04ab6b";
document.getElementById("errorContent").innerText = "User added";
fetch(this.state.url, {
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({ name: name, email: email })
})
.then(resp => resp.json())
.then(data => console.log(data))
.catch(error => console.log("error: ", error))
this.inputToggle();
} else {
submitBtn.setAttribute("disabled", "disabled");
errorCont.style.display = "block";
errorSmall.innerText = "List has reached maximum";
setTimeout(function () {
submitBtn.removeAttribute("disabled");
errorCont.style.display = "none";
errorSmall.innerText = "";
}, 3000)
this.setState({
emailDoubled: false
});
}
}