diff --git a/README.md b/README.md index f2e24ba..9b01d82 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ The lookup component provides the following features: 2. **Create the `LookupDefinition` record** - Go to Setup > Custom Metadata Types > LookupDefinition > add a new record + Go to Setup > Custom Metadata Types > click in Manage Records (LookupDefinition) > add a new record **Label**: `String`
**ApiName**: `String`
@@ -143,6 +143,7 @@ public List getDefaultSearchResults(Map payload) { return result; } ``` +**RECOMMENDED**: `selectedIds` *is the Id selected, use in `getSelection()` method and `getSearchResults()` method* ## Work with contexts using `payload` @@ -170,7 +171,8 @@ singleLookupResult.recordPayload = new Map { ```js handleChange(event) { const value = event.detail.value;// ["someId", "someId2", "someId3"] - const payload = event.detail.payload;// {"someId": {type: nurse, name : johana}, someId2: {ty...}} + const payload = { someId : event.detail.value };// {"someId": {type: nurse, name : johana}, someId2: {ty...}} + //this create a copy of the payload of the id consulted // TODO: do something with the selection } ``` diff --git a/src-lookup-sample/main/default/classes/AccountContactLookup.cls b/src-lookup-sample/main/default/classes/AccountContactLookup.cls new file mode 100644 index 0000000..c3409c3 --- /dev/null +++ b/src-lookup-sample/main/default/classes/AccountContactLookup.cls @@ -0,0 +1,98 @@ +public class AccountContactLookup implements LookupResult.ILookupResult { + + public final static Map CONTACT_ICON = new Map{ + 'iconName' => 'standard:contact' + }; + + public List getSelection(List selectedIds, Map payload) { + + String accountId = (String) payload.get('accountId'); + List result = new List(); + + if (accountId == null) { + return result; + } + + List contacts = [ + SELECT Name, Id, Phone + FROM Contact + WHERE AccountId = :accountId + AND Id IN: selectedIds + ]; + + for (Contact contact : contacts) { + result.add(buildResult(contact)); + } + + return result; + } + + public List getSearchResults( + String searchTerm, + List selectedIds, + Map payload) { + + String accountId = (String) payload.get('accountId'); + List result = new List(); + + if (accountId == null) { + return result; + } + + searchTerm = '%' + searchTerm + '%'; + + List contacts = [ + SELECT Name, Email, Phone + FROM Contact + WHERE + (Name LIKE :searchTerm + OR Phone LIKE :searchTerm + OR Email LIKE :searchTerm) + AND AccountId = :accountId + AND Id NOT IN :selectedIds + ]; + + for (Contact contact : contacts) { + result.add(buildResult(contact)); + } + + return result; + } + + public List getDefaultSearchResults(Map payload) { + + String accountId = (String) payload.get('accountId'); + List result = new List(); + + if (accountId == null) { + return result; + } + + List contacts = [ + SELECT Name, id, Phone + FROM Contact + WHERE AccountId = :accountId + LIMIT 10 + ]; + + for (Contact contact : contacts) { + result.add(buildResult(contact)); + } + + return result; + } + + private LookupResult buildResult(Contact contact) { + + LookupResult result = new LookupResult(); + result.id = contact.Id; + result.icon = CONTACT_ICON; + result.title = contact.Name; + result.recordPayload = new Map { + 'name' => contact.Name, + 'phone' => contact.Phone + }; + + return result; + } +} \ No newline at end of file diff --git a/src-lookup-sample/main/default/classes/AccountContactLookup.cls-meta.xml b/src-lookup-sample/main/default/classes/AccountContactLookup.cls-meta.xml new file mode 100644 index 0000000..019e850 --- /dev/null +++ b/src-lookup-sample/main/default/classes/AccountContactLookup.cls-meta.xml @@ -0,0 +1,5 @@ + + + 59.0 + Active + \ No newline at end of file diff --git a/src-lookup-sample/main/default/classes/AccountContactLookupTest.cls b/src-lookup-sample/main/default/classes/AccountContactLookupTest.cls new file mode 100644 index 0000000..5cd62f5 --- /dev/null +++ b/src-lookup-sample/main/default/classes/AccountContactLookupTest.cls @@ -0,0 +1,56 @@ +@IsTest +private class AccountContactLookupTest { + @IsTest + static void shouldCreateLookupResults() { + // DATA + Account account = new Account(Name = 'account name test'); + insert account; + + Contact contact = new Contact( + FirstName = 'FirstName', + LastName = 'LastName', + Phone = '123456789', + Email = 'FirstName.LastName@example.com', + HasOptedOutOfEmail = false, + AccountId = account.Id + ); + insert contact; + + Map payload = new Map(); + payload.put('accountId', account.Id); + + // TEST + Test.startTest(); + AccountContactLookup accountContactLookup = new AccountContactLookup(); + List selectionResult = AccountContactLookup.getSelection( + new List{ contact.id }, + payload + ); + List defaultSearchResultsResult = AccountContactLookup.getDefaultSearchResults( + payload + ); + List searchResultsResult = AccountContactLookup.getSearchResults( + '123', + new List(), + payload + ); + Test.stopTest(); + + // ASSERT + Assert.areEqual( + selectionResult[0].id, + contact.id, + 'should get the selection' + ); + Assert.areEqual( + defaultSearchResultsResult[0].id, + contact.id, + 'should set getDefaultResults from recentlyViewed' + ); + Assert.areEqual( + searchResultsResult[0].id, + contact.id, + 'filters contacts using searchKey' + ); + } +} diff --git a/src-lookup-sample/main/default/classes/AccountContactLookupTest.cls-meta.xml b/src-lookup-sample/main/default/classes/AccountContactLookupTest.cls-meta.xml new file mode 100644 index 0000000..7d5f9e8 --- /dev/null +++ b/src-lookup-sample/main/default/classes/AccountContactLookupTest.cls-meta.xml @@ -0,0 +1,5 @@ + + + 61.0 + Active + \ No newline at end of file diff --git a/src-lookup-sample/main/default/classes/AccountLookup.cls b/src-lookup-sample/main/default/classes/AccountLookup.cls new file mode 100644 index 0000000..4cf8f97 --- /dev/null +++ b/src-lookup-sample/main/default/classes/AccountLookup.cls @@ -0,0 +1,62 @@ +public class AccountLookup implements LookupResult.ILookupResult { + + public final static Map ACCOUNT_ICON = new Map{ + 'iconName' => 'standard:account' + }; + + public List getSelection(List selectedIds, Map payload) { + + List result = new List(); + + for (Account account : [SELECT Id, Name FROM Account WHERE Id IN: selectedIds]) { + result.add(buildResult(account)); + } + + return result; + } + + public List getSearchResults( + String searchTerm, + List selectedIds, + Map payload) { + + List result = new List(); + searchTerm = '%' + searchTerm + '%'; + + for (Account account : [ + SELECT Id, Name + FROM Account + WHERE + Name LIKE :searchTerm + AND Id NOT IN :selectedIds + ]) { + result.add(buildResult(account)); + } + + return result; + } + + public List getDefaultSearchResults(Map payload) { + + List result = new List(); + + for (Account account : [SELECT Id, Name FROM Account LIMIT 10]) { + result.add(buildResult(account)); + } + + return result; + } + + private LookupResult buildResult(Account account) { + + LookupResult result = new LookupResult(); + result.id = account.Id; + result.icon = ACCOUNT_ICON; + result.title = account.Name; + result.recordPayload = new Map { + 'name' => account.Name + }; + + return result; + } +} \ No newline at end of file diff --git a/src-lookup-sample/main/default/classes/AccountLookup.cls-meta.xml b/src-lookup-sample/main/default/classes/AccountLookup.cls-meta.xml new file mode 100644 index 0000000..019e850 --- /dev/null +++ b/src-lookup-sample/main/default/classes/AccountLookup.cls-meta.xml @@ -0,0 +1,5 @@ + + + 59.0 + Active + \ No newline at end of file diff --git a/src-lookup-sample/main/default/classes/AccountLookupTest.cls b/src-lookup-sample/main/default/classes/AccountLookupTest.cls new file mode 100644 index 0000000..ccf4601 --- /dev/null +++ b/src-lookup-sample/main/default/classes/AccountLookupTest.cls @@ -0,0 +1,46 @@ +@IsTest +private class AccountLookupTest { + @IsTest + static void shouldCreateLookupResults() { + // DATA + Account account = new Account(Name = 'account name test'); + insert account; + + Map payload = new Map(); + payload.put('name', account.Name); + + // TEST + Test.startTest(); + AccountLookup accountLookup = new AccountLookup(); + List selectionResult = AccountLookup.getSelection( + new List{ account.Id }, + payload + ); + List defaultSearchResultsResult = AccountLookup.getDefaultSearchResults( + payload + ); + List searchResultsResult = AccountLookup.getSearchResults( + 'accou', + new List(), + payload + ); + Test.stopTest(); + + // ASSERT + Assert.areEqual( + selectionResult[0].Id, + account.Id, + 'should get the selection' + ); + Assert.areEqual( + defaultSearchResultsResult[0].Id, + account.Id, + 'should set getDefaultResults from recentlyViewed' + ); + Assert.areEqual( + searchResultsResult[0].Id, + account.Id, + 'filters contacts using searchKey' + ); + } +} diff --git a/src-lookup-sample/main/default/classes/AccountLookupTest.cls-meta.xml b/src-lookup-sample/main/default/classes/AccountLookupTest.cls-meta.xml new file mode 100644 index 0000000..7d5f9e8 --- /dev/null +++ b/src-lookup-sample/main/default/classes/AccountLookupTest.cls-meta.xml @@ -0,0 +1,5 @@ + + + 61.0 + Active + \ No newline at end of file diff --git a/src-lookup-sample/main/default/lwc/workingWithPayloads/__tests__/workingWithPayloads.test.js b/src-lookup-sample/main/default/lwc/workingWithPayloads/__tests__/workingWithPayloads.test.js new file mode 100644 index 0000000..0e8085a --- /dev/null +++ b/src-lookup-sample/main/default/lwc/workingWithPayloads/__tests__/workingWithPayloads.test.js @@ -0,0 +1,52 @@ +import { createElement } from 'lwc'; +import WorkingWithPayloads from 'c/workingWithPayloads'; + +describe('c-working-with-payloads', () => { + afterEach(() => { + // The jsdom instance is shared across test cases in a single file so reset the DOM + while (document.body.firstChild) { + document.body.removeChild(document.body.firstChild); + } + }); + + it('this should show a text with account name when selected the account and contact name and phone when selected contact', async () => { + // Arrange + const element = createElement('c-working-with-payloads', { + is: WorkingWithPayloads + }); + + document.body.appendChild(element); + + const accountChangeEvent = new CustomEvent('change', { + detail: { + value: ['accountId'], + payload: { + 'accountId': { name: 'Account name' } + } + } + }); + + const contactChangeEvent = new CustomEvent('change', { + detail: { + value: ['contactId'], + payload: { + 'contactId': { name: 'Contact Name', phone: 'Phone' } + } + } + }); + + // Act + const accountLookup = element.shadowRoot.querySelector('c-lookup[data-id="accountLookup"]'); + accountLookup.dispatchEvent(accountChangeEvent); + + const contactLookup = element.shadowRoot.querySelector('c-lookup[data-id="contactLookup"]'); + contactLookup.dispatchEvent(contactChangeEvent); + + await Promise.resolve(); + // Assert + const selectedAccount = element.shadowRoot.querySelector('p[data-id="selectedAccount"]'); + const selectedContact = element.shadowRoot.querySelector('p[data-id="selectedContact"]'); + expect(selectedAccount.textContent).toBe('Your account selected was Account name'); + expect(selectedContact.textContent).toBe('Your contact selected was Contact Name and its phone number is Phone'); + }); +}); \ No newline at end of file diff --git a/src-lookup-sample/main/default/lwc/workingWithPayloads/workingWithPayloads.html b/src-lookup-sample/main/default/lwc/workingWithPayloads/workingWithPayloads.html new file mode 100644 index 0000000..eaaa296 --- /dev/null +++ b/src-lookup-sample/main/default/lwc/workingWithPayloads/workingWithPayloads.html @@ -0,0 +1,40 @@ + diff --git a/src-lookup-sample/main/default/lwc/workingWithPayloads/workingWithPayloads.js b/src-lookup-sample/main/default/lwc/workingWithPayloads/workingWithPayloads.js new file mode 100644 index 0000000..1b4a66e --- /dev/null +++ b/src-lookup-sample/main/default/lwc/workingWithPayloads/workingWithPayloads.js @@ -0,0 +1,32 @@ +import { LightningElement, track } from "lwc"; +import { NavigationMixin } from "lightning/navigation"; + +export default class WorkingWithPayloads extends NavigationMixin(LightningElement) { + + @track contactPayload; + accountValue; + contactValue; + selectedAccount; + selectedContact; + + handleAccountChange(event) { + + this.contactValue = []; + this.selectedAccount = undefined; + this.selectedContact = undefined; + this.accountValue = event.detail.value[0]; + this.contactPayload = { accountId: event.detail.value[0] }; + this.selectedAccount = event.detail.payload[this.accountValue]; + } + + handleContactChange(event) { + + this.selectedContact = undefined; + this.contactValue = event.detail.value[0]; + this.selectedContact = event.detail.payload[this.contactValue]; + } + + get hasNotAccount() { + return !this.accountValue; + } +} \ No newline at end of file diff --git a/src-lookup-sample/main/default/lwc/workingWithPayloads/workingWithPayloads.js-meta.xml b/src-lookup-sample/main/default/lwc/workingWithPayloads/workingWithPayloads.js-meta.xml new file mode 100644 index 0000000..2719e4d --- /dev/null +++ b/src-lookup-sample/main/default/lwc/workingWithPayloads/workingWithPayloads.js-meta.xml @@ -0,0 +1,8 @@ + + + 61.0 + true + + lightning__AppPage + + \ No newline at end of file diff --git a/src-lookup/main/default/lwc/lookup/lookup.js b/src-lookup/main/default/lwc/lookup/lookup.js index 54114d0..1b8f76c 100644 --- a/src-lookup/main/default/lwc/lookup/lookup.js +++ b/src-lookup/main/default/lwc/lookup/lookup.js @@ -140,7 +140,10 @@ export default class SobjectLookup extends LightningElement { new CustomEvent("change", { detail: { value, - payload: this._value.map(({ id, payload }) => ({ id, payload })) + payload: this._value.reduce((map, { id, recordPayload }) => { + map[id] = recordPayload; + return map; + }, {}) } }) );