Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
986 stevensc 1
import { async } from "postcss-js";
976 stevensc 2
import React, { useCallback } from "react";
1 www 3
import { useState, useEffect } from "react";
976 stevensc 4
import { axios } from "../../utils";
1 www 5
import Contacts from "./contacts/Contacts";
1335 stevensc 6
import NotificationAlert from "../../shared/notification/NotificationAlert";
1 www 7
import Groups from "./groups/Groups";
8
import PersonalChat from "./personal-chat/PersonalChat";
9
 
10
const notifyAudio = new Audio("/audio/chat.mp3");
11
 
12
const Chat = (props) => {
13
  // states
681 steven 14
  const [fullChats, setFullChats] = useState([]);
1 www 15
  const [contacts, setContacts] = useState([]);
16
  const [groups, setGroups] = useState([]);
17
  const [activeChats, setActiveChats] = useState([]);
18
  const [isChatOpen, setIsChatOpen] = useState(false);
19
  const [isMuted, setIsMuted] = useState(false);
20
  const [activeTab, setActiveTab] = useState("user");
669 steven 21
  const defaultChatInterval = 1500;
22
  const [chatInterval, setChatInterval] = useState(defaultChatInterval);
976 stevensc 23
  const [search, setSearch] = useState('');
24
 
994 stevensc 25
  const filtredContacts = contacts.filter(({ name }) => name.toLowerCase().includes(search.toLowerCase()))
26
  const filtredGroups = groups.filter(({ name }) => name.toLowerCase().includes(search.toLowerCase()))
976 stevensc 27
 
1 www 28
  // Time intervals
29
  let heartBeatInterval;
976 stevensc 30
  const handleEntities = entities => {
687 steven 31
    setFullChats([...entities]);
32
    let newUserContacts = [];
33
    let newGroups = [];
34
    entities.map((entity) => {
35
      if (entity.is_open) {
36
        handleOpenConversation(entity, true);
37
      }
38
      if (entity.not_received_messages) {
39
        handleNewMessage(entity);
40
      }
41
      switch (entity.type) {
42
        case "user":
43
          newUserContacts = [...newUserContacts, entity];
44
          handleUpdateOnline(entity);
45
          break;
46
        case "group":
47
          newGroups = [...newGroups, entity];
48
          break;
49
        default:
50
          break;
51
      }
52
    });
53
    setContacts(newUserContacts);
54
    setGroups(newGroups);
55
  }
56
  const heartBeat = async () => {
689 steven 57
    try {
58
      const res = await axios.get("/chat/heart-beat")
681 steven 59
      let entities = [];
689 steven 60
      const resData = res.data;
1 www 61
      if (resData.success) {
62
        entities = resData.data;
689 steven 63
        handleEntities(entities);
1 www 64
      }
689 steven 65
      return entities;
66
    } catch (error) {
976 stevensc 67
      console.log('>>: chat error > ', error)
689 steven 68
    }
687 steven 69
  };
1 www 70
 
976 stevensc 71
 
72
 
1 www 73
  const handleUpdateOnline = (entity) => {
74
    const existingChatId = activeChats.findIndex(
75
      (activeChat) => activeChat.id === entity.id
76
    );
77
    if (existingChatId >= 0) {
78
      if (activeChats[existingChatId].online !== entity.online) {
79
        const newActiveChats = [...activeChats];
80
        newActiveChats[existingChatId].online = entity.online;
81
        setActiveChats(newActiveChats);
82
      }
83
    }
84
  };
85
 
86
  const handleNewMessage = async (unseeEntity) => {
87
    await axios.post(unseeEntity.url_mark_received).then((response) => {
976 stevensc 88
      ('')
1 www 89
    });
90
    if (!activeChats.some((activeChat) => activeChat.id === unseeEntity.id)) {
91
      setActiveChats([...activeChats, { ...unseeEntity, minimized: false }]);
92
      playNotifyAudio();
93
    } else {
94
      const existingChatId = activeChats.findIndex(
95
        (activeChat) => activeChat.id === unseeEntity.id
96
      );
97
      if (!activeChats[existingChatId].unsee_messages) {
98
        const newActiveChats = [...activeChats];
99
        newActiveChats[existingChatId].unsee_messages = true;
100
        setActiveChats(newActiveChats);
101
        playNotifyAudio(newActiveChats[existingChatId].minimized);
102
      }
103
    }
104
  };
105
 
106
  const handleOpenConversation = (entity, minimized = false) => {
978 stevensc 107
    if (activeChats.length < 3 && !activeChats.some((el) => el.id === entity.id)) {
1 www 108
      setActiveChats([...activeChats, { ...entity, minimized: minimized }]);
109
    }
986 stevensc 110
    if (activeChats.length >= 3 && !activeChats.some((el) => el.id === entity.id)) {
111
      activeChats.map((el, index) => {
112
        if (index === 0) {
113
          axios.post(el.url_close)
114
        }
115
      })
116
      const newActiveChats = activeChats.filter((el, index) => index !== 0)
117
      setActiveChats([...newActiveChats, { ...entity, minimized: minimized }]);
118
    }
1 www 119
  };
120
 
121
  const handleReadChat = (index) => {
122
    if (activeChats[index].unsee_messages) {
123
      const newActiveChats = [...activeChats];
124
      newActiveChats[index].unsee_messages = false;
125
      setActiveChats(newActiveChats);
126
    }
127
  };
128
 
129
  const handleMinimizeChat = (chatIndex, minimize) => {
130
    const newActiveChats = [...activeChats];
131
    switch (minimize) {
132
      case false:
133
        newActiveChats[chatIndex].minimized = false;
134
        break;
135
      default:
136
        newActiveChats[chatIndex].minimized =
137
          !newActiveChats[chatIndex].minimized;
138
        break;
139
    }
140
    setActiveChats(newActiveChats);
141
  };
142
 
143
  const handleCloseChat = (entityId, url_close) => {
144
    let newActiveChats = [];
145
    axios.post(url_close).then((response) => {
146
      const resData = response.data;
147
    });
148
    newActiveChats = activeChats.filter(
149
      (activeChat) => activeChat.id !== entityId
150
    );
151
    setActiveChats(newActiveChats);
152
  };
153
 
154
  const playNotifyAudio = (minimized = true) => {
155
    if (!isMuted && minimized) {
156
      notifyAudio.play();
157
    }
158
  };
159
 
160
  const handleMute = () => {
161
    setIsMuted(!isMuted);
162
    if (isMuted) {
163
      notifyAudio.play();
164
    }
165
  };
166
 
167
  const handleChangeTab = (tab) => {
168
    setActiveTab(tab);
169
  };
170
 
171
  useEffect(() => {
691 steven 172
    let entities = [];
1 www 173
    clearInterval(heartBeatInterval);
976 stevensc 174
    heartBeatInterval = setInterval(async () => {
691 steven 175
      entities = await heartBeat() || [];
669 steven 176
    }, chatInterval);
976 stevensc 177
    if ((entities === fullChats) && (chatInterval !== defaultChatInterval * 2)) {
691 steven 178
      clearInterval(heartBeatInterval);
179
      setChatInterval(defaultChatInterval * 2);
180
    }
1 www 181
    return () => {
182
      clearInterval(heartBeatInterval);
183
    };
689 steven 184
  }, [chatInterval, fullChats]);
978 stevensc 185
 
1 www 186
  useEffect(() => {
187
    emojione.imageType = "png";
188
    emojione.sprites = false;
189
    emojione.ascii = true;
190
    emojione.imagePathPNG = props.emojiOnePath;
191
  }, []);
192
 
193
  return window.innerWidth > 1000 ? (
194
    <React.Fragment>
195
      <div id="drupalchat-wrapper">
196
        <div id="drupalchat">
197
          <div className="item-list" id="chatbox_chatlist">
198
            <ul id="mainpanel">
199
              <li id="chatpanel" className="first last">
200
                <div className="subpanel" style={{ display: "block" }}>
201
                  <div
202
                    className="subpanel_title"
203
                    onClick={(e) => {
204
                      if (e.currentTarget === e.target)
205
                        setIsChatOpen(!isChatOpen);
206
                    }}
207
                  >
208
                    <div
209
                      style={{ width: "89%", height: "100%" }}
210
                      id="minmaxchatlist"
211
                      onClick={() => {
212
                        setIsChatOpen(!isChatOpen);
213
                      }}
214
                    >
215
                      <a
216
                        href="/chat"
217
                        className="text-white"
218
                      >
219
                        Chat
220
                      </a>
221
                    </div>
222
                    <span
223
                      className="min localhost-icon-minus-1"
224
                      id="mute-sound"
225
                    >
226
                      <i
976 stevensc 227
                        className={`icon ${isMuted ? "icon-volume-off" : "icon-volume-2"
228
                          } text-20`}
1 www 229
                        aria-hidden="true"
230
                        onClick={handleMute}
231
                      ></i>
232
                    </span>
233
                    {/* <span
234
                      className="min localhost-icon-minus-1"
235
                      id="new-chat-group"
236
                      style={{ marginRight: "5%" }}
237
                      title="Crear grupo"
238
                    >
239
                      <i className="fa fa-edit"></i>
240
                    </span> */}
241
                  </div>
242
                  <div
243
                    id="showhidechatlist"
244
                    style={{ display: isChatOpen ? "block" : "none" }}
245
                  >
246
                    <div
247
                      className="drupalchat_search_main chatboxinput"
248
                      style={{ background: "#f9f9f9" }}
249
                    >
250
                      <div
251
                        className="drupalchat_search"
252
                        style={{ height: "auto" }}
253
                      >
254
                        <input
255
                          className="drupalchat_searchinput live-search-box"
256
                          id="live-search-box"
257
                          placeholder="Buscar"
258
                          size="24"
259
                          type="text"
976 stevensc 260
                          name='search'
261
                          value={search}
262
                          onChange={e => {
263
                            setSearch(e.target.value || '')
264
                          }}
1 www 265
                        />
266
                        <button
267
                          className="searchbutton"
268
                          id="searchbutton"
269
                          title=""
270
                          style={{
271
                            height: "30px",
272
                            border: "none",
273
                            margin: "0px",
274
                            paddingRight: "13px",
275
                            verticalAlign: "middle",
276
                          }}
277
                          type="submit"
278
                        ></button>
279
                        <div
280
                          id="search_result"
281
                          style={{
282
                            textAlign: "center",
283
                            fontSize: "11px",
284
                            display: "none",
285
                          }}
286
                        >
287
                          Sin resultados
288
                        </div>
289
                      </div>
290
                    </div>
291
                    <div
292
                      className="drupalchat_search_main chatboxinput"
293
                      style={{ background: "#f9f9f9" }}
294
                    >
295
                      <div
296
                        style={{
297
                          width: "50%",
298
                          float: "left",
299
                          display: "inline-block",
300
                          padding: "5px",
301
                          textAlign: "center",
302
                          fontSize: "14px",
303
                        }}
304
                      >
305
                        <a
306
                          href="#"
307
                          className="blue-color chat-contacts"
308
                          onClick={(e) => {
309
                            e.preventDefault();
310
                            handleChangeTab("user");
311
                          }}
312
                        >
313
                          Contactos
314
                        </a>
315
                      </div>
316
                      <div
317
                        style={{
318
                          width: "50%",
319
                          display: "inline-block",
320
                          padding: "5px",
321
                          textAlign: "center",
322
                          fontSize: "14px",
323
                        }}
324
                      >
325
                        <a
326
                          href="#"
327
                          className="chat-groups"
328
                          onClick={(e) => {
329
                            e.preventDefault();
330
                            handleChangeTab("group");
331
                          }}
332
                        >
333
                          Grupos
334
                        </a>
335
                      </div>
336
                    </div>
337
                    <div
338
                      className="contact-list chatboxcontent"
339
                      style={{
340
                        display: activeTab === "user" ? "block" : "none",
341
                      }}
342
                    >
343
                      <Contacts
976 stevensc 344
                        contacts={filtredContacts}
1 www 345
                        onOpenConversation={handleOpenConversation}
346
                      />
347
                    </div>
348
                    <div
349
                      className="group-list chatboxcontent"
350
                      style={{
351
                        display: activeTab === "group" ? "block" : "none",
352
                      }}
353
                    >
354
                      <ul id="group-list-ul" className="live-search-list-group">
355
                        <Groups
988 stevensc 356
                          groups={filtredGroups}
1 www 357
                          onOpenConversation={handleOpenConversation}
358
                        />
359
                      </ul>
360
                    </div>
361
                    <div
362
                      className="group-contacts-list chatboxcontent"
363
                      style={{ display: "none" }}
364
                    >
365
                      <div style={{ textAlign: "center", fontSize: "13px" }}>
366
                        Integrantes del grupo
367
                      </div>
368
                      <ul
369
                        id="contact-group-list-ul"
370
                        className="live-search-list"
371
                      ></ul>
372
                    </div>
373
                  </div>
374
                </div>
375
              </li>
376
            </ul>
377
          </div>
378
        </div>
379
      </div>
380
      <div style={{ display: "flex" }}>
381
        {activeChats.map((entity, index) => (
382
          <PersonalChat
383
            key={entity.id}
384
            entity={entity}
385
            index={index}
386
            onClose={handleCloseChat}
387
            onMinimize={handleMinimizeChat}
388
            onRead={handleReadChat}
389
          />
390
        ))}
391
      </div>
1335 stevensc 392
      <NotificationAlert />
1 www 393
    </React.Fragment>
394
  ) : (
395
    ""
396
  );
397
};
398
 
399
export default Chat;