Skip to content

Commit

Permalink
Add error alert (react-hot-toast)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stylize-K committed Aug 11, 2023
1 parent b79d251 commit e134e9e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Filter } from './Filter/Filter';
import { ContactList } from './ContactList/ContactList';
import { useSelector } from 'react-redux';
import { getContactsCount } from 'redux/selectors';
import { Toaster } from 'react-hot-toast';
import css from './App.module.css';

export const App = () => {
Expand All @@ -29,6 +30,7 @@ export const App = () => {
</p>
<Filter />
<ContactList />
<Toaster position="top-right" />
</div>
);
};
5 changes: 2 additions & 3 deletions src/components/ContactForm/ContactForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { nanoid } from '@reduxjs/toolkit';
import { useSelector, useDispatch } from 'react-redux';
import { addContact } from 'redux/contactsSlice';
import { getContacts } from 'redux/selectors';
import toast from 'react-hot-toast';
import css from './ContactForm.module.css';

//Регулярні вирази для валідації відповідних полів форми
Expand Down Expand Up @@ -45,10 +46,9 @@ export const ContactForm = () => {

//Функція обробки сабміту форми - додавання нового контакту в стор при сабміті форми
const formSubmitHandler = data => {
console.log(data);
//Заборона додавати контакти, імена яких вже присутні у телефонній книзі.
if (contacts.some(contact => contact.name === data.name)) {
alert(`${data.name} is already in contacts.`);
toast.error(`${data.name} is already in contacts.`);
return;
}
dispatch(
Expand All @@ -58,7 +58,6 @@ export const ContactForm = () => {

//Функція сабміту форми
const handleSubmit = (values, { resetForm }) => {
console.log(values);
formSubmitHandler(values);
resetForm();
};
Expand Down

0 comments on commit e134e9e

Please sign in to comment.