/
ContactListHelper.js
93 lines (92 loc) · 4.3 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
({
// 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);
}
})