-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.ts
266 lines (230 loc) · 10.2 KB
/
contacts.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import {CRUD} from "./CRUD.js";
import { DataModel } from "./DataModel.js";
var tempName:any,tempEmail:any,tempPhone:any,tempLandline:any,tempWebsite:any,tempAddress:any
var crudobj=new CRUD();
var globalID:any;
var isName:boolean=false,isEmail:boolean=false,isPhone:boolean=false
var nameBox:any,emailBox:any,phoneBox:any;
document.querySelector("body").addEventListener('load',window.onload=function(){
loadData();
})
document.getElementById('add-btn').addEventListener('click',function(){
displayForm();
})
document.getElementById('contact-name').addEventListener('change',function(){
validateName();
})
document.getElementById('contact-email').addEventListener('change',function(){
validateEmail();
})
document.getElementById('contact-phone').addEventListener('change',function(){
validatePhone();
})
document.getElementById('form-add-btn').addEventListener('click',function(){
addRecord();
})
document.querySelector('.edit-img').addEventListener('click',function(){
onEdit();
})
document.getElementById('form-save-btn').addEventListener('click',function(){
updateChanges();
})
document.querySelector('.delete-img').addEventListener('click',function(){
deleteRecord();
})
function loadData(){
var detailsContainer:any=document.getElementById("contact-list");
var localData:any="";
var currentData:any="";
let storedData=async ()=>crudobj.fetchData().then((data)=>{
data.map((val:any)=>{
// console.log(val.id);
currentData=`<div class='contact-details-list' style="border:2px solid black " id='${val.id}'>
<p class='namebox'>${val.name}</p>
<p class='details'>${val.email}</p>
<p class='details'>${val.phone}</p>
</div>`
localData+=currentData
})
detailsContainer.innerHTML = localData;
document.querySelectorAll('.contact-details-list').forEach(x=>{
x.addEventListener('click',function(e:Event){
document.querySelectorAll('.contact-details-list').forEach(y=>{
(y as any).style.backgroundColor='white'
});
(x as any).style.backgroundColor='rgb(206,231,242)'
crudobj.fetchDataWithId(globalID=(e.currentTarget as any).id).then(data=>{
ondisplay(data.name,data.email,data.phone,data.landline,data.website,data.address);
})
})
});
})
storedData();
}
function createContact(name:string,email:string,phone:string,landline:string,website:string,address:string){
crudobj.postData(new DataModel(name,email,phone,landline,website,address));
loadData();
var contactList:any=document.getElementById('contact-list')
var contactDetails:any=document.createElement('div')
contactDetails.setAttribute('class','contact-details-list')
nameBox=document.createElement('p')
nameBox.setAttribute('class','namebox')
nameBox.innerText=name;
emailBox=document.createElement('p')
emailBox.setAttribute('class','details')
emailBox.innerText=email;
phoneBox=document.createElement('p')
phoneBox.setAttribute('class','details')
phoneBox.innerText=phone;
contactDetails.appendChild(nameBox)
contactDetails.appendChild(emailBox)
contactDetails.appendChild(phoneBox)
contactList?.appendChild(contactDetails)
loadData();
}
function onEdit(){
var userdetails:any=document.getElementById('details-title')
userdetails.style.display='none'
var getForm:any=document.getElementById('formdetails')
getForm.style.display='block';
var addBtn:any=document.getElementById('form-add-btn');
addBtn.style.display='none';
var saveBtn:any=document.getElementById('form-save-btn');
saveBtn.style.display='block';
crudobj.fetchDataWithId(globalID).then(data=>{
console.log(data);
(<HTMLInputElement>document.getElementById('contact-name')).value=data.name;
(<HTMLInputElement>document.getElementById('contact-email')).value=data.email;
(<HTMLInputElement>document.getElementById('contact-phone')).value=data.phone;
(<HTMLInputElement>document.getElementById('contact-landline')).value=data.landline;
(<HTMLInputElement>document.getElementById('contact-website')).value=data.website;
(<HTMLInputElement>document.getElementById('contact-address')).value=data.address;
})
}
function ondisplay(name:string,email:string,phone:string,landline:string,website:string,address:string){
var userdetails:any=document.getElementById('details-title')
userdetails.style.display='block'
var getForm:any=document.getElementById('formdetails')
getForm.style.display='none';
tempName=document.getElementById('insertName')
tempName.innerText=name
tempEmail=document.getElementById('user-email')
tempEmail.innerText="Email: "+email
tempPhone=document.getElementById('user-phone')
tempPhone.innerText="Phone: "+phone
tempLandline=document.getElementById('user-landline')
tempLandline.innerText="Landline: "+landline
tempWebsite=document.getElementById('user-website')
tempWebsite.innerText="Website: "+website
tempAddress=document.getElementById('user-address')
tempAddress.innerText=address
}
function displayForm(){
(<any>document.getElementById('contact_form')).reset()
var userdetails:any=document.getElementById('details-title')
userdetails.style.display='none'
var getForm:any=document.getElementById('formdetails')
getForm.style.display='block';
var addBtn:any=document.getElementById('form-add-btn');
addBtn.style.display='block';
var saveBtn:any=document.getElementById('form-save-btn');
saveBtn.style.display='none';
}
function validateName(){
isName=false
var formName=(<HTMLInputElement>document.getElementById('contact-name')).value;
if(formName.length===0)
(<any>document.getElementById("warning-name")).innerText='*Name is required';
else if(formName.length>0){
(<any>document.getElementById("warning-name")).innerText='';
isName=true;
}
}
function validateEmail(){
isEmail=false
var formEmail=(<HTMLInputElement>document.getElementById('contact-email')).value;
if(formEmail.length===0)
(<any>document.getElementById("warning-email")).innerText='*Email is required';
else if(formEmail.length>0)
{
if(/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/.test(formEmail)){
(<any>document.getElementById("warning-email")).innerText='';
isEmail=true;
}
else{
(<any>document.getElementById("warning-email")).innerText='* valid email is required';
}
}
}
//
function validatePhone(){
isPhone=false
var formPhone:any=(<HTMLInputElement>document.getElementById('contact-phone')).value;
if(formPhone.length===0)
(<any>document.getElementById("warning-phone")).innerText='*Phone is required';
else if(formPhone.length>0)
{
if(!isNaN(formPhone)&&formPhone.split('+91').pop().length===10){
isPhone=true;
(<any>document.getElementById("warning-phone")).innerText='';
}
else{
(<any>document.getElementById("warning-phone")).innerText='* valid phone is required';
}
}
}
function addRecord(){
// console.log(isName,isEmail,isPhone);
if(isName&&isEmail&&isPhone){
var formName=(<HTMLInputElement>document.getElementById('contact-name')).value
var formEmail=(<HTMLInputElement>document.getElementById('contact-email')).value
var formPhone=(<HTMLInputElement>document.getElementById('contact-phone')).value
var formLandline=(<HTMLInputElement>document.getElementById('contact-landline')).value
var formWebsite=(<HTMLInputElement>document.getElementById('contact-website')).value
var formAddress=(<HTMLInputElement>document.getElementById('contact-address')).value
createContact(formName,formEmail,formPhone,formLandline,formWebsite,formAddress)
alert("successfully added");
(<any>document.getElementById('contact_form')).reset()
var getForm:any=document.getElementById('formdetails')
getForm.style.display='none';
}
else{
if(!isPhone)
(<any>document.getElementById("warning-phone")).innerText='*Phone is required';
if(!isEmail)
(<any>document.getElementById("warning-email")).innerText='*Email is required';
if(!isName)
(<any>document.getElementById("warning-name")).innerText='*Name is required';
}
}
function updateChanges(){
// // console.log(isName,isEmail,isPhone)
if(isName&&isEmail&&isPhone){
var name=(<HTMLInputElement>document.getElementById('contact-name')).value
var email=(<HTMLInputElement>document.getElementById('contact-email')).value
var phone=(<HTMLInputElement>document.getElementById('contact-phone')).value
var landline=(<HTMLInputElement>document.getElementById('contact-landline')).value
var website=(<HTMLInputElement>document.getElementById('contact-website')).value
var address=(<HTMLInputElement>document.getElementById('contact-address')).value
//console.log("value sent "+ name,email,phone);
crudobj.updateData(globalID,new DataModel(name,email,phone,landline,website,address))
alert('saved changes successfully');
(<any>document.getElementById('contact_form')).reset()
var getForm:any=document.getElementById('formdetails')
getForm.style.display='none';
loadData();
}
else{
if(!isPhone)
(<any>document.getElementById("warning-phone")).innerText='*Valid Phone is required';
if(!isEmail)
(<any>document.getElementById("warning-email")).innerText='*valid Email is required';
if(!isName)
(<any>document.getElementById("warning-name")).innerText='*Name is required';
}
}
function deleteRecord(){
crudobj.deleteData(globalID);
alert("Item deleted with ID: "+globalID)
loadData();
}