|
| 1 | +import React from 'react'; |
| 2 | +import ContactInfo from './ContactInfo'; |
| 3 | +import ContactDetails from './ContactDetails'; |
| 4 | +import ContactCreate from './ContactCreate'; |
| 5 | + |
| 6 | +import update from 'react-addons-update'; |
| 7 | + |
| 8 | +export default class Contact extends React.Component { |
| 9 | + |
| 10 | + constructor(props) { |
| 11 | + super(props); |
| 12 | + this.state = { |
| 13 | + selectedKey: -1, |
| 14 | + keyword: '', |
| 15 | + nextId: 4, |
| 16 | + contactData: [{ |
| 17 | + id: 0, |
| 18 | + name: 'Abet', |
| 19 | + phone: '010-0000-0001' |
| 20 | + }, { |
| 21 | + id: 1, |
| 22 | + name: 'Betty', |
| 23 | + phone: '010-0000-0002' |
| 24 | + }, { |
| 25 | + id: 2, |
| 26 | + name: 'Charlie', |
| 27 | + phone: '010-0000-0003' |
| 28 | + }, { |
| 29 | + id: 3, |
| 30 | + name: 'David', |
| 31 | + phone: '010-0000-0004' |
| 32 | + }] |
| 33 | + }; |
| 34 | + |
| 35 | + this.handleChange = this.handleChange.bind(this); |
| 36 | + this.handleClick = this.handleClick.bind(this); |
| 37 | + |
| 38 | + this.handleCreate = this.handleCreate.bind(this); |
| 39 | + this.handleRemove = this.handleRemove.bind(this); |
| 40 | + this.handleEdit = this.handleEdit.bind(this); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + componentWillMount() { |
| 45 | + const contactData = localStorage.contactData; |
| 46 | + const nextId = localStorage.nextId; |
| 47 | + |
| 48 | + if(contactData) { |
| 49 | + this.setState({ |
| 50 | + contactData: JSON.parse(contactData), |
| 51 | + nextId |
| 52 | + }) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + componentDidUpdate(prevProps, prevState) { |
| 57 | + if(JSON.stringify(prevState.contactData) != JSON.stringify(this.state.contactData)) { |
| 58 | + localStorage.contactData = JSON.stringify(this.state.contactData); |
| 59 | + } |
| 60 | + |
| 61 | + if(prevState.nextId !== this.state.nextId){ |
| 62 | + localStorage.nextId = this.state.nextId; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + handleChange(e) { |
| 68 | + this.setState({ |
| 69 | + keyword: e.target.value |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + handleClick(id) { |
| 74 | + this.setState({ |
| 75 | + selectedId: id |
| 76 | + }); |
| 77 | + |
| 78 | + console.log(id, 'is selected'); |
| 79 | + } |
| 80 | + |
| 81 | + handleCreate(contact) { |
| 82 | + contact.id = this.state.nextId; |
| 83 | + |
| 84 | + this.setState({ |
| 85 | + contactData: update(this.state.contactData, { $push: [contact] }), |
| 86 | + nextId: this.state.nextId + 1 |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + handleRemove() { |
| 91 | + if(this.state.selectedId < 0) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + this.setState({ |
| 96 | + contactData: update(this.state.contactData, |
| 97 | + { $splice: [[this.state.selectedId, 1]] } |
| 98 | + ), |
| 99 | + selectedId: -1 |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + handleEdit(name, phone) { |
| 104 | + this.setState({ |
| 105 | + contactData: update(this.state.contactData, |
| 106 | + { |
| 107 | + [this.state.selectedId]: { |
| 108 | + name: { $set: name }, |
| 109 | + phone: { $set: phone } |
| 110 | + } |
| 111 | + } |
| 112 | + ) |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + render() { |
| 117 | + const mapToComponents = (data) => { |
| 118 | + data = data.slice().sort((a,b) => a.name > b.name); |
| 119 | + data = data.filter( |
| 120 | + (contact) => { |
| 121 | + return contact.name.toLowerCase() |
| 122 | + .indexOf(this.state.keyword.toLowerCase()) > -1; |
| 123 | + } |
| 124 | + ); |
| 125 | + return data.map((contact, i) => { |
| 126 | + return (<ContactInfo |
| 127 | + contact={contact} |
| 128 | + key={i} |
| 129 | + onClick={() => this.handleClick(contact.id)}/>); |
| 130 | + }); |
| 131 | + }; |
| 132 | + |
| 133 | + return ( |
| 134 | + <div> |
| 135 | + <h1>Contacts</h1> |
| 136 | + <input |
| 137 | + name="keyword" |
| 138 | + placeholder="Search" |
| 139 | + value={this.state.keyword} |
| 140 | + onChange={this.handleChange} |
| 141 | + /> |
| 142 | + <div>{mapToComponents(this.state.contactData)}</div> |
| 143 | + <ContactDetails |
| 144 | + isSelected={this.state.selectedId != -1} |
| 145 | + contact={this.state.contactData[this.state.selectedId]} |
| 146 | + onRemove={this.handleRemove} |
| 147 | + onEdit={this.handleEdit} |
| 148 | + /> |
| 149 | + <ContactCreate |
| 150 | + onCreate={this.handleCreate} |
| 151 | + /> |
| 152 | + </div> |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
0 commit comments