@meetelise/chat 1.20.48 → 1.20.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/MEChat.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import axios from "axios";
1
2
  import { StyleInfo } from "lit/directives/style-map";
2
3
  import { ThemeIdString } from "./themes";
3
4
  import "./WebComponent/me-chat";
@@ -17,21 +18,29 @@ import { MEChat as MEChatLitElement } from "./WebComponent/me-chat";
17
18
  */
18
19
  export default class MEChat {
19
20
  static meChat: MEChatLitElement | null = null;
21
+ static orgSlug = "";
22
+ static mutationObserver: MutationObserver | null = null;
23
+ static previousUrl = "";
24
+ static hasBuildingSlug: boolean | null = null;
20
25
 
21
- static start(opts: Options): void {
26
+ static start(opts: Options, isInitialStart = true): void {
22
27
  // If clients are Yardi and have a resident portal with rentcafe,
23
- // we do NOT want to display the launcher (if following in shows up anywhere in url)
28
+ // we do NOT want to display the launcher (if following in shows up anywhere in url).
24
29
  if (
25
30
  window.location.pathname.includes("residentservices") ||
26
31
  window.location.pathname.includes("onlineleasing")
27
32
  ) {
28
33
  return;
29
34
  }
35
+ if (isInitialStart) {
36
+ this.orgSlug = opts.organization;
37
+ this.hasBuildingSlug = !!opts.building;
38
+ }
30
39
  installFont();
31
40
  installTalkjsMobileStyleFix();
32
41
  const meChat = document.createElement("me-chat");
33
42
  meChat.setAttribute("orgSlug", opts.organization);
34
- meChat.setAttribute("buildingSlug", opts.building);
43
+ this.handleBuildingslug(meChat, opts.building);
35
44
  meChat.setAttribute("class", "meetelise-chat");
36
45
  if (opts.themeId) {
37
46
  meChat.setAttribute("themeId", opts.themeId);
@@ -64,6 +73,62 @@ export default class MEChat {
64
73
  document.body.removeChild(this.meChat);
65
74
  MEChat.meChat = null;
66
75
  }
76
+
77
+ static async handleBuildingslug(
78
+ meChat: MEChatLitElement,
79
+ buildingSlug?: string
80
+ ): Promise<void> {
81
+ if (buildingSlug) {
82
+ meChat.setAttribute("buildingSlug", buildingSlug);
83
+ return;
84
+ }
85
+
86
+ if (this.mutationObserver) {
87
+ return;
88
+ }
89
+
90
+ this.handleSingleFamilyUrl();
91
+ this.previousUrl = window.location.href;
92
+
93
+ document.body.addEventListener(
94
+ "click",
95
+ () => {
96
+ requestAnimationFrame(() => {
97
+ if (
98
+ this.previousUrl === window.location.href ||
99
+ this.hasBuildingSlug
100
+ ) {
101
+ return;
102
+ }
103
+ this.previousUrl = window.location.href;
104
+ this.handleSingleFamilyUrl();
105
+ });
106
+ },
107
+ true
108
+ );
109
+ }
110
+
111
+ static async handleSingleFamilyUrl(): Promise<void> {
112
+ if (this.hasBuildingSlug) {
113
+ return;
114
+ }
115
+ this.remove();
116
+ const url = window.location.href;
117
+ const requestUrl = `https://app.meetelise.com/eliseCrmApi/webchat/microsite_slug?uri=${encodeURIComponent(
118
+ url
119
+ )}`;
120
+ const buildingSlug = await axios.get<string | null>(requestUrl, {
121
+ headers: { ["org-slug"]: this.orgSlug },
122
+ });
123
+ if (!buildingSlug || !buildingSlug.data) {
124
+ return;
125
+ }
126
+ const opts = {
127
+ organization: this.orgSlug,
128
+ building: buildingSlug.data,
129
+ };
130
+ this.start(opts, false);
131
+ }
67
132
  }
68
133
 
69
134
  export interface Options {