-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
165 lines (129 loc) · 5.58 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue Contactos</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<div class="container" id="app">
<div class="row mt-5">
<div class="col-12 border-bottom mb-5 d-flex justify-content-between align-items-center">
<h2 :title="title">{{name}}</h2>
<p>{{contactsCount}}</p>
</div>
<div class="col-4">
<form action="">
<div class="form-group mr-1">
<label>Nome</label>
<input type="text" class="form-control" placeholder="Nome Completo..." v-model="contact.name">
</div>
<div class="form-group mr-1">
<label>E-mail</label>
<input type="email" class="form-control" placeholder="Seu melhor email..."
v-model="contact.email">
</div>
<div class="form-group mr-1">
<label>Contacto</label>
<input type="phone" class="form-control" placeholder="Telefone/Celular" v-model="contact.phone">
</div>
<div class="form-group ml-1">
<button v-if="!isEdit" class="btn btn-lg btn-rounded btn-success"
@click.prevent="saveContact(contact)">Criar Contacto</button>
<button v-if="isEdit" class="btn btn-lg btn-rounded btn-secondary"
@click.prevent="updateContact(contact)">Actualizar Contacto</button>
</div>
</form>
</div>
<div class="col-8 border-left">
<div class="contact" v-if="contacts.length">
<div class="col-12 mb-2" v-for="contact in contacts">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{contact.name}}</h5>
<p class="card-text">{{contact.email}}, {{contact.phone}}</p>
<a href="#" class="btn btn-sm btn-primary"
@click.prevent="editContact(contact)">EDITAR</a>
<a href="#" class="btn btn-sm btn-danger"
@click.prevent="removeContact(contact.id)">DELETAR</a>
</div>
</div>
</div>
</div>
<div v-else> Nenhum contacto encontrado!</div>
</div>
</div>
</div>
<!-- LINK DE SCRIPT DE FRAMEWORK VUE.JS -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
let app = new Vue({
el: "#app",
data: {
contacts: [],
name: 'Contactos App',
contact: {
id: '',
name: '',
phone: '',
email: '',
},
isEdit: false,
title: 'Meu primeiro app - VueJS'
},
computed: {
contactsCount() {
return `Total de contactos: ${this.contacts.length}`;
}
},
created() {
this.contacts = JSON.parse(localStorage.getItem('contactsApp'));
},
methods: {
saveContact(contact) {
let contacts = localStorage.getItem('contactsApp');
contact.id = new Date().getTime();
if (contacts) {
contacts = JSON.parse(contacts);
contacts.push(contact);
} else {
contacts = [contact]
}
this.contacts = contacts;
localStorage.setItem('contactsApp', JSON.stringify(contacts))
location.reload();
},
removeContact(contactId) {
console.log("ID: " + contactId)
let contacts = localStorage.getItem('contactsApp');
if (!contacts) return;
contacts = JSON.parse(contacts);
contacts = this.contacts.filter((contact) => {
return contact.id != contactId;
});
this.contacts = contacts;
localStorage.setItem('contactsApp', JSON.stringify(contacts));
},
editContact(contact) {
this.contact = contact;
this.isEdit = true;
},
updateContact(contact) {
let contacts = this.contacts.map(contactMap => {
if (contactMap.id == contact.id) {
console.log("Contacto actualizado com sucesso");
return contact;
}
return contactMap;
})
this.contacts = contacts;
this.isEdit = false;
localStorage.setItem('contactsApp', JSON.stringify(contacts));
location.reload();
}
}
});
</script>
</body>
</html>