-
Notifications
You must be signed in to change notification settings - Fork 3
/
boatSearchResults.js
119 lines (108 loc) · 3.82 KB
/
boatSearchResults.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
import { api, LightningElement, track, wire } from 'lwc';
import { publish, MessageContext } from 'lightning/messageService';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { refreshApex } from '@salesforce/apex';
import getBoats from '@salesforce/apex/BoatDataService.getBoats';
import updateBoatList from '@salesforce/apex/BoatDataService.updateBoatList';
import BOATMC from '@salesforce/messageChannel/BoatMessageChannel__c';
const SUCCESS_TITLE = 'Success';
const MESSAGE_SHIP_IT = 'Ship it!';
const SUCCESS_VARIANT = 'success';
const ERROR_TITLE = 'Error';
const ERROR_VARIANT = 'error';
export default class BoatSearchResults extends LightningElement {
@api
selectedBoatId;
columns = [
{ label: 'Name', fieldName: 'Name', editable: true },
{ label: 'Length', fieldName: 'Length__c', type: 'number'},
{ label: 'Price', fieldName: 'Price__c', type: 'currency'},
{ label: 'Description', fieldName: 'Description__c'},
];
boatTypeId = '';
@track
boats;
isLoading = false;
@track
draftValues = [];
// wired message context
@wire(MessageContext)
messageContext;
// wired getBoats method
@wire(getBoats, {boatTypeId: '$boatTypeId'})
wiredBoats({data, error}) {
if (data) {
this.boats = data;
} else if (error) {
console.log('data.error')
console.log(error)
}
}
// public function that updates the existing boatTypeId property
// uses notifyLoading
@api
searchBoats(boatTypeId) {
this.isLoading = true;
this.notifyLoading(this.isLoading);
this.boatTypeId = boatTypeId;
}
// this public function must refresh the boats asynchronously
// uses notifyLoading
@api
async refresh() {
this.isLoading = true;
this.notifyLoading(this.isLoading);
await refreshApex(this.boats);
this.isLoading = false;
this.notifyLoading(this.isLoading);
}
// this function must update selectedBoatId and call sendMessageService
updateSelectedTile(event) {
this.selectedBoatId = event.detail.boatId;
this.sendMessageService(this.selectedBoatId)
}
// Publishes the selected boat Id on the BoatMC.
sendMessageService(boatId) {
// explicitly pass boatId to the parameter recordId
publish(this.messageContext, BOATMC, { recordId: boatId });
}
// The handleSave method must save the changes in the Boat Editor
// passing the updated fields from draftValues to the
// Apex method updateBoatList(Object data).
// Show a toast message with the title
// clear lightning-datatable draft values
handleSave(event) {
// notify loading
const updatedFields = event.detail.draftValues;
// Update the records via Apex
updateBoatList({data: updatedFields})
.then(result => {
const toast = new ShowToastEvent({
title: SUCCESS_TITLE,
message: MESSAGE_SHIP_IT,
variant: SUCCESS_VARIANT,
});
this.dispatchEvent(toast);
this.draftValues = [];
return this.refresh();
})
.catch(error => {
const toast = new ShowToastEvent({
title: ERROR_TITLE,
message: error.message,
variant: ERROR_VARIANT,
});
this.dispatchEvent(toast);
})
.finally(() => {
});
}
// Check the current value of isLoading before dispatching the doneloading or loading custom event
notifyLoading(isLoading) {
if (isLoading) {
this.dispatchEvent(new CustomEvent('loading'));
} else {
this.dispatchEvent(CustomEvent('doneloading'));
}
}
}