-
Notifications
You must be signed in to change notification settings - Fork 178
/
reservationList.js
99 lines (87 loc) · 2.75 KB
/
reservationList.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
import { LightningElement, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getOpenReservations from '@salesforce/apex/reservationManagerController.getOpenReservations';
import TILE_SELECTION_MC from '@salesforce/messageChannel/Tile_Selection__c';
import FLOW_STATUS_CHANGE_MC from '@salesforce/messageChannel/Flow_Status_Change__c';
import {
subscribe,
unsubscribe,
APPLICATION_SCOPE,
MessageContext,
publish
} from 'lightning/messageService';
export default class ReservationList extends LightningElement {
wiredResult;
reservationSelected = false;
selectedRecId;
_records = [];
reservations = [];
errorMsg;
msgForUser;
noRecords = false;
subscription = null;
@wire(MessageContext)
messageContext;
subscribeToMessageChannel() {
this.subscription = subscribe(
this.messageContext,
FLOW_STATUS_CHANGE_MC,
(message) => this.handleMessage(message),
{ scope: APPLICATION_SCOPE }
);
}
unsubscribeToMessageChannel() {
unsubscribe(this.subscription);
this.subscription = null;
}
handleMessage(message) {
if (
message.flowName === 'spaceDesigner' &&
message.status === 'FINISHED'
) {
refreshApex(this.wiredResult);
}
}
connectedCallback() {
this.subscribeToMessageChannel();
}
disconnectedCallback() {
this.unsubscribeToMessageChannel();
}
@wire(getOpenReservations)
wiredReservations(value) {
this.wiredResult = value;
if (value.error) {
this.errorMsg = value.error;
this.msgForUser = 'There was an issue loading reservations.';
} else if (value.data) {
if (value.data.length) {
this._records = [...value.data];
this.handleMute();
if (this.noRecords) this.noRecords = false;
} else {
this.noRecords = true;
}
}
}
handleMute() {
if (!this.reservationSelected) {
let muted = '';
this.reservations = this._records.map((record) => {
if (this.selectedRecId) {
this.reservationSelected = true;
muted = record.Id === this.selectedRecId ? false : true;
} else {
muted = false;
}
return { record, muted };
});
}
}
handleSelectEvent(event) {
this.selectedRecId = event.detail.reservationId;
const payload = { tileType: 'reservation', properties: event.detail };
publish(this.messageContext, TILE_SELECTION_MC, payload);
this.handleMute();
}
}