Skip to content

Commit 1673288

Browse files
committed
add contacts app
1 parent 0c08055 commit 1673288

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

contacts-app/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<div id="contact-editor">
11+
Name: <input name="name" type="text"> <br>
12+
Phone: <input name="phone" type="text"> <br>
13+
Photo <input name="photo" type="text"> <br>
14+
<button onclick="addContact()">Save Contact</button>
15+
</div>
16+
17+
<hr>
18+
19+
<!-- <button>Add New Contact</button> -->
20+
21+
<div id="contacts">
22+
23+
</div>
24+
25+
<script src="index.js"></script>
26+
</body>
27+
</html>

contacts-app/index.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const contactsState = []
2+
3+
4+
function renderContacts (state) {
5+
return state.map((contact, index) => {
6+
return contact.editMode ? `
7+
<div class="contact-card-editor">
8+
Name: <input name="name" type="text" value="${contact.name}"> <br>
9+
Phone: <input name="phone" type="text" value="${contact.phone}"> <br>
10+
Photo <input name="photo" type="text" value="${contact.photo}"> <br>
11+
12+
<button data-action="save" data-index="${index}"> Save </button>
13+
</div>
14+
` : `
15+
<div class="contact-card">
16+
<p> Name: ${contact.name} </p>
17+
<p> Phone: ${contact.phone} </p>
18+
<p> Photo: ${contact.photo} </p>
19+
20+
<button data-action="delete" data-index="${index}"> Delete </button>
21+
<button data-action="edit" data-index="${index}"> Edit </button>
22+
</div>
23+
`
24+
}).reduce((acc, val) => acc + val, '')
25+
}
26+
27+
function paintContacts () {
28+
document.getElementById('contacts').innerHTML = renderContacts(contactsState)
29+
}
30+
31+
// paintContacts()
32+
33+
function addContact (e) {
34+
const inputElements = [...document.querySelectorAll('#contact-editor > input')]
35+
const inputs = reduceStateFromInputElements(inputElements)
36+
contactsState.push(inputs)
37+
paintContacts()
38+
}
39+
40+
const reduceStateFromInputElements = inputElements =>
41+
inputElements.reduce((acc, val) => ({
42+
...acc,
43+
[val.name]: [val.value]
44+
}), {})
45+
46+
47+
48+
document.getElementById('contacts').addEventListener('click', e => {
49+
// const {index, action} = e.target.dataset
50+
const index = e.target.dataset.index
51+
const action = e.target.dataset.action
52+
53+
// console.log(action, index)
54+
55+
if (!action) {
56+
return ;
57+
}
58+
59+
switch (action) {
60+
case 'delete':
61+
contactsState.splice(index, 1)
62+
break
63+
case 'edit':
64+
contactsState[index].editMode = true
65+
break;
66+
case 'save':
67+
const inputNodes = [...e.target.parentNode.querySelectorAll('input')]
68+
const contactState = reduceStateFromInputElements(inputNodes)
69+
contactsState[index] = contactState
70+
break;
71+
}
72+
73+
paintContacts()
74+
})

0 commit comments

Comments
 (0)