|
| 1 | +import React from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import uuid from 'uuid/v4'; |
| 4 | +import { Row, Button } from '../components'; |
| 5 | +import { |
| 6 | + showEditor, delContact, |
| 7 | + filterFavorites, resetFilter, edit |
| 8 | +} from '../actions'; |
| 9 | +import './contacts.less'; |
| 10 | + |
| 11 | + |
| 12 | +const Contacts = ({ show, filter, contacts, dispatch }) => { |
| 13 | + if (show) { return null; } |
| 14 | + |
| 15 | + const onAddClick = () => { |
| 16 | + dispatch(showEditor()); |
| 17 | + dispatch(edit({ |
| 18 | + id: uuid() |
| 19 | + })); |
| 20 | + }; |
| 21 | + |
| 22 | + const onToggleFilerClick = () => { |
| 23 | + if (filter === 'SHOW_ALL') { |
| 24 | + dispatch(filterFavorites()); |
| 25 | + } else { |
| 26 | + dispatch(resetFilter()); |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + return (<div className="contacts"> |
| 31 | + <Row><Button className="add-contact" label="Add Contact" onClick={onAddClick} /></Row> |
| 32 | + |
| 33 | + <table> |
| 34 | + <tbody> |
| 35 | + <tr> |
| 36 | + <th> |
| 37 | + <Button |
| 38 | + label={filter === 'SHOW_ALL' ? 'Filter Favorites' : 'Show All'} |
| 39 | + onClick={() => onToggleFilerClick()} |
| 40 | + /> |
| 41 | + </th> |
| 42 | + <th>Name</th> |
| 43 | + <th>Phone</th> |
| 44 | + <th>Email</th> |
| 45 | + <th>Operations</th> |
| 46 | + </tr> |
| 47 | + {contacts.map(el => (<tr key={el.id}> |
| 48 | + <td className="favorite"> |
| 49 | + {el.isFavorite ? '★' : '☆' } |
| 50 | + </td> |
| 51 | + <td>{el.name}</td> |
| 52 | + <td>{el.phone}</td> |
| 53 | + <td>{el.email}</td> |
| 54 | + <td> |
| 55 | + <Button |
| 56 | + label="Edit" |
| 57 | + onClick={() => { |
| 58 | + dispatch(showEditor()); |
| 59 | + dispatch(edit(el)); |
| 60 | + }} |
| 61 | + /> |
| 62 | + |
| 63 | + {' '} |
| 64 | + |
| 65 | + <Button |
| 66 | + label="Del" |
| 67 | + onClick={() => { |
| 68 | + dispatch(delContact(el.id)); |
| 69 | + }} |
| 70 | + /> |
| 71 | + </td> |
| 72 | + </tr>))} |
| 73 | + </tbody> |
| 74 | + </table> |
| 75 | + </div>); |
| 76 | +}; |
| 77 | + |
| 78 | + |
| 79 | +export default connect(state => ({ |
| 80 | + show: state.editor.get('show'), |
| 81 | + filter: state.filter, |
| 82 | + contacts: state.contacts.map((val, id) => ({ |
| 83 | + id, |
| 84 | + isFavorite: state.favorites.has(id), |
| 85 | + ...val |
| 86 | + })).filter((val, id) => { |
| 87 | + if (state.filter === 'FAVORITES_ONLY') { return state.favorites.has(id); } |
| 88 | + return true; |
| 89 | + }).toArray() |
| 90 | +}))(Contacts); |
0 commit comments