Skip to content

Commit

Permalink
selectors added
Browse files Browse the repository at this point in the history
  • Loading branch information
kharchenkok committed Dec 13, 2023
1 parent 58045eb commit c9efc6d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/components/ContactForm/ContactForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
colorPickerOptions,
DEFAULT_COLOR,
} from '../../constans/ColorConstans';
import { contactsSelector } from '../../store/contacts/contactsSelectors';
import style from './ContactForm.module.css';

export default function ContactForm() {
const dispatch = useDispatch();
const { contacts } = useSelector(state => state.contacts);
const { contacts } = useSelector(contactsSelector);

const handleSubmit = event => {
event.preventDefault();
Expand Down
6 changes: 4 additions & 2 deletions src/components/ContactList/ContactList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { BiSolidUserRectangle, BiPhone } from 'react-icons/bi';
import ColorFilter from '../Filter/ColorFilter';
import Notification from '../Notification';
import { showInfo } from '../../utils/ToastNotification';
import { contactsSelector } from '../../store/contacts/contactsSelectors';
import { filterSelector } from '../../store/filter/filterSelector';
import style from './ContactList.module.css';

const ContactList = () => {
const dispatch = useDispatch();
const { contacts } = useSelector(state => state.contacts);
const { nameFilter, colorFilter } = useSelector(state => state.filter);
const { contacts } = useSelector(contactsSelector);
const { nameFilter, colorFilter } = useSelector(filterSelector);

const filteredContacts = contacts.filter(
contact =>
Expand Down
11 changes: 6 additions & 5 deletions src/components/Filter/ColorFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { useDispatch, useSelector } from 'react-redux';
import React, { useState } from 'react';
import { setColorFilterAction } from '../../store/filter/filterSlice';
import { DEFAULT_COLOR } from '../../constans/ColorConstans';
import { contactsSelector } from '../../store/contacts/contactsSelectors';

import style from './Filter.module.css';

const ColorFilter = () => {
const dispatch = useDispatch();
const [activeColor, setActiveColor] = useState('');
const { contacts } = useSelector(contactsSelector);

const { contacts } = useSelector(state => state.contacts);
const [activeColor, setActiveColor] = useState('');

const uniqueColors = [...new Set(contacts.map(contact => contact.color))];

Expand All @@ -35,7 +36,7 @@ const ColorFilter = () => {
type={'button'}
value={''}
className={makeActiveButtonClass('')}
onClick={event => handleColorFilter(event)}
onClick={handleColorFilter}
>
All
{contacts.length > 0 && <span>({contacts.length})</span>}
Expand All @@ -50,7 +51,7 @@ const ColorFilter = () => {
value={`${color}`}
style={{ backgroundColor: ` ${color}` }}
className={makeActiveButtonClass(color)}
onClick={event => handleColorFilter(event)}
onClick={handleColorFilter}
></button>
)
)}
Expand All @@ -60,7 +61,7 @@ const ColorFilter = () => {
style={{ backgroundColor: DEFAULT_COLOR }}
value={DEFAULT_COLOR}
className={makeActiveButtonClass(DEFAULT_COLOR)}
onClick={event => handleColorFilter(event)}
onClick={handleColorFilter}
>
Other
</button>
Expand Down
1 change: 1 addition & 0 deletions src/store/contacts/contactsSelectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const contactsSelector = state => state.contacts;
1 change: 1 addition & 0 deletions src/store/filter/filterSelector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const filterSelector = state => state.filter;

0 comments on commit c9efc6d

Please sign in to comment.