|
| 1 | +import { LightningElement } from 'lwc'; |
| 2 | +import { createListInfo } from 'lightning/uiListsApi'; |
| 3 | + |
| 4 | +export default class CreateListInfo extends LightningElement { |
| 5 | + objectApiName = 'Account'; |
| 6 | + listViewApiName; |
| 7 | + displayColumns; |
| 8 | + filterLogicString; |
| 9 | + filteredByInfo; |
| 10 | + label; |
| 11 | + listShares; |
| 12 | + scope; |
| 13 | + visibility; |
| 14 | + |
| 15 | + isValidJson = true; |
| 16 | + |
| 17 | + handleChange(event) { |
| 18 | + const element = event.target; |
| 19 | + if (event.detail) { |
| 20 | + // Dropdown |
| 21 | + this[element.name] = event.detail.value; |
| 22 | + } else { |
| 23 | + // Other inputs |
| 24 | + this[element.name] = element.value; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + handleJsonInputChange(event) { |
| 29 | + const { name, value } = event.target; |
| 30 | + this[name] = value; |
| 31 | + try { |
| 32 | + if (value && value !== '') { |
| 33 | + JSON.parse(value); |
| 34 | + } |
| 35 | + event.target.setCustomValidity(''); |
| 36 | + this.checkJsonTextAreasValidity(); |
| 37 | + } catch (error) { |
| 38 | + event.target.setCustomValidity('Invalid JSON'); |
| 39 | + this.isValidJson = false; |
| 40 | + } |
| 41 | + event.target.reportValidity(); |
| 42 | + } |
| 43 | + |
| 44 | + async handleSendRequest() { |
| 45 | + this.dispatchEvent(new CustomEvent('request', { bubbles: true })); |
| 46 | + |
| 47 | + const input = { |
| 48 | + objectApiName: this.objectApiName, |
| 49 | + listViewApiName: this.listViewApiName, |
| 50 | + displayColumns: this.displayColumns, |
| 51 | + filterLogicString: this.filterLogicString, |
| 52 | + label: this.label, |
| 53 | + visibility: this.visibility |
| 54 | + }; |
| 55 | + if (this.isSharedVisibility && this.listShares) { |
| 56 | + input.listShares = JSON.parse(this.listShares); |
| 57 | + } |
| 58 | + if (this.filteredByInfo) { |
| 59 | + input.filteredByInfo = JSON.parse(this.filteredByInfo); |
| 60 | + } |
| 61 | + if (this.filteredByInfo) { |
| 62 | + input.scope = JSON.parse(this.scope); |
| 63 | + } |
| 64 | + try { |
| 65 | + const response = await createListInfo(input); |
| 66 | + this.dispatchEvent( |
| 67 | + new CustomEvent('response', { |
| 68 | + detail: response, |
| 69 | + bubbles: true |
| 70 | + }) |
| 71 | + ); |
| 72 | + } catch (error) { |
| 73 | + this.dispatchEvent( |
| 74 | + new CustomEvent('response', { |
| 75 | + detail: { error }, |
| 76 | + bubbles: true |
| 77 | + }) |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + checkJsonTextAreasValidity() { |
| 83 | + let isValidJson = true; |
| 84 | + const textAreas = this.template.querySelectorAll('lightning-textarea'); |
| 85 | + textAreas.forEach((textArea) => { |
| 86 | + if (!textArea.checkValidity()) { |
| 87 | + isValidJson = false; |
| 88 | + } |
| 89 | + }); |
| 90 | + this.isValidJson = isValidJson; |
| 91 | + } |
| 92 | + |
| 93 | + get isCallApiButtonDisabled() { |
| 94 | + return !( |
| 95 | + this.objectApiName && |
| 96 | + this.listViewApiName && |
| 97 | + this.isValidJson |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + get isSharedVisibility() { |
| 102 | + return this.visibility === 'Shared'; |
| 103 | + } |
| 104 | + |
| 105 | + get visibilityOptions() { |
| 106 | + return [ |
| 107 | + { label: 'Private', value: 'Private' }, |
| 108 | + { label: 'Public', value: 'Public' }, |
| 109 | + { label: 'Shared', value: 'Shared' } |
| 110 | + ]; |
| 111 | + } |
| 112 | +} |
0 commit comments