Skip to content

Commit 749103d

Browse files
Merge pull request #25 from mctekk/feat/clickable-header
Improve initial load
2 parents 9518837 + 380f13a commit 749103d

File tree

1 file changed

+52
-49
lines changed

1 file changed

+52
-49
lines changed

src/components/chat/index.vue

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -68,106 +68,106 @@ export default {
6868
ChatMessager,
6969
ChatSide,
7070
ChatLogin,
71-
ChatLoading,
71+
ChatLoading
7272
},
7373
props: {
7474
tokenField: {
7575
type: String,
76-
default: "channel_owner_token",
76+
default: "channel_owner_token"
7777
},
7878
displayFull: {
7979
type: Boolean,
80-
default: false,
80+
default: false
8181
},
8282
endpoint: {
8383
type: String,
84-
required: true,
84+
required: true
8585
},
8686
receiver: {
87-
type: String,
87+
type: String
8888
},
8989
showHeader: {
9090
type: Boolean,
91-
default: true,
91+
default: true
9292
},
9393
showChannelList: {
9494
type: Boolean,
95-
default: false,
95+
default: false
9696
},
9797
showSuggestButton: {
9898
type: Boolean,
99-
default: true,
99+
default: true
100100
},
101101
httpOptions: {
102102
type: Object,
103103
default() {
104104
return {};
105-
},
105+
}
106106
},
107107
httpMethod: {
108-
type: Function,
108+
type: Function
109109
},
110110
isExpanded: {
111111
type: Boolean,
112-
default: false,
112+
default: false
113113
},
114114
externalChannelHandle: {
115115
type: Boolean,
116-
default: false,
116+
default: false
117117
},
118118
user: {
119119
type: String,
120-
default: "",
121-
},
120+
default: ""
121+
}
122122
},
123123
data() {
124124
return {
125125
client: null,
126126
channels: [],
127127
activeChannel: null,
128-
userContext: { identity: null },
128+
userContext: { identity: null }
129129
};
130130
},
131131
watch: {
132132
receiver() {
133133
this.unlistenEvents();
134-
},
134+
}
135135
},
136136
computed: {
137137
isLoggedIn() {
138138
return this.userContext.identity;
139-
},
139+
}
140140
},
141141
beforeDestroy() {
142142
this.unlistenEvents();
143143
},
144144
methods: {
145-
createClient(data) {
146-
Twilio.Client.create(data[this.tokenField], {
147-
logLevel: "info",
148-
}).then((client) => {
149-
this.userContext = {
150-
...data,
151-
identity: client.user.identity,
152-
user: client.user,
153-
};
154-
this.client = client;
155-
156-
client.on("tokenAboutToExpire", this.onTokenAboutToExpire);
157-
this.loadChannelEvents(client);
158-
this.updateChannels();
145+
async createClient(data) {
146+
const client = await Twilio.Client.create(data[this.tokenField], {
147+
logLevel: "info"
159148
});
149+
150+
this.userContext = {
151+
...data,
152+
identity: client.user.identity,
153+
user: client.user
154+
};
155+
this.client = client;
156+
157+
client.on("tokenAboutToExpire", this.onTokenAboutToExpire);
158+
this.loadChannelEvents(client);
159+
this.updateChannels();
160160
},
161161
162162
async loadChannel() {
163163
if (!this.activeChannel && !this.showChannelList) {
164-
const channel = this.channels.find((channel) => {
165-
return channel.sid == this.userContext.channel_sid;
166-
});
167-
168-
if (channel) {
169-
this.joinChannel(channel);
170-
}
164+
this.client
165+
.getChannelBySid(this.userContext.channel_sid)
166+
.then(channel => {
167+
if (channel) {
168+
this.joinChannel(channel);
169+
}
170+
});
171171
}
172172
},
173173
@@ -193,13 +193,16 @@ export default {
193193
channel.join();
194194
}
195195
196-
const subscribed = await this.client
197-
.getSubscribedChannels({ limit: 100 })
198-
.then((page) => {
199-
return this.appendChannels(page, []);
200-
});
201-
this.channels = subscribed;
202-
this.loadChannel();
196+
if (!this.showChannelList) {
197+
this.loadChannel();
198+
} else {
199+
const subscribed = await this.client
200+
.getSubscribedChannels({ limit: 100 })
201+
.then(page => {
202+
return this.appendChannels(page, []);
203+
});
204+
this.channels = subscribed;
205+
}
203206
},
204207
205208
async appendChannels(paginator, current) {
@@ -217,7 +220,7 @@ export default {
217220
},
218221
219222
loadChannelEvents(client) {
220-
client.on("channelJoined", (channel) => {
223+
client.on("channelJoined", channel => {
221224
channel.on("messageAdded", () => {
222225
this.updateChannels();
223226
});
@@ -233,7 +236,7 @@ export default {
233236
234237
unlistenEvents() {
235238
if (this.client) {
236-
this.client.removeListener("channelJoined", (channel) => {
239+
this.client.removeListener("channelJoined", channel => {
237240
channel.removeListener("messageAdded", () => {
238241
this.updateChannels();
239242
});
@@ -270,8 +273,8 @@ export default {
270273
271274
sendMessage(message, attributes) {
272275
this.$refs.messenger.sendMessage(message, attributes);
273-
},
274-
},
276+
}
277+
}
275278
};
276279
</script>
277280

0 commit comments

Comments
 (0)