/
ContactListHelper.js
162 lines (160 loc) · 7.58 KB
/
ContactListHelper.js
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
({
// Function to fetch data from server called in initial loading of page
fetchContacts : function(component, event, helper) {
// Assign server method to action variable
var action = component.get("c.getContactList");
// Getting the account id from page
var accountId = component.get("v.recordId");
// Setting parameters for server method
action.setParams({
accountIds: accountId
});
// Callback function to get the response
action.setCallback(this, function(response) {
// Getting the response state
var state = response.getState();
// Check if response state is success
if(state === 'SUCCESS') {
// Getting the list of contacts from response and storing in js variable
var contactList = response.getReturnValue();
// Set the list attribute in component with the value returned by function
component.set("v.contactList",contactList);
}
else {
// Show an alert if the state is incomplete or error
alert('Error in getting data');
}
});
// Adding the action variable to the global action queue
$A.enqueueAction(action);
},
// Function to update the contacts on server
saveContacts: function(component, event, helper) {
// Getting the contact list from lightning component
var contactList = component.get("v.contactList");
// Getting the recordViewForm and recordEditForm component
var recordViewForm = component.find('recordViewForm');
var recordEditForm = component.find('recordEditForm');
// Initializing the toast event to show toast
var toastEvent = $A.get('e.force:showToast');
// Defining the action to save contact List ( will call the saveContactList apex controller )
var saveAction = component.get("c.saveContactList");
// setting the params to be passed to apex controller
saveAction.setParams({ contactList: contactList });
// callback action on getting the response from server
saveAction.setCallback(this, function(response) {
// Getting the state from response
var state = response.getState();
if(state === 'SUCCESS') {
// Getting the response from server
var dataMap = response.getReturnValue();
// Checking if the status is success
if(dataMap.status=='success') {
// Remove the formHide class
$A.util.removeClass(recordViewForm,'formHide');
// Add the formHide class
$A.util.addClass(recordEditForm,'formHide');
// Getting the button element
var btn = event.getSource();
// Setting the label and name of button back to edit
btn.set('v.name','edit');
btn.set('v.label','Edit');
// Setting the success toast which is dismissable ( vanish on timeout or on clicking X button )
toastEvent.setParams({
'title': 'Success!',
'type': 'success',
'mode': 'dismissable',
'message': dataMap.message
});
// Fire success toast event ( Show toast )
toastEvent.fire();
}
// Checking if the status is error
else if(dataMap.status=='error') {
// Setting the error toast which is dismissable ( vanish on timeout or on clicking X button )
toastEvent.setParams({
'title': 'Error!',
'type': 'error',
'mode': 'dismissable',
'message': dataMap.message
});
// Fire error toast event ( Show toast )
toastEvent.fire();
}
}
else {
// Show an alert if the state is incomplete or error
alert('Error in getting data');
}
});
$A.enqueueAction(saveAction);
},
// Function to delete the contacts from server
removeContacts: function(component, event, helper) {
// Getting the deleteContact Component
var contactsToDelete = component.find("deleteContact");
// Initialize an empty array
var idsToDelete = [];
// Checking if contactsToDelete is an array
if(contactsToDelete.length!=undefined) {
// Iterating the array to get contact ids
for(var i=0;i<contactsToDelete.length;i++) {
// If contact has delete checkbox checked, add contact id to list of ids to delete
if(contactsToDelete[i].get("v.checked"))
idsToDelete.push(contactsToDelete[i].get("v.value"));
}
} else {
// if contactsToDelete is not an array but single object,
// check if delete checkbox is checked and push id to list of ids to delete
if(contactsToDelete.get("v.checked"))
idsToDelete.push(contactsToDelete.get("v.value"));
}
// Initializing the toast event to show toast
var toastEvent = $A.get('e.force:showToast');
// Defining the action to delete contact List ( will call the deleteContactList apex controller )
var deleteAction = component.get('c.deleteContactList');
// setting the params to be passed to apex controller
deleteAction.setParams({
contactIds: idsToDelete
});
// callback action on getting the response from server
deleteAction.setCallback(this, function(response) {
// Getting the state from response
var state = response.getState();
if(state === 'SUCCESS') {
// Getting the response from server
var dataMap = response.getReturnValue();
// Checking if the status is success
if(dataMap.status=='success') {
// Setting the success toast which is dismissable ( vanish on timeout or on clicking X button )
toastEvent.setParams({
'title': 'Success!',
'type': 'success',
'mode': 'dismissable',
'message': dataMap.message
});
// Fire success toast event ( Show toast )
toastEvent.fire();
window.location.reload();
}
// Checking if the status is error
else if(dataMap.status=='error') {
// Setting the error toast which is dismissable ( vanish on timeout or on clicking X button )
toastEvent.setParams({
'title': 'Error!',
'type': 'error',
'mode': 'dismissable',
'message': dataMap.message
});
// Fire error toast event ( Show toast )
toastEvent.fire();
}
}
else {
// Show an alert if the state is incomplete or error
alert('Error in getting data');
}
});
$A.enqueueAction(deleteAction);
}
})