touchstudy-core 0.1.6 → 0.1.8

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/dist/index.js CHANGED
@@ -16,6 +16,11 @@ var gapiScript = require('gapi-script');
16
16
  var i18n = _interopDefault(require('i18next'));
17
17
  var reactI18next = require('react-i18next');
18
18
  var io5 = require('react-icons/io5');
19
+ var Select = _interopDefault(require('react-select'));
20
+ var reactRouterDom = require('react-router-dom');
21
+ var io = require('react-icons/io');
22
+ var reactToastify = require('react-toastify');
23
+ var Pusher = _interopDefault(require('pusher-js'));
19
24
 
20
25
  var setLoading = toolkit.createAction("common/setLoading");
21
26
  var setAlert = toolkit.createAction("common/setAlert");
@@ -33,9 +38,10 @@ var BASE_URL = function () {
33
38
  }();
34
39
  var PUSHER_CONFIG = {
35
40
  cluster: "ap1",
36
- key: "9018c77328885a14150b"
41
+ key: "dde049326fbe52b2a749"
37
42
  };
38
- var EXAM_CHANNEL = "EXAM-CHANNEL";
43
+ var EXAM_CHANNEL = "presence-exam-channel";
44
+ var EXAM_STUDENT_CHANNEL = "EXAM-CHANNEL-{examCode}-STUDENT-{studentId}";
39
45
  var getAccessToken = function getAccessToken() {
40
46
  try {
41
47
  return localStorage.getItem(ACCESS_TOKEN);
@@ -639,66 +645,9 @@ var Loading = function Loading() {
639
645
  }))) : null;
640
646
  };
641
647
 
642
- var encodeParams$1 = (function (params) {
643
- return Object.keys(params).filter(function (key) {
644
- return params[key] || params[key] === 0 || params[key] === false;
645
- }).map(function (key) {
646
- if (Array.isArray(params[key])) {
647
- return params[key].map(function (i) {
648
- return encodeURIComponent(key) + "=" + encodeURIComponent(i);
649
- }).join('&');
650
- }
651
- return encodeURIComponent(key) + "=" + encodeURIComponent(params[key]);
652
- }).join('&');
653
- });
654
-
655
- var api$1 = axios.create({
656
- baseURL: BASE_URL,
657
- timeout: 0,
658
- headers: {
659
- "Content-Type": "application/json"
660
- },
661
- paramsSerializer: function paramsSerializer(params) {
662
- return encodeParams$1(params);
663
- }
664
- });
665
- var apiUpload$1 = axios.create({
666
- baseURL: BASE_URL,
667
- timeout: 60000,
668
- headers: {
669
- "Content-Type": "multipart/form-data"
670
- }
671
- });
672
- [api$1, apiUpload$1].forEach(function (i) {
673
- return i.interceptors.request.use(function (config) {
674
- var token = getAccessToken$1();
675
- if (token) {
676
- config.headers.Authorization = "Bearer " + token;
677
- }
678
- return config;
679
- }, function (error) {
680
- return Promise.reject(error);
681
- });
682
- });
683
- [api$1, apiUpload$1].forEach(function (i) {
684
- return i.interceptors.response.use(function (response) {
685
- return response;
686
- }, function (error) {
687
- if (error.response.status === 401) {
688
- window.location.href = "/login";
689
- }
690
- if (error.response.status == 403) {
691
- localStorage.removeItem(ACCESS_TOKEN);
692
- localStorage.removeItem("USER_EMAIL");
693
- window.location.href = "/login";
694
- }
695
- return Promise.reject(error);
696
- });
697
- });
698
-
699
648
  var AUTH_URL = BASE_URL + "/api/auth";
700
649
  var getInfo = function getInfo() {
701
- return api$1.get(AUTH_URL + "/info");
650
+ return api.get(AUTH_URL + "/info");
702
651
  };
703
652
 
704
653
  var LayoutContext = function LayoutContext(_ref) {
@@ -961,10 +910,8 @@ var CustomPagination = function CustomPagination(_ref) {
961
910
  (function (ExamEvent) {
962
911
  ExamEvent["StartExam"] = "start-exam";
963
912
  ExamEvent["TerminateExam"] = "terminate-exam";
964
- ExamEvent["JoinExam"] = "join-exam";
965
913
  ExamEvent["AddExtraDuration"] = "add-extra-duration-exam";
966
- ExamEvent["TeacherDisconnectChannel"] = "teacher-disconnect-channel";
967
- ExamEvent["StudentDisconnectChannel"] = "student-disconnect-channel";
914
+ ExamEvent["TeacherKickOutStudent"] = "teacher-kick-out-student";
968
915
  })(exports.ExamEvent || (exports.ExamEvent = {}));
969
916
 
970
917
  var problem_solving = "문제풀기";
@@ -1023,6 +970,7 @@ var internet_connection_was_lost = "인터넷 연결이 끊어졌습니다";
1023
970
  var problem = "문제";
1024
971
  var problem_solving_time = "문제풀이 시간";
1025
972
  var minutes = "분";
973
+ var correct_rate = "정확한 비율";
1026
974
  var lang_ko = {
1027
975
  problem_solving: problem_solving,
1028
976
  my_story: my_story,
@@ -1079,7 +1027,8 @@ var lang_ko = {
1079
1027
  internet_connection_was_lost: internet_connection_was_lost,
1080
1028
  problem: problem,
1081
1029
  problem_solving_time: problem_solving_time,
1082
- minutes: minutes
1030
+ minutes: minutes,
1031
+ correct_rate: correct_rate
1083
1032
  };
1084
1033
 
1085
1034
  var problem_solving$1 = "Problem Solving";
@@ -1138,6 +1087,7 @@ var internet_connection_was_lost$1 = "Internet connection was lost";
1138
1087
  var problem$1 = "Problem ";
1139
1088
  var problem_solving_time$1 = "Problem solving time";
1140
1089
  var minutes$1 = "m";
1090
+ var correct_rate$1 = "Correct rate";
1141
1091
  var lang_en = {
1142
1092
  problem_solving: problem_solving$1,
1143
1093
  my_story: my_story$1,
@@ -1194,7 +1144,8 @@ var lang_en = {
1194
1144
  internet_connection_was_lost: internet_connection_was_lost$1,
1195
1145
  problem: problem$1,
1196
1146
  problem_solving_time: problem_solving_time$1,
1197
- minutes: minutes$1
1147
+ minutes: minutes$1,
1148
+ correct_rate: correct_rate$1
1198
1149
  };
1199
1150
 
1200
1151
  i18n.use(reactI18next.initReactI18next).init({
@@ -1339,6 +1290,1273 @@ var canAccess = function canAccess(userRoles, componentRoles) {
1339
1290
  return true;
1340
1291
  };
1341
1292
 
1293
+ function _extends() {
1294
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
1295
+ for (var i = 1; i < arguments.length; i++) {
1296
+ var source = arguments[i];
1297
+ for (var key in source) {
1298
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
1299
+ target[key] = source[key];
1300
+ }
1301
+ }
1302
+ }
1303
+ return target;
1304
+ };
1305
+ return _extends.apply(this, arguments);
1306
+ }
1307
+ function _objectWithoutPropertiesLoose(source, excluded) {
1308
+ if (source == null) return {};
1309
+ var target = {};
1310
+ var sourceKeys = Object.keys(source);
1311
+ var key, i;
1312
+ for (i = 0; i < sourceKeys.length; i++) {
1313
+ key = sourceKeys[i];
1314
+ if (excluded.indexOf(key) >= 0) continue;
1315
+ target[key] = source[key];
1316
+ }
1317
+ return target;
1318
+ }
1319
+
1320
+ var _excluded = ["defaultValue", "options", "isDisabled", "scrollBottom", "value", "isMulti"];
1321
+ var CustomSelect = function CustomSelect(_ref) {
1322
+ var options = _ref.options,
1323
+ isDisabled = _ref.isDisabled,
1324
+ scrollBottom = _ref.scrollBottom,
1325
+ value = _ref.value,
1326
+ isMulti = _ref.isMulti,
1327
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded);
1328
+ var initialValues = Array.isArray(value) ? options.filter(function (i) {
1329
+ return value.includes(i.value);
1330
+ }) : isMulti ? options.filter(function (i) {
1331
+ return i.value == value;
1332
+ }) : options.find(function (i) {
1333
+ return i.value == value;
1334
+ });
1335
+ return React__default.createElement(Select, Object.assign({
1336
+ isDisabled: isDisabled,
1337
+ options: options,
1338
+ value: initialValues,
1339
+ menuPlacement: scrollBottom ? "top" : "auto",
1340
+ isMulti: isMulti
1341
+ }, rest));
1342
+ };
1343
+
1344
+ var styles$1 = {"header__avatar":"_2oGcJ","header__select":"_2y2XV","body":"_ntGyI","list":"_m7Fwt","list__item__header":"_3BM8N","list__item__avatar":"_1ReOK","list__item__avatar-container":"_2qbld","list__item__message":"_3s01i","list__item__message--reply-from":"_2Qxi3","list__item__message--reply-from-right":"_34Vx4","list__item__message--reply":"_3uheq","list__item__message--reply-right":"_2iBr7","reply-text":"_3ZPjO"};
1345
+
1346
+ var MessageSortBy;
1347
+ (function (MessageSortBy) {
1348
+ MessageSortBy["CreatedAt"] = "CreatedAt";
1349
+ })(MessageSortBy || (MessageSortBy = {}));
1350
+ var OrderBy;
1351
+ (function (OrderBy) {
1352
+ OrderBy["ASC"] = "ASC";
1353
+ OrderBy["DESC"] = "DESC";
1354
+ })(OrderBy || (OrderBy = {}));
1355
+ var StudentSortBy;
1356
+ (function (StudentSortBy) {
1357
+ StudentSortBy["CreatedAt"] = "CreatedAt";
1358
+ StudentSortBy["DisplayName"] = "DisplayName";
1359
+ })(StudentSortBy || (StudentSortBy = {}));
1360
+ var StudentOrderBy;
1361
+ (function (StudentOrderBy) {
1362
+ StudentOrderBy["ASC"] = "ASC";
1363
+ StudentOrderBy["DESC"] = "DESC";
1364
+ })(StudentOrderBy || (StudentOrderBy = {}));
1365
+ var MessageAction;
1366
+ (function (MessageAction) {
1367
+ MessageAction[MessageAction["Default"] = 0] = "Default";
1368
+ MessageAction[MessageAction["Took"] = 1] = "Took";
1369
+ })(MessageAction || (MessageAction = {}));
1370
+ var MessageSourceType;
1371
+ (function (MessageSourceType) {
1372
+ MessageSourceType[MessageSourceType["Default"] = 0] = "Default";
1373
+ MessageSourceType[MessageSourceType["Exam"] = 1] = "Exam";
1374
+ MessageSourceType[MessageSourceType["Class"] = 2] = "Class";
1375
+ })(MessageSourceType || (MessageSourceType = {}));
1376
+ var MessageStatus;
1377
+ (function (MessageStatus) {
1378
+ MessageStatus["Default"] = "Sent";
1379
+ MessageStatus["Sending"] = "Sending...";
1380
+ MessageStatus["SentError"] = "Sent fail, Retry";
1381
+ })(MessageStatus || (MessageStatus = {}));
1382
+
1383
+ var ChatRightItem = function ChatRightItem(_ref) {
1384
+ var content = _ref.content,
1385
+ createdAt = _ref.createdAt,
1386
+ isRead = _ref.isRead,
1387
+ _ref$type = _ref.type,
1388
+ type = _ref$type === void 0 ? exports.ChatItemType.Default : _ref$type,
1389
+ examCode = _ref.examCode,
1390
+ isSystemLog = _ref.isSystemLog,
1391
+ parentId = _ref.parentId,
1392
+ _ref$showExamHeader = _ref.showExamHeader,
1393
+ showExamHeader = _ref$showExamHeader === void 0 ? false : _ref$showExamHeader,
1394
+ _ref$isFirst = _ref.isFirst,
1395
+ isFirst = _ref$isFirst === void 0 ? true : _ref$isFirst,
1396
+ isLast = _ref.isLast,
1397
+ actionLink = _ref.actionLink,
1398
+ examId = _ref.examId,
1399
+ examTitle = _ref.examTitle,
1400
+ parentContent = _ref.parentContent,
1401
+ status = _ref.status,
1402
+ onAddExamMessage = _ref.onAddExamMessage,
1403
+ onReTrySendMessage = _ref.onReTrySendMessage;
1404
+ var _useState = React.useState(false),
1405
+ isShowTime = _useState[0],
1406
+ setShowTime = _useState[1];
1407
+ var _useState2 = React.useState(false),
1408
+ isShowInfo = _useState2[0],
1409
+ setShowInfo = _useState2[1];
1410
+ var handleShowInfo = function handleShowInfo() {
1411
+ setShowInfo(!isShowInfo);
1412
+ !isFirst && !isSystemLog && setShowTime(!isShowInfo);
1413
+ };
1414
+ var handleReTrySendMessage = function handleReTrySendMessage() {
1415
+ if (status !== MessageStatus.SentError || !content) return;
1416
+ onReTrySendMessage === null || onReTrySendMessage === void 0 ? void 0 : onReTrySendMessage({
1417
+ content: content,
1418
+ examId: examId,
1419
+ examTitle: examTitle,
1420
+ parentContent: parentContent,
1421
+ parentId: parentId
1422
+ });
1423
+ };
1424
+ React.useEffect(function () {
1425
+ setShowTime(isSystemLog || !!isFirst);
1426
+ }, [isFirst, isSystemLog]);
1427
+ return React__default.createElement("li", {
1428
+ className: "px-3 " + (!isLast ? "pb-1" : "pb-3") + " " + (!!examCode ? "border border-top-0 border-info" : "") + " " + (!!examCode && isLast ? "border-bottom-1 mb-3" : "border-bottom-0")
1429
+ }, showExamHeader && isFirst && React__default.createElement("div", {
1430
+ className: "d-flex bg-info justify-content-between px-3 mb-3 py-1 " + styles$1["list__item__header"]
1431
+ }, React__default.createElement(reactRouterDom.Link, {
1432
+ to: "" + (actionLink || "/"),
1433
+ className: "text-black"
1434
+ }, examTitle), !!examId && !!examTitle && React__default.createElement("span", {
1435
+ className: "pointer text-black",
1436
+ onClick: function onClick() {
1437
+ return onAddExamMessage === null || onAddExamMessage === void 0 ? void 0 : onAddExamMessage(examId, examTitle);
1438
+ }
1439
+ }, "Add message to exam")), isSystemLog || !!isFirst ? React__default.createElement("p", {
1440
+ className: "mb-0 py-1 px-3 fs-6 text-muted text-center"
1441
+ }, utcToLocalTime(createdAt, "HH:mm DD MMM YYYY")) : React__default.createElement(reactstrap.Collapse, {
1442
+ className: "w-100",
1443
+ isOpen: isShowTime
1444
+ }, React__default.createElement("p", {
1445
+ className: "mb-0 py-1 px-3 fs-6 text-muted text-center"
1446
+ }, utcToLocalTime(createdAt, "HH:mm DD MMM YYYY"))), isSystemLog ? React__default.createElement("div", {
1447
+ className: "text-center w-100"
1448
+ }, React__default.createElement(reactRouterDom.Link, {
1449
+ to: "" + (actionLink || "/"),
1450
+ title: "See the results",
1451
+ className: "text-primary"
1452
+ }, content)) : React__default.createElement("div", {
1453
+ className: "d-flex align-items-start w-100 flex-row-reverse"
1454
+ }, React__default.createElement("div", {
1455
+ className: "d-flex flex-column align-items-end"
1456
+ }, parentId && React__default.createElement("p", {
1457
+ className: "opacity-50 mb-0 py-2 px-3 fw-medium pointer " + styles$1["list__item__message"] + " " + styles$1["list__item__message--reply-from-right"] + " text-end bg-primary text-white"
1458
+ }, React__default.createElement("b", null, "Reply To:"), " ", parentContent), React__default.createElement("p", {
1459
+ className: "mb-0 py-2 px-3 fw-medium pointer " + styles$1["list__item__message"] + " " + (parentId ? styles$1["list__item__message--reply-right"] : "") + " text-end bg-primary text-white",
1460
+ onClick: handleShowInfo
1461
+ }, content), status !== MessageStatus.Default && React__default.createElement("p", {
1462
+ onClick: handleReTrySendMessage,
1463
+ className: "fs-6 mb-0 " + (status === MessageStatus.SentError ? "text-danger text-decoration-underline pointer" : "")
1464
+ }, status), (type !== exports.ChatItemType.Exam || !examCode) && status === MessageStatus.Default && React__default.createElement(reactstrap.Collapse, {
1465
+ isOpen: isShowInfo
1466
+ }, React__default.createElement("p", {
1467
+ className: "mb-0 py-1 px-3 fs-6 text-muted text-end"
1468
+ }, isRead ? "seen" : "sent")))));
1469
+ };
1470
+
1471
+ var ChatLeftItem = function ChatLeftItem(_ref) {
1472
+ var id = _ref.id,
1473
+ avatar = _ref.avatar,
1474
+ content = _ref.content,
1475
+ createdAt = _ref.createdAt,
1476
+ examCode = _ref.examCode,
1477
+ isSystemLog = _ref.isSystemLog,
1478
+ parentId = _ref.parentId,
1479
+ _ref$showExamHeader = _ref.showExamHeader,
1480
+ showExamHeader = _ref$showExamHeader === void 0 ? false : _ref$showExamHeader,
1481
+ _ref$isFirst = _ref.isFirst,
1482
+ isFirst = _ref$isFirst === void 0 ? true : _ref$isFirst,
1483
+ isLast = _ref.isLast,
1484
+ actionLink = _ref.actionLink,
1485
+ examId = _ref.examId,
1486
+ examTitle = _ref.examTitle,
1487
+ parentContent = _ref.parentContent,
1488
+ onAddExamMessage = _ref.onAddExamMessage,
1489
+ onReply = _ref.onReply,
1490
+ selectedReplyMessageId = _ref.selectedReplyMessageId,
1491
+ onToggleReply = _ref.onToggleReply,
1492
+ selectedReplyItemRef = _ref.selectedReplyItemRef;
1493
+ var _useState = React.useState(false),
1494
+ isShowTime = _useState[0],
1495
+ setShowTime = _useState[1];
1496
+ var _useState2 = React.useState(false),
1497
+ isShowInfo = _useState2[0],
1498
+ setShowInfo = _useState2[1];
1499
+ var handleShowInfo = function handleShowInfo() {
1500
+ if (!id) return;
1501
+ setShowInfo(!isShowInfo);
1502
+ onToggleReply === null || onToggleReply === void 0 ? void 0 : onToggleReply(id);
1503
+ !isFirst && !isSystemLog && setShowTime(!isShowInfo);
1504
+ };
1505
+ var handleReply = function handleReply() {
1506
+ if (!id) return;
1507
+ onReply === null || onReply === void 0 ? void 0 : onReply(id, content || "", examId);
1508
+ };
1509
+ React.useEffect(function () {
1510
+ setShowTime(isSystemLog || !!isFirst);
1511
+ }, [isFirst, isSystemLog]);
1512
+ return React__default.createElement("li", {
1513
+ ref: selectedReplyMessageId === id ? selectedReplyItemRef : undefined,
1514
+ className: "px-3 " + (!isLast ? "pb-1" : "pb-3") + " " + (!!examCode ? "border border-top-0 border-info" : "") + " " + (!!examCode && isLast ? "border-bottom-1 mb-3" : "border-bottom-0")
1515
+ }, showExamHeader && isFirst && React__default.createElement("div", {
1516
+ className: "d-flex bg-info justify-content-between px-3 mb-3 py-1 " + styles$1["list__item__header"]
1517
+ }, React__default.createElement(reactRouterDom.Link, {
1518
+ to: "" + (actionLink || "/"),
1519
+ className: "text-black"
1520
+ }, examTitle), !!examId && !!examTitle && React__default.createElement("span", {
1521
+ className: "pointer text-black",
1522
+ onClick: function onClick() {
1523
+ return onAddExamMessage === null || onAddExamMessage === void 0 ? void 0 : onAddExamMessage(examId, examTitle);
1524
+ }
1525
+ }, "Add message to exam")), isSystemLog || !!isFirst ? React__default.createElement("p", {
1526
+ className: "mb-0 py-1 px-3 fs-6 text-muted text-center"
1527
+ }, utcToLocalTime(createdAt, "HH:mm DD MMM YYYY")) : React__default.createElement(reactstrap.Collapse, {
1528
+ className: "w-100",
1529
+ isOpen: isShowTime
1530
+ }, React__default.createElement("p", {
1531
+ className: "mb-0 py-1 px-3 fs-6 text-muted text-center"
1532
+ }, utcToLocalTime(createdAt, "HH:mm DD MMM YYYY"))), isSystemLog ? React__default.createElement("div", {
1533
+ className: "text-center w-100"
1534
+ }, React__default.createElement(reactRouterDom.Link, {
1535
+ to: "" + (actionLink || "/"),
1536
+ title: "See the results",
1537
+ className: "text-primary"
1538
+ }, content)) : React__default.createElement("div", {
1539
+ className: "d-flex align-items-start w-100"
1540
+ }, React__default.createElement("div", {
1541
+ className: "rounded-circle " + styles$1["list__item__avatar-container"] + " bg-light me-2 mt-1 " + (isFirst ? "" : "opacity-0")
1542
+ }, isFirst && React__default.createElement("img", {
1543
+ className: styles$1["list__item__avatar"] + " bg-light rounded-circle",
1544
+ src: avatar
1545
+ })), React__default.createElement("div", {
1546
+ className: "d-flex flex-column"
1547
+ }, parentId && React__default.createElement("p", {
1548
+ className: "opacity-50 mb-0 py-2 px-3 fw-medium pointer " + styles$1["list__item__message"] + " " + styles$1["list__item__message--reply-from"] + " text-start bg-light"
1549
+ }, React__default.createElement("b", null, "Reply To:"), " ", parentContent), React__default.createElement("p", {
1550
+ className: "mb-0 py-2 px-3 fw-medium pointer " + styles$1["list__item__message"] + " " + (parentId ? styles$1["list__item__message--reply"] : "") + " text-start bg-light",
1551
+ onClick: handleShowInfo
1552
+ }, content), !isSystemLog && React__default.createElement(reactstrap.Collapse, {
1553
+ className: "w-100",
1554
+ isOpen: selectedReplyMessageId === id
1555
+ }, React__default.createElement("p", {
1556
+ onClick: handleReply,
1557
+ className: "mb-0 py-1 px-3 fs-6 text-primary text-left pointer"
1558
+ }, "Reply")))));
1559
+ };
1560
+
1561
+ (function (ChatItemType) {
1562
+ ChatItemType[ChatItemType["Default"] = 0] = "Default";
1563
+ ChatItemType[ChatItemType["Exam"] = 1] = "Exam";
1564
+ ChatItemType[ChatItemType["Class"] = 2] = "Class";
1565
+ })(exports.ChatItemType || (exports.ChatItemType = {}));
1566
+ var ChatItem = function ChatItem(props) {
1567
+ var isMe = props.isMe,
1568
+ onReTrySendMessage = props.onReTrySendMessage;
1569
+ return React__default.createElement(React.Fragment, null, isMe ? React__default.createElement(ChatRightItem, Object.assign({}, props, {
1570
+ onReTrySendMessage: onReTrySendMessage
1571
+ })) : React__default.createElement(ChatLeftItem, Object.assign({}, props)));
1572
+ };
1573
+
1574
+ var ChatList = function ChatList(_ref) {
1575
+ var _ref$messages = _ref.messages,
1576
+ messages = _ref$messages === void 0 ? [] : _ref$messages,
1577
+ onAddExamMessage = _ref.onAddExamMessage,
1578
+ onReply = _ref.onReply,
1579
+ onReTrySendMessage = _ref.onReTrySendMessage,
1580
+ listItemRef = _ref.listItemRef;
1581
+ var _useState = React.useState(),
1582
+ selectedReplyMessageId = _useState[0],
1583
+ setSelectedReplyMessageId = _useState[1];
1584
+ var selectedReplyItemRef = React.useRef(null);
1585
+ var handleToggleReply = function handleToggleReply(id) {
1586
+ setSelectedReplyMessageId(id === selectedReplyMessageId ? undefined : id);
1587
+ };
1588
+ React.useEffect(function () {
1589
+ selectedReplyMessageId && selectedReplyItemRef.current && selectedReplyItemRef.current.scrollIntoView({
1590
+ behavior: "smooth"
1591
+ });
1592
+ }, [selectedReplyMessageId, selectedReplyItemRef.current]);
1593
+ return React__default.createElement("ul", {
1594
+ ref: listItemRef,
1595
+ className: styles$1["list"] + " h-100 d-flex flex-column py-3 px-0 mb-0"
1596
+ }, React__default.createElement("li", {
1597
+ className: "flex-grow-1",
1598
+ key: "start-of-list"
1599
+ }), !(messages !== null && messages !== void 0 && messages.length) && React__default.createElement("li", {
1600
+ className: "text-muted text-center fst-italic fs-6"
1601
+ }, "No message"), !!(messages !== null && messages !== void 0 && messages.length) && messages.map(function (message) {
1602
+ return React__default.createElement(ChatItem, Object.assign({
1603
+ key: message.id
1604
+ }, message, {
1605
+ onAddExamMessage: onAddExamMessage,
1606
+ onReply: onReply,
1607
+ selectedReplyMessageId: selectedReplyMessageId,
1608
+ onToggleReply: handleToggleReply,
1609
+ selectedReplyItemRef: selectedReplyItemRef,
1610
+ onReTrySendMessage: onReTrySendMessage
1611
+ }));
1612
+ }));
1613
+ };
1614
+
1615
+ var InputChat = function InputChat(_ref) {
1616
+ var text = _ref.text,
1617
+ parentText = _ref.parentText,
1618
+ examTitle = _ref.examTitle,
1619
+ inputRef = _ref.inputRef,
1620
+ onChangeInput = _ref.onChangeInput,
1621
+ onSubmit = _ref.onSubmit,
1622
+ onClearReply = _ref.onClearReply;
1623
+ var handleSubmit = function handleSubmit() {
1624
+ if (!text.trim().length) return;
1625
+ onSubmit();
1626
+ };
1627
+ var handleInputChange = function handleInputChange(event) {
1628
+ onChangeInput(event.target.value);
1629
+ };
1630
+ var handleKeyPressEnter = function handleKeyPressEnter(e) {
1631
+ if (e.code === "Enter") handleSubmit();
1632
+ };
1633
+ var handleClearReply = function handleClearReply() {
1634
+ onClearReply === null || onClearReply === void 0 ? void 0 : onClearReply();
1635
+ };
1636
+ return React__default.createElement("div", {
1637
+ className: "bg-light rounded-3 p-2"
1638
+ }, React__default.createElement(reactstrap.Collapse, {
1639
+ isOpen: !!parentText || !!examTitle && !parentText
1640
+ }, !!parentText ? React__default.createElement("div", {
1641
+ className: "d-flex justify-content-between align-items-center"
1642
+ }, React__default.createElement("p", {
1643
+ className: "mb-3 " + styles$1["reply-text"]
1644
+ }, React__default.createElement("b", null, "Reply to:"), " " + parentText), !!onClearReply && React__default.createElement("span", {
1645
+ className: "pointer ms-3 mb-3 d-flex justify-content-center align-items-center",
1646
+ onClick: handleClearReply
1647
+ }, React__default.createElement(io.IoIosCloseCircle, null))) : React__default.createElement("div", {
1648
+ className: "d-flex justify-content-between align-items-center"
1649
+ }, React__default.createElement("p", {
1650
+ className: "mb-3 " + styles$1["reply-text"]
1651
+ }, React__default.createElement("b", null, "Reply to", examTitle ? " exam" : "", ": "), " " + examTitle), !!onClearReply && React__default.createElement("span", {
1652
+ className: "pointer ms-3 mb-3 d-flex justify-content-center align-items-center",
1653
+ onClick: handleClearReply
1654
+ }, React__default.createElement(io.IoIosCloseCircle, null)))), React__default.createElement(reactstrap.InputGroup, {
1655
+ className: "d-flex align-items"
1656
+ }, React__default.createElement(reactstrap.Input, {
1657
+ innerRef: inputRef,
1658
+ value: text,
1659
+ placeholder: "Enter something",
1660
+ onKeyUp: handleKeyPressEnter,
1661
+ className: "rounded-pill px-3",
1662
+ onChange: handleInputChange
1663
+ }), React__default.createElement("div", {
1664
+ className: "p-2 pointer d-flex align-items-center",
1665
+ onClick: handleSubmit
1666
+ }, React__default.createElement(io.IoMdSend, {
1667
+ className: "text-primary fs-4"
1668
+ }))));
1669
+ };
1670
+
1671
+ var ChatHeader = function ChatHeader(_ref) {
1672
+ var name = _ref.name,
1673
+ avatar = _ref.avatar,
1674
+ options = _ref.options,
1675
+ onChangeOption = _ref.onChangeOption;
1676
+ var wrapperRef = React.useRef(null);
1677
+ var actionRef = React.useRef(null);
1678
+ var handleChangeOption = function handleChangeOption(val) {
1679
+ onChangeOption === null || onChangeOption === void 0 ? void 0 : onChangeOption(val);
1680
+ };
1681
+ React.useEffect(function () {
1682
+ var handleClickOutside = function handleClickOutside(event) {
1683
+ if (wrapperRef.current && !wrapperRef.current.contains(event.target) && actionRef.current && !actionRef.current.contains(event.target)) ;
1684
+ };
1685
+ document.addEventListener("mousedown", handleClickOutside);
1686
+ return function () {
1687
+ document.removeEventListener("mousedown", handleClickOutside);
1688
+ };
1689
+ }, [wrapperRef, actionRef]);
1690
+ var selectOptions = React.useMemo(function () {
1691
+ return options === null || options === void 0 ? void 0 : options.map(function (i) {
1692
+ return {
1693
+ value: i.id,
1694
+ label: i.title + " (" + utcToLocalTime(i.startTime) + ")"
1695
+ };
1696
+ });
1697
+ }, [JSON.stringify(options)]);
1698
+ return React__default.createElement("div", {
1699
+ className: "d-flex bg-light rounded-3 p-3"
1700
+ }, React__default.createElement("div", null, React__default.createElement("img", {
1701
+ className: "rounded-circle " + styles$1["header__avatar"],
1702
+ src: avatar,
1703
+ alt: name
1704
+ })), React__default.createElement("div", {
1705
+ className: "flex-grow-1 px-3 d-flex align-items-center"
1706
+ }, React__default.createElement("p", {
1707
+ className: "mb-0 text-primary fw-medium"
1708
+ }, name)), React__default.createElement("div", {
1709
+ className: "d-flex align-items-center"
1710
+ }, selectOptions !== undefined && React__default.createElement("div", {
1711
+ className: styles$1["header__select"] + " mx-3"
1712
+ }, React__default.createElement(CustomSelect, {
1713
+ options: selectOptions,
1714
+ onChange: handleChangeOption,
1715
+ isClearable: true
1716
+ })), React__default.createElement("div", {
1717
+ className: "actions d-flex align-items-center"
1718
+ })));
1719
+ };
1720
+
1721
+ var ChatContainer = function ChatContainer(_ref) {
1722
+ var chatHeaderProps = _ref.chatHeaderProps,
1723
+ chatListProps = _ref.chatListProps,
1724
+ inputProps = _ref.inputProps,
1725
+ onRead = _ref.onRead;
1726
+ var onReply = chatListProps.onReply,
1727
+ onAddExamMessage = chatListProps.onAddExamMessage;
1728
+ var onChangeOption = chatHeaderProps.onChangeOption;
1729
+ var inputRef = React.useRef(null);
1730
+ var listRef = React.useRef(null);
1731
+ var listItemRef = React.useRef(null);
1732
+ var scrollRef = React.useRef();
1733
+ var focusRef = React.useRef();
1734
+ var addEventTimeout = React.useRef();
1735
+ var handleReply = function handleReply(id, content, examId) {
1736
+ var _inputRef$current;
1737
+ (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.focus();
1738
+ onReply === null || onReply === void 0 ? void 0 : onReply(id, content, examId);
1739
+ };
1740
+ var handleAddExamMessage = function handleAddExamMessage(examId, examTitle) {
1741
+ var _inputRef$current2;
1742
+ (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.focus();
1743
+ onAddExamMessage(examId, examTitle);
1744
+ };
1745
+ var handleChangeExamOption = function handleChangeExamOption(val) {
1746
+ var _inputRef$current3;
1747
+ if (!!(val !== null && val !== void 0 && val.value)) (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 ? void 0 : _inputRef$current3.focus();
1748
+ onChangeOption === null || onChangeOption === void 0 ? void 0 : onChangeOption(val);
1749
+ };
1750
+ var handleScroll = function handleScroll() {
1751
+ if (scrollRef.current) clearTimeout(scrollRef.current);
1752
+ scrollRef.current = setTimeout(function () {
1753
+ onRead === null || onRead === void 0 ? void 0 : onRead();
1754
+ }, 800);
1755
+ };
1756
+ var handleFocus = function handleFocus() {
1757
+ if (focusRef.current) clearTimeout(focusRef.current);
1758
+ focusRef.current = setTimeout(function () {
1759
+ onRead === null || onRead === void 0 ? void 0 : onRead();
1760
+ }, 800);
1761
+ };
1762
+ React.useEffect(function () {
1763
+ var _listRef$current, _listItemRef$current, _listItemRef$current$;
1764
+ scrollRef.current && clearTimeout(scrollRef.current);
1765
+ addEventTimeout.current && clearTimeout(addEventTimeout.current);
1766
+ (_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.removeEventListener("scroll", handleScroll);
1767
+ (_listItemRef$current = listItemRef.current) === null || _listItemRef$current === void 0 ? void 0 : (_listItemRef$current$ = _listItemRef$current.lastElementChild) === null || _listItemRef$current$ === void 0 ? void 0 : _listItemRef$current$.scrollIntoView({
1768
+ behavior: "smooth"
1769
+ });
1770
+ addEventTimeout.current = setTimeout(function () {
1771
+ var _listRef$current2;
1772
+ (_listRef$current2 = listRef.current) === null || _listRef$current2 === void 0 ? void 0 : _listRef$current2.addEventListener("scroll", handleScroll);
1773
+ }, 800);
1774
+ return function () {
1775
+ var _listRef$current3;
1776
+ (_listRef$current3 = listRef.current) === null || _listRef$current3 === void 0 ? void 0 : _listRef$current3.removeEventListener("scroll", handleScroll);
1777
+ scrollRef.current && clearTimeout(scrollRef.current);
1778
+ addEventTimeout.current && clearTimeout(addEventTimeout.current);
1779
+ };
1780
+ }, [listItemRef.current, JSON.stringify(chatListProps.messages)]);
1781
+ React.useEffect(function () {
1782
+ var _inputRef$current4;
1783
+ (_inputRef$current4 = inputRef.current) === null || _inputRef$current4 === void 0 ? void 0 : _inputRef$current4.addEventListener("focus", handleFocus);
1784
+ return function () {
1785
+ var _inputRef$current5;
1786
+ (_inputRef$current5 = inputRef.current) === null || _inputRef$current5 === void 0 ? void 0 : _inputRef$current5.removeEventListener("focus", handleFocus);
1787
+ if (focusRef.current) clearTimeout(focusRef.current);
1788
+ };
1789
+ }, []);
1790
+ return React__default.createElement("div", {
1791
+ className: "d-flex flex-column w-100 h-100 border border-1 border-light rounded-3 shadow-sm"
1792
+ }, React__default.createElement("div", null, React__default.createElement(ChatHeader, Object.assign({}, chatHeaderProps, {
1793
+ onChangeOption: handleChangeExamOption
1794
+ }))), React__default.createElement("div", {
1795
+ ref: listRef,
1796
+ className: "flex-grow-1 " + styles$1["body"]
1797
+ }, React__default.createElement(ChatList, Object.assign({}, chatListProps, {
1798
+ listItemRef: listItemRef,
1799
+ onReply: handleReply,
1800
+ onAddExamMessage: handleAddExamMessage
1801
+ }))), React__default.createElement("div", null, React__default.createElement(InputChat, Object.assign({}, inputProps, {
1802
+ inputRef: inputRef
1803
+ }))), React__default.createElement(reactToastify.ToastContainer, null));
1804
+ };
1805
+
1806
+ var CONVERSATION_URL = BASE_URL + "/api/conversation";
1807
+ var getListConversation = function getListConversation(query) {
1808
+ return api.get("" + CONVERSATION_URL, {
1809
+ params: query
1810
+ });
1811
+ };
1812
+ var createConversation = function createConversation(studentId) {
1813
+ return api.post("" + CONVERSATION_URL, {
1814
+ targetUserId: "" + studentId
1815
+ });
1816
+ };
1817
+ var apiAddMessage = function apiAddMessage(conversationId, message) {
1818
+ return api.post(CONVERSATION_URL + "/" + conversationId + "/messages", message);
1819
+ };
1820
+ var getMessagesByConversation = function getMessagesByConversation(conversationId, filter) {
1821
+ return api.get(CONVERSATION_URL + "/" + conversationId + "/messages", {
1822
+ params: filter
1823
+ });
1824
+ };
1825
+ var apiMarkReadMessage = function apiMarkReadMessage(conversationId, messageId) {
1826
+ return api.put(CONVERSATION_URL + "/" + conversationId + "/messages/" + messageId + "/mark-read");
1827
+ };
1828
+ var apiGetConversationByUserId = function apiGetConversationByUserId(userId) {
1829
+ return api.get(CONVERSATION_URL + "/users/" + userId);
1830
+ };
1831
+
1832
+ var ExamStatus;
1833
+ (function (ExamStatus) {
1834
+ ExamStatus["Default"] = "Default";
1835
+ ExamStatus["Pending"] = "Pending";
1836
+ ExamStatus["Inprogress"] = "Inprogress";
1837
+ ExamStatus["Completed"] = "Completed";
1838
+ })(ExamStatus || (ExamStatus = {}));
1839
+ var CONVERSATION_DEFAULT_FILTER = {
1840
+ currentPage: 1,
1841
+ pageSize: -1,
1842
+ textSearch: ""
1843
+ };
1844
+ var MESSAGE_DEFAULT_FILTER = {
1845
+ currentPage: 1,
1846
+ pageSize: -1,
1847
+ textSearch: "",
1848
+ sortColumnDirection: OrderBy.DESC,
1849
+ sortColumnName: MessageSortBy.CreatedAt
1850
+ };
1851
+
1852
+ var EXAM_URL = BASE_URL + "/api/exam";
1853
+ var getListExamApi = function getListExamApi(query) {
1854
+ return api.get(EXAM_URL + "/getList", {
1855
+ params: query
1856
+ });
1857
+ };
1858
+
1859
+ var useExamList = function useExamList(isStudent) {
1860
+ if (isStudent === void 0) {
1861
+ isStudent = true;
1862
+ }
1863
+ var _useState = React.useState(),
1864
+ exams = _useState[0],
1865
+ setExams = _useState[1];
1866
+ var getListExam = function getListExam(userId) {
1867
+ try {
1868
+ setExams([]);
1869
+ var _temp = _catch(function () {
1870
+ return Promise.resolve(getListExamApi({
1871
+ currentPage: 1,
1872
+ pageSize: -1,
1873
+ sortColumnDirection: "DESC",
1874
+ sortColumnName: "CreatedAt",
1875
+ statuses: [ExamStatus.Completed],
1876
+ studentId: !isStudent ? userId : undefined,
1877
+ teacherId: isStudent ? userId : undefined
1878
+ })).then(function (res) {
1879
+ var _res$data;
1880
+ var data = ((_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.items) || [];
1881
+ setExams(data);
1882
+ });
1883
+ }, function (error) {
1884
+ console.log({
1885
+ error: error
1886
+ });
1887
+ });
1888
+ return Promise.resolve(_temp && _temp.then ? _temp.then(function () {}) : void 0);
1889
+ } catch (e) {
1890
+ return Promise.reject(e);
1891
+ }
1892
+ };
1893
+ return {
1894
+ getListExam: getListExam,
1895
+ exams: exams
1896
+ };
1897
+ };
1898
+
1899
+ var useMessageList = function useMessageList(userId, getListExamOptions) {
1900
+ var _useState = React.useState([]),
1901
+ messages = _useState[0],
1902
+ setMessages = _useState[1];
1903
+ var _useState2 = React.useState(MESSAGE_DEFAULT_FILTER),
1904
+ messageFilter = _useState2[0],
1905
+ setMessageFilter = _useState2[1];
1906
+ var getMessageList = function getMessageList(conversationId, examId) {
1907
+ try {
1908
+ var filter = _extends({}, messageFilter, {
1909
+ examId: !examId ? messageFilter.examId : examId
1910
+ });
1911
+ return Promise.resolve(getMessagesByConversation(conversationId, filter)).then(function (res) {
1912
+ var _res$data$items;
1913
+ function _temp(_getListExamOptions) {
1914
+ _getListExamOptions;
1915
+ }
1916
+ setMessages(((_res$data$items = res.data.items) === null || _res$data$items === void 0 ? void 0 : _res$data$items.reverse()) || []);
1917
+ return getListExamOptions ? Promise.resolve(!!getListExamOptions && getListExamOptions(userId)).then(_temp) : _temp(!!getListExamOptions && getListExamOptions(userId));
1918
+ });
1919
+ } catch (e) {
1920
+ return Promise.reject(e);
1921
+ }
1922
+ };
1923
+ var handleMarkReadMessage = function handleMarkReadMessage(conversationId, messageId) {
1924
+ try {
1925
+ var _temp2 = _catch(function () {
1926
+ return Promise.resolve(apiMarkReadMessage(conversationId, messageId)).then(function () {});
1927
+ }, function (error) {
1928
+ console.log({
1929
+ error: error
1930
+ });
1931
+ });
1932
+ return Promise.resolve(_temp2 && _temp2.then ? _temp2.then(function () {}) : void 0);
1933
+ } catch (e) {
1934
+ return Promise.reject(e);
1935
+ }
1936
+ };
1937
+ var resetMessages = function resetMessages() {
1938
+ setMessages([]);
1939
+ };
1940
+ var handleChangeMessageFilter = function handleChangeMessageFilter(filter) {
1941
+ setMessageFilter(_extends({}, messageFilter, filter));
1942
+ };
1943
+ var handleChangeExamId = function handleChangeExamId(value) {
1944
+ setMessageFilter(_extends({}, messageFilter, {
1945
+ examId: value === null || value === void 0 ? void 0 : value.value,
1946
+ currentPage: 1
1947
+ }));
1948
+ };
1949
+ return {
1950
+ messageFilter: messageFilter,
1951
+ messages: messages,
1952
+ getMessageList: getMessageList,
1953
+ setMessages: setMessages,
1954
+ resetMessages: resetMessages,
1955
+ handleChangeMessageFilter: handleChangeMessageFilter,
1956
+ handleChangeExamId: handleChangeExamId,
1957
+ handleMarkReadMessage: handleMarkReadMessage
1958
+ };
1959
+ };
1960
+
1961
+ var useChatContainer = function useChatContainer(props) {
1962
+ var _selectedConversation, _selectedConversation2, _selectedConversation3, _selectedConversation4, _selectedConversation5, _selectedConversation6;
1963
+ var conversation = props.conversation,
1964
+ userId = props.userId,
1965
+ examId = props.examId,
1966
+ onUpdateLastMessage = props.onUpdateLastMessage,
1967
+ onCreateConversationSuccess = props.onCreateConversationSuccess,
1968
+ onGetMessagesSuccess = props.onGetMessagesSuccess;
1969
+ var pusher = React.useRef();
1970
+ var channel = React.useRef();
1971
+ var channelName = React.useRef();
1972
+ var loadingRef = React.useRef(false);
1973
+ var dispatch = reactRedux.useDispatch();
1974
+ var roles = reactRedux.useSelector(function (state) {
1975
+ var _state$common, _state$common$user;
1976
+ return state === null || state === void 0 ? void 0 : (_state$common = state.common) === null || _state$common === void 0 ? void 0 : (_state$common$user = _state$common.user) === null || _state$common$user === void 0 ? void 0 : _state$common$user.roles;
1977
+ }) || [];
1978
+ var isStudent = roles.includes("Student");
1979
+ var _useState = React.useState(),
1980
+ selectedConversation = _useState[0],
1981
+ setSelectedConversation = _useState[1];
1982
+ var _useState2 = React.useState(),
1983
+ message = _useState2[0],
1984
+ setMessage = _useState2[1];
1985
+ var _useState3 = React.useState(MessageStatus.Default),
1986
+ messageStatus = _useState3[0],
1987
+ setMessageStatus = _useState3[1];
1988
+ var _useExamList = useExamList(isStudent),
1989
+ exams = _useExamList.exams,
1990
+ getListExam = _useExamList.getListExam;
1991
+ var _useMessageList = useMessageList(isStudent ? selectedConversation === null || selectedConversation === void 0 ? void 0 : (_selectedConversation = selectedConversation.teacher) === null || _selectedConversation === void 0 ? void 0 : _selectedConversation.id : selectedConversation === null || selectedConversation === void 0 ? void 0 : (_selectedConversation2 = selectedConversation.student) === null || _selectedConversation2 === void 0 ? void 0 : _selectedConversation2.id, !userId ? getListExam : undefined),
1992
+ messages = _useMessageList.messages,
1993
+ messageFilter = _useMessageList.messageFilter,
1994
+ getMessageList = _useMessageList.getMessageList,
1995
+ resetMessages = _useMessageList.resetMessages,
1996
+ handleChangeExamId = _useMessageList.handleChangeExamId,
1997
+ setMessages = _useMessageList.setMessages;
1998
+ var handleAddMessage = function handleAddMessage() {
1999
+ try {
2000
+ var _message$content, _message$content$trim;
2001
+ var _temp2 = function _temp2() {
2002
+ loadingRef.current = false;
2003
+ };
2004
+ if (loadingRef.current) return Promise.resolve();
2005
+ if (messages.some(function (i) {
2006
+ return i.id === 0;
2007
+ })) return Promise.resolve();
2008
+ loadingRef.current = true;
2009
+ if (!(selectedConversation !== null && selectedConversation !== void 0 && selectedConversation.id) || !(message !== null && message !== void 0 && (_message$content = message.content) !== null && _message$content !== void 0 && (_message$content$trim = _message$content.trim()) !== null && _message$content$trim !== void 0 && _message$content$trim.length)) return Promise.resolve();
2010
+ setMessages(function (state) {
2011
+ return [].concat(state, [{
2012
+ id: 0,
2013
+ isStudent: isStudent,
2014
+ content: message.content,
2015
+ examId: message.examId,
2016
+ parentId: message.parentId,
2017
+ parentContent: message.parentContent,
2018
+ exam: message.examId ? {
2019
+ id: message.examId,
2020
+ startTime: moment().format("YYYY-MM-DDTHH:mm:ss"),
2021
+ title: message.examTitle || "",
2022
+ code: "temp",
2023
+ createdAt: moment().format("YYYY-MM-DDTHH:mm:ss")
2024
+ } : undefined,
2025
+ isRead: false,
2026
+ createdAt: moment().format("YYYY-MM-DDTHH:mm:ss")
2027
+ }]);
2028
+ });
2029
+ setMessage(function (state) {
2030
+ if (!state) return undefined;
2031
+ return _extends({}, state, {
2032
+ content: "",
2033
+ parentId: undefined,
2034
+ parentContent: "",
2035
+ examId: messageFilter.examId,
2036
+ examTitle: messageFilter.examId ? message.examTitle : ""
2037
+ });
2038
+ });
2039
+ setMessageStatus(MessageStatus.Sending);
2040
+ var _temp = _catch(function () {
2041
+ return Promise.resolve(apiAddMessage(selectedConversation.id, _extends({}, message, {
2042
+ examId: !!userId ? examId : message.examId
2043
+ }))).then(function () {});
2044
+ }, function (error) {
2045
+ var _error$response;
2046
+ console.log({
2047
+ error: error
2048
+ });
2049
+ setMessageStatus(MessageStatus.SentError);
2050
+ setMessages(function (state) {
2051
+ return [].concat(state.filter(function (i) {
2052
+ return i.id !== 0;
2053
+ }));
2054
+ });
2055
+ reactToastify.toast.error((_error$response = error.response) === null || _error$response === void 0 ? void 0 : _error$response.data);
2056
+ });
2057
+ return Promise.resolve(_temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp));
2058
+ } catch (e) {
2059
+ return Promise.reject(e);
2060
+ }
2061
+ };
2062
+ var handleAddExamMessage = function handleAddExamMessage(examId, title) {
2063
+ setMessage(!!message ? _extends({}, message, {
2064
+ examId: examId,
2065
+ examTitle: title,
2066
+ parentId: undefined,
2067
+ parentContent: ""
2068
+ }) : {
2069
+ examId: examId,
2070
+ content: "",
2071
+ examTitle: title,
2072
+ parentId: undefined,
2073
+ parentContent: ""
2074
+ });
2075
+ };
2076
+ var handleReplyMessage = function handleReplyMessage(parentId, content, examId) {
2077
+ setMessage({
2078
+ content: "",
2079
+ examId: examId,
2080
+ parentId: parentId,
2081
+ parentContent: content,
2082
+ examTitle: message === null || message === void 0 ? void 0 : message.examTitle
2083
+ });
2084
+ };
2085
+ var handleClearReply = function handleClearReply() {
2086
+ message && setMessage(_extends({}, message, {
2087
+ parentId: undefined,
2088
+ examId: messageFilter.examId ? messageFilter.examId : undefined,
2089
+ parentContent: "",
2090
+ examTitle: messageFilter.examId ? message.examTitle : ""
2091
+ }));
2092
+ };
2093
+ var handleChangeInput = function handleChangeInput(text) {
2094
+ setMessage(_extends({}, message, {
2095
+ content: text
2096
+ }));
2097
+ };
2098
+ var handleFilterExam = function handleFilterExam(val) {
2099
+ var _val$label, _val$label$split;
2100
+ var examTitle = val === null || val === void 0 ? void 0 : (_val$label = val.label) === null || _val$label === void 0 ? void 0 : (_val$label$split = _val$label.split(" ")) === null || _val$label$split === void 0 ? void 0 : _val$label$split.slice(0, -1).join(" ");
2101
+ setMessage({
2102
+ examId: val === null || val === void 0 ? void 0 : val.value,
2103
+ content: "",
2104
+ examTitle: examTitle,
2105
+ parentId: undefined,
2106
+ parentContent: ""
2107
+ });
2108
+ handleChangeExamId(val);
2109
+ };
2110
+ var messageList = React.useMemo(function () {
2111
+ if (!selectedConversation) return [];
2112
+ var examCode = undefined;
2113
+ var results = messages.map(function (i, index) {
2114
+ var _i$exam, _i$exam4, _i$exam5, _i$exam6, _i$exam7, _i$exam8, _i$exam9, _i$exam10, _i$exam11;
2115
+ var isFirst = index === 0;
2116
+ var isLast = index === messages.length - 1;
2117
+ if (index > 0 && (messages[index - 1].isStudent !== i.isStudent && !i.isSystemLog || i.isSystemLog || messages[index - 1].examId !== i.examId)) isFirst = true;
2118
+ if (index < messages.length - 1 && (messages[index + 1].isStudent !== i.isStudent && i.examId !== messages[index + 1].examId || i.examId !== messages[index + 1].examId || messages[index + 1].isSystemLog)) isLast = true;
2119
+ var name = isStudent ? "You" : selectedConversation.student.displayName;
2120
+ var content = i === null || i === void 0 ? void 0 : i.content;
2121
+ var showExamHeader = false;
2122
+ if (examCode != ((_i$exam = i.exam) === null || _i$exam === void 0 ? void 0 : _i$exam.code) && !messageFilter.examId && !userId) {
2123
+ var _i$exam2, _i$exam3;
2124
+ if ((_i$exam2 = i.exam) !== null && _i$exam2 !== void 0 && _i$exam2.code) showExamHeader = true;
2125
+ examCode = (_i$exam3 = i.exam) === null || _i$exam3 === void 0 ? void 0 : _i$exam3.code;
2126
+ }
2127
+ switch (i.type) {
2128
+ case MessageSourceType.Exam:
2129
+ switch (i.action) {
2130
+ case MessageAction.Took:
2131
+ content = name + " took the exam " + ((_i$exam4 = i.exam) === null || _i$exam4 === void 0 ? void 0 : _i$exam4.title);
2132
+ break;
2133
+ }
2134
+ break;
2135
+ }
2136
+ return {
2137
+ id: i.id,
2138
+ isMe: isStudent === i.isStudent,
2139
+ avatar: isStudent ? selectedConversation.teacher.avatar : selectedConversation.student.avatar,
2140
+ name: selectedConversation.student.displayName,
2141
+ isRead: i.isRead,
2142
+ createdAt: i.createdAt,
2143
+ content: content,
2144
+ isFirst: isFirst,
2145
+ isLast: isLast,
2146
+ isSystemLog: i.isSystemLog,
2147
+ examCode: !messageFilter.examId && !userId ? (_i$exam5 = i.exam) === null || _i$exam5 === void 0 ? void 0 : _i$exam5.code : undefined,
2148
+ examId: (_i$exam6 = i.exam) === null || _i$exam6 === void 0 ? void 0 : _i$exam6.id,
2149
+ examTitle: (_i$exam7 = i.exam) === null || _i$exam7 === void 0 ? void 0 : _i$exam7.title,
2150
+ type: (_i$exam8 = i.exam) !== null && _i$exam8 !== void 0 && _i$exam8.code ? exports.ChatItemType.Exam : undefined,
2151
+ showExamHeader: showExamHeader,
2152
+ actionLink: ((_i$exam9 = i.exam) === null || _i$exam9 === void 0 ? void 0 : _i$exam9.code) && (isStudent ? "/student/check/live-result?examCode=" + ((_i$exam10 = i.exam) === null || _i$exam10 === void 0 ? void 0 : _i$exam10.code) : "/teacher/exam-status/main?examCode=" + ((_i$exam11 = i.exam) === null || _i$exam11 === void 0 ? void 0 : _i$exam11.code)),
2153
+ parentId: i.parentId,
2154
+ parentContent: i.parentContent,
2155
+ status: i.id ? MessageStatus.Default : messageStatus
2156
+ };
2157
+ });
2158
+ return results;
2159
+ }, [JSON.stringify(messages), JSON.stringify(message), isStudent, JSON.stringify(selectedConversation), messageFilter.examId, isStudent, userId, messageStatus]);
2160
+ var handleNewMessageSent = function handleNewMessageSent(data) {
2161
+ if (!messageFilter.examId || messageFilter.examId && data.examId === messageFilter.examId) {
2162
+ setMessages(function (state) {
2163
+ if (state.some(function (i) {
2164
+ return i.id === 0;
2165
+ })) return [].concat(state.filter(function (i) {
2166
+ return i.id !== 0;
2167
+ }), [data]);else return [].concat(state, [data]);
2168
+ });
2169
+ }
2170
+ onUpdateLastMessage === null || onUpdateLastMessage === void 0 ? void 0 : onUpdateLastMessage(data);
2171
+ };
2172
+ var handleMessageMarkRead = function handleMessageMarkRead(data) {
2173
+ setMessages(function (state) {
2174
+ return [].concat(state.map(function (i) {
2175
+ return _extends({}, i, {
2176
+ isRead: data.includes(i.id || 0) ? true : i.isRead
2177
+ });
2178
+ }));
2179
+ });
2180
+ };
2181
+ var cleanupPusher = function cleanupPusher() {
2182
+ var _pusher$current, _pusher$current2;
2183
+ (_pusher$current = pusher.current) === null || _pusher$current === void 0 ? void 0 : _pusher$current.unbind("new-message-event", handleNewMessageSent);
2184
+ (_pusher$current2 = pusher.current) === null || _pusher$current2 === void 0 ? void 0 : _pusher$current2.unbind("message-read-event", handleMessageMarkRead);
2185
+ channelName.current && pusher.current && pusher.current.unsubscribe(channelName.current);
2186
+ };
2187
+ React.useEffect(function () {
2188
+ if (PUSHER_CONFIG.cluster && PUSHER_CONFIG.key && !!(selectedConversation !== null && selectedConversation !== void 0 && selectedConversation.id)) {
2189
+ var token = getAccessToken();
2190
+ pusher.current = new Pusher(PUSHER_CONFIG.key, {
2191
+ cluster: PUSHER_CONFIG.cluster,
2192
+ authEndpoint: BASE_URL + "/api/auth/pusher",
2193
+ auth: {
2194
+ params: {},
2195
+ headers: {
2196
+ Authorization: "Bearer " + token
2197
+ }
2198
+ }
2199
+ });
2200
+ channelName.current = "presence-conversation-channel-" + (selectedConversation === null || selectedConversation === void 0 ? void 0 : selectedConversation.id);
2201
+ channel.current = pusher.current.subscribe(channelName.current);
2202
+ pusher.current.bind("new-message-event", handleNewMessageSent);
2203
+ pusher.current.bind("message-read-event", handleMessageMarkRead);
2204
+ }
2205
+ return cleanupPusher;
2206
+ }, [selectedConversation === null || selectedConversation === void 0 ? void 0 : selectedConversation.id, messageFilter.examId]);
2207
+ React.useEffect(function () {
2208
+ var getConversation = function getConversation() {
2209
+ try {
2210
+ var _temp6 = function _temp6() {
2211
+ dispatch(setLoading(false));
2212
+ };
2213
+ if ((conversation === null || conversation === void 0 ? void 0 : conversation.id) !== undefined) {
2214
+ setSelectedConversation(conversation);
2215
+ return Promise.resolve();
2216
+ }
2217
+ if (!userId) return Promise.resolve();
2218
+ dispatch(setLoading(true));
2219
+ var _temp5 = _catch(function () {
2220
+ return Promise.resolve(apiGetConversationByUserId(userId)).then(function (res) {
2221
+ function _temp4() {
2222
+ setSelectedConversation(res.data.data);
2223
+ }
2224
+ var _temp3 = function () {
2225
+ if (!res.data.data) {
2226
+ return Promise.resolve(createConversation(userId)).then(function (_createConversation) {
2227
+ res = _createConversation;
2228
+ });
2229
+ }
2230
+ }();
2231
+ return _temp3 && _temp3.then ? _temp3.then(_temp4) : _temp4(_temp3);
2232
+ });
2233
+ }, function (error) {
2234
+ console.log({
2235
+ error: error
2236
+ });
2237
+ });
2238
+ return Promise.resolve(_temp5 && _temp5.then ? _temp5.then(_temp6) : _temp6(_temp5));
2239
+ } catch (e) {
2240
+ return Promise.reject(e);
2241
+ }
2242
+ };
2243
+ getConversation();
2244
+ }, [conversation === null || conversation === void 0 ? void 0 : conversation.id, userId]);
2245
+ var handleMarkRead = function handleMarkRead() {
2246
+ try {
2247
+ if (!(selectedConversation !== null && selectedConversation !== void 0 && selectedConversation.id)) return Promise.resolve();
2248
+ var _temp7 = _catch(function () {
2249
+ return Promise.resolve(apiMarkReadMessage(selectedConversation.id, -1)).then(function () {});
2250
+ }, function (error) {
2251
+ console.log({
2252
+ error: error
2253
+ });
2254
+ });
2255
+ return Promise.resolve(_temp7 && _temp7.then ? _temp7.then(function () {}) : void 0);
2256
+ } catch (e) {
2257
+ return Promise.reject(e);
2258
+ }
2259
+ };
2260
+ var getMessageConversation = function getMessageConversation() {
2261
+ try {
2262
+ var _temp12 = function _temp12() {
2263
+ dispatch(setLoading(false));
2264
+ };
2265
+ if (!roles.length) return Promise.resolve();
2266
+ if ((selectedConversation === null || selectedConversation === void 0 ? void 0 : selectedConversation.id) === undefined) return Promise.resolve();
2267
+ if (messageFilter.currentPage === 1) resetMessages();
2268
+ dispatch(setLoading(true));
2269
+ var _temp11 = _catch(function () {
2270
+ var _temp10 = function () {
2271
+ if (selectedConversation.id === 0) {
2272
+ var _userId = isStudent ? selectedConversation.teacher.id : selectedConversation.student.id;
2273
+ return Promise.resolve(createConversation(_userId)).then(function (res) {
2274
+ var _res$data$data;
2275
+ return Promise.resolve(onCreateConversationSuccess === null || onCreateConversationSuccess === void 0 ? void 0 : onCreateConversationSuccess((_res$data$data = res.data.data) === null || _res$data$data === void 0 ? void 0 : _res$data$data.id)).then(function () {});
2276
+ });
2277
+ } else {
2278
+ var _temp9 = function _temp9() {
2279
+ return Promise.resolve(getMessageList(selectedConversation.id, examId)).then(function () {
2280
+ onGetMessagesSuccess === null || onGetMessagesSuccess === void 0 ? void 0 : onGetMessagesSuccess();
2281
+ });
2282
+ };
2283
+ var _temp8 = function () {
2284
+ if (messageFilter.currentPage === 1) return Promise.resolve(handleMarkRead()).then(function () {});
2285
+ }();
2286
+ return _temp8 && _temp8.then ? _temp8.then(_temp9) : _temp9(_temp8);
2287
+ }
2288
+ }();
2289
+ if (_temp10 && _temp10.then) return _temp10.then(function () {});
2290
+ }, function () {
2291
+ reactToastify.toast.error("Fail to fetch data!");
2292
+ });
2293
+ return Promise.resolve(_temp11 && _temp11.then ? _temp11.then(_temp12) : _temp12(_temp11));
2294
+ } catch (e) {
2295
+ return Promise.reject(e);
2296
+ }
2297
+ };
2298
+ React.useEffect(function () {
2299
+ getMessageConversation();
2300
+ }, [selectedConversation === null || selectedConversation === void 0 ? void 0 : selectedConversation.id, selectedConversation === null || selectedConversation === void 0 ? void 0 : selectedConversation.teacher.id, selectedConversation === null || selectedConversation === void 0 ? void 0 : selectedConversation.student.id, isStudent, roles.length, JSON.stringify(messageFilter), examId]);
2301
+ var userName = isStudent ? selectedConversation === null || selectedConversation === void 0 ? void 0 : (_selectedConversation3 = selectedConversation.teacher) === null || _selectedConversation3 === void 0 ? void 0 : _selectedConversation3.displayName : selectedConversation === null || selectedConversation === void 0 ? void 0 : (_selectedConversation4 = selectedConversation.student) === null || _selectedConversation4 === void 0 ? void 0 : _selectedConversation4.displayName;
2302
+ var userAvatar = isStudent ? selectedConversation === null || selectedConversation === void 0 ? void 0 : (_selectedConversation5 = selectedConversation.teacher) === null || _selectedConversation5 === void 0 ? void 0 : _selectedConversation5.avatar : selectedConversation === null || selectedConversation === void 0 ? void 0 : (_selectedConversation6 = selectedConversation.student) === null || _selectedConversation6 === void 0 ? void 0 : _selectedConversation6.avatar;
2303
+ return {
2304
+ chatHeaderProps: {
2305
+ name: userName || "",
2306
+ avatar: userAvatar || "",
2307
+ options: exams,
2308
+ onChangeOption: handleFilterExam
2309
+ },
2310
+ chatListProps: {
2311
+ messages: messageList,
2312
+ onAddExamMessage: handleAddExamMessage,
2313
+ onReply: handleReplyMessage,
2314
+ onReTrySendMessage: handleAddMessage
2315
+ },
2316
+ inputProps: {
2317
+ text: (message === null || message === void 0 ? void 0 : message.content) || "",
2318
+ parentText: message === null || message === void 0 ? void 0 : message.parentContent,
2319
+ examTitle: message === null || message === void 0 ? void 0 : message.examTitle,
2320
+ onChangeInput: handleChangeInput,
2321
+ onSubmit: handleAddMessage,
2322
+ onClearReply: !messageFilter.examId || message !== null && message !== void 0 && message.parentContent ? handleClearReply : undefined
2323
+ },
2324
+ exams: exams,
2325
+ messageList: messageList,
2326
+ selectedConversation: selectedConversation,
2327
+ messageStatus: messageStatus,
2328
+ messageFilter: messageFilter,
2329
+ getMessageList: getMessageList,
2330
+ handleFilterExam: handleFilterExam,
2331
+ setMessage: setMessage,
2332
+ resetMessages: resetMessages,
2333
+ getMessageConversation: getMessageConversation,
2334
+ handleMarkRead: handleMarkRead
2335
+ };
2336
+ };
2337
+
2338
+ var STUDENT_STATUS_URL = BASE_URL + "/api/user";
2339
+ var getStudentConversationListApi = function getStudentConversationListApi(query) {
2340
+ return api.get(STUDENT_STATUS_URL + "/GetStudentConversations", {
2341
+ params: query
2342
+ });
2343
+ };
2344
+
2345
+ var isFirstLoad = true;
2346
+ var useConversationList = function useConversationList() {
2347
+ var dispatch = reactRedux.useDispatch();
2348
+ var user = reactRedux.useSelector(function (state) {
2349
+ var _state$common;
2350
+ return state === null || state === void 0 ? void 0 : (_state$common = state.common) === null || _state$common === void 0 ? void 0 : _state$common.user;
2351
+ });
2352
+ var roles = (user === null || user === void 0 ? void 0 : user.roles) || [];
2353
+ var isStudent = roles.includes("Student");
2354
+ var _useState = React.useState(),
2355
+ selectedConversation = _useState[0],
2356
+ setSelectedConversation = _useState[1];
2357
+ var _useState2 = React.useState([]),
2358
+ conversations = _useState2[0],
2359
+ setConversations = _useState2[1];
2360
+ var _useState3 = React.useState(CONVERSATION_DEFAULT_FILTER),
2361
+ conversationFilter = _useState3[0],
2362
+ setConversationFilter = _useState3[1];
2363
+ var handleChangeSelectedConversation = function handleChangeSelectedConversation(val) {
2364
+ setSelectedConversation(val);
2365
+ };
2366
+ var handleNewMessageConversation = function handleNewMessageConversation(val) {
2367
+ setConversations(function (state) {
2368
+ var index = state.findIndex(function (i) {
2369
+ return i.id === val.id;
2370
+ });
2371
+ if (index > -1 && JSON.stringify(val) !== JSON.stringify(state[index])) {
2372
+ return [].concat(state.slice(0, index), [val], state.slice(index + 1));
2373
+ } else return [].concat(state, [val]);
2374
+ });
2375
+ setSelectedConversation(function (state) {
2376
+ if (!state) return val;
2377
+ return state;
2378
+ });
2379
+ };
2380
+ var handleMessageReadConversation = function handleMessageReadConversation(id, totalUnread) {
2381
+ setConversations(function (state) {
2382
+ var index = state.findIndex(function (i) {
2383
+ return i.id === id;
2384
+ });
2385
+ if (index === -1 || totalUnread === state[index].totalUnReadMessage) return state;
2386
+ return [].concat(state.slice(0, index), [_extends({}, state[index], {
2387
+ totalUnReadMessage: totalUnread
2388
+ })], state.slice(index + 1));
2389
+ });
2390
+ setSelectedConversation(function (state) {
2391
+ if (!!state) return _extends({}, state, {
2392
+ totalUnReadMessage: totalUnread
2393
+ });
2394
+ return state;
2395
+ });
2396
+ };
2397
+ var handleChangeTextSearch = function handleChangeTextSearch(e) {
2398
+ setConversationFilter(function (state) {
2399
+ return _extends({}, state, {
2400
+ textSearch: e.target.value
2401
+ });
2402
+ });
2403
+ };
2404
+ var handleChangeFilter = function handleChangeFilter(filter) {
2405
+ setConversationFilter(function (state) {
2406
+ return _extends({}, state, filter);
2407
+ });
2408
+ };
2409
+ var formatConversationLastMessageContent = function formatConversationLastMessageContent(conversation) {
2410
+ var _conversation$lastMes;
2411
+ if (!(conversation !== null && conversation !== void 0 && conversation.lastMessage)) return "";
2412
+ var content = conversation.lastMessage.content;
2413
+ switch (conversation.lastMessage.type) {
2414
+ case MessageSourceType.Exam:
2415
+ switch (conversation.lastMessage.action) {
2416
+ case MessageAction.Took:
2417
+ content = (isStudent ? "You" : conversation.student.displayName) + " took the exam " + ((_conversation$lastMes = conversation.lastMessage.exam) === null || _conversation$lastMes === void 0 ? void 0 : _conversation$lastMes.title);
2418
+ break;
2419
+ }
2420
+ break;
2421
+ }
2422
+ return content;
2423
+ };
2424
+ var getConversationList = function getConversationList() {
2425
+ try {
2426
+ var _temp2 = function _temp2() {
2427
+ isFirstLoad && dispatch(setLoading(false));
2428
+ if (isFirstLoad) isFirstLoad = false;
2429
+ };
2430
+ if (!roles.length) return Promise.resolve();
2431
+ isFirstLoad && dispatch(setLoading(true));
2432
+ var _temp = _catch(function () {
2433
+ var apiGetConversations = isStudent ? getListConversation : getStudentConversationListApi;
2434
+ return Promise.resolve(apiGetConversations(conversationFilter)).then(function (res) {
2435
+ var _res$data$items;
2436
+ setConversations(res.data.items || []);
2437
+ !selectedConversation && setSelectedConversation((_res$data$items = res.data.items) === null || _res$data$items === void 0 ? void 0 : _res$data$items[0]);
2438
+ });
2439
+ }, function () {
2440
+ setSelectedConversation(undefined);
2441
+ dispatch(setAlert({
2442
+ type: "danger",
2443
+ message: "Fail to fetch data!"
2444
+ }));
2445
+ });
2446
+ return Promise.resolve(_temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp));
2447
+ } catch (e) {
2448
+ return Promise.reject(e);
2449
+ }
2450
+ };
2451
+ var handleUpdateTotalUnreadMessage = function handleUpdateTotalUnreadMessage() {
2452
+ if (!(selectedConversation !== null && selectedConversation !== void 0 && selectedConversation.id)) return;
2453
+ setConversations(function (state) {
2454
+ var updateIndex = state.findIndex(function (i) {
2455
+ return i.id === selectedConversation.id;
2456
+ });
2457
+ if (updateIndex < 0) return state;
2458
+ return [].concat(state.slice(0, updateIndex), [_extends({}, state[updateIndex], {
2459
+ totalUnReadMessage: 0
2460
+ })], state.slice(updateIndex + 1));
2461
+ });
2462
+ };
2463
+ var handleUpdateLastMessage = function handleUpdateLastMessage(lastMessage) {
2464
+ setConversations(function (state) {
2465
+ var updateIndex = state.findIndex(function (i) {
2466
+ return i.id === lastMessage.conversationId;
2467
+ });
2468
+ if (updateIndex < 0) return state;
2469
+ return [].concat(state.slice(0, updateIndex), [_extends({}, state[updateIndex], {
2470
+ lastMessage: lastMessage
2471
+ })], state.slice(updateIndex + 1));
2472
+ });
2473
+ };
2474
+ var handleCreateConversationSuccess = function handleCreateConversationSuccess(id) {
2475
+ try {
2476
+ if (!selectedConversation) return Promise.resolve();
2477
+ setSelectedConversation(_extends({}, selectedConversation, {
2478
+ id: id
2479
+ }));
2480
+ var index = conversations.findIndex(function (i) {
2481
+ return i.student.id === (selectedConversation === null || selectedConversation === void 0 ? void 0 : selectedConversation.student.id) && i.teacher.id === (selectedConversation === null || selectedConversation === void 0 ? void 0 : selectedConversation.teacher.id);
2482
+ });
2483
+ if (index > -1) {
2484
+ setConversations([].concat(conversations.slice(0, index), [_extends({}, selectedConversation, {
2485
+ id: id
2486
+ })], conversations.slice(index + 1)));
2487
+ }
2488
+ return Promise.resolve();
2489
+ } catch (e) {
2490
+ return Promise.reject(e);
2491
+ }
2492
+ };
2493
+ React.useEffect(function () {
2494
+ getConversationList();
2495
+ }, [JSON.stringify(conversationFilter), roles.length]);
2496
+ React.useEffect(function () {
2497
+ window.document.title = "Q&A";
2498
+ }, []);
2499
+ return {
2500
+ isStudent: isStudent,
2501
+ conversationFilter: conversationFilter,
2502
+ selectedConversation: selectedConversation,
2503
+ conversations: conversations,
2504
+ handleUpdateTotalUnreadMessage: handleUpdateTotalUnreadMessage,
2505
+ handleChangeFilter: handleChangeFilter,
2506
+ handleChangeSelectedConversation: handleChangeSelectedConversation,
2507
+ handleChangeTextSearch: handleChangeTextSearch,
2508
+ setSelectedConversation: setSelectedConversation,
2509
+ handleUpdateLastMessage: handleUpdateLastMessage,
2510
+ getConversationList: getConversationList,
2511
+ formatConversationLastMessageContent: formatConversationLastMessageContent,
2512
+ handleNewMessageConversation: handleNewMessageConversation,
2513
+ handleCreateConversationSuccess: handleCreateConversationSuccess,
2514
+ handleMessageReadConversation: handleMessageReadConversation
2515
+ };
2516
+ };
2517
+
2518
+ var usePusherConversation = function usePusherConversation(onNewMessageConversation, onReadMessageConversation) {
2519
+ var pusher = React.useRef();
2520
+ var channel = React.useRef();
2521
+ var channelName = React.useRef();
2522
+ var userId = reactRedux.useSelector(function (state) {
2523
+ var _state$common, _state$common$user;
2524
+ return state === null || state === void 0 ? void 0 : (_state$common = state.common) === null || _state$common === void 0 ? void 0 : (_state$common$user = _state$common.user) === null || _state$common$user === void 0 ? void 0 : _state$common$user.id;
2525
+ });
2526
+ var handleNewMessageConversationCreated = function handleNewMessageConversationCreated(data) {
2527
+ onNewMessageConversation === null || onNewMessageConversation === void 0 ? void 0 : onNewMessageConversation(data);
2528
+ };
2529
+ var handleReadMessageConversation = function handleReadMessageConversation(data) {
2530
+ onReadMessageConversation === null || onReadMessageConversation === void 0 ? void 0 : onReadMessageConversation(data.id, data.totalUnReadMessage);
2531
+ };
2532
+ var cleanupPusher = function cleanupPusher() {
2533
+ channel.current && channel.current.unbind("message-conversation-event", handleNewMessageConversationCreated);
2534
+ channel.current && channel.current.unbind("message-conversation-read-event", handleReadMessageConversation);
2535
+ channelName.current && pusher.current && pusher.current.unsubscribe(channelName.current);
2536
+ };
2537
+ React.useEffect(function () {
2538
+ if (PUSHER_CONFIG.cluster && PUSHER_CONFIG.key && !!userId) {
2539
+ var token = getAccessToken$1();
2540
+ pusher.current = new Pusher(PUSHER_CONFIG.key, {
2541
+ cluster: PUSHER_CONFIG.cluster,
2542
+ authEndpoint: BASE_URL + "/api/auth/pusher",
2543
+ auth: {
2544
+ params: {},
2545
+ headers: {
2546
+ Authorization: "Bearer " + token
2547
+ }
2548
+ }
2549
+ });
2550
+ channelName.current = "presence-conversation-channel-userId-" + userId;
2551
+ channel.current = pusher.current.subscribe(channelName.current);
2552
+ channel.current.bind("message-conversation-event", handleNewMessageConversationCreated);
2553
+ channel.current.bind("message-conversation-read-event", handleReadMessageConversation);
2554
+ }
2555
+ return cleanupPusher;
2556
+ }, [userId]);
2557
+ return {};
2558
+ };
2559
+
1342
2560
  var historyCore = history.createBrowserHistory();
1343
2561
 
1344
2562
  Object.defineProperty(exports, 'I18nextProvider', {
@@ -1355,22 +2573,26 @@ Object.defineProperty(exports, 'useTranslation', {
1355
2573
  });
1356
2574
  exports.ACCESS_TOKEN = ACCESS_TOKEN;
1357
2575
  exports.BASE_URL = BASE_URL;
2576
+ exports.ChatContainer = ChatContainer;
1358
2577
  exports.CommonAlert = CommonAlert;
1359
2578
  exports.CommonDialog = CommonDialog;
1360
2579
  exports.ConfirmDialog = ConfirmDialog;
1361
2580
  exports.CustomPagination = CustomPagination;
2581
+ exports.CustomSelect = CustomSelect;
1362
2582
  exports.DATE_MIN_VALUE = DATE_MIN_VALUE;
1363
2583
  exports.EXAM_CHANNEL = EXAM_CHANNEL;
2584
+ exports.EXAM_STUDENT_CHANNEL = EXAM_STUDENT_CHANNEL;
1364
2585
  exports.LayoutContext = LayoutContext;
1365
2586
  exports.Loading = Loading;
1366
2587
  exports.Login = Login;
1367
2588
  exports.NotFound = NotFound;
1368
2589
  exports.PUSHER_CONFIG = PUSHER_CONFIG;
1369
2590
  exports.TheLanguageDropdown = TheLanguageDropdown;
1370
- exports.api = api$1;
2591
+ exports.api = api;
1371
2592
  exports.canAccessRoute = canAccess;
1372
2593
  exports.diffFromNow = diffFromNow;
1373
2594
  exports.formatTime = formatTime;
2595
+ exports.getAccessToken = getAccessToken$1;
1374
2596
  exports.historyCore = historyCore;
1375
2597
  exports.i18n = i18n;
1376
2598
  exports.minutesToTimeSpan = minutesToTimeSpan;
@@ -1380,6 +2602,9 @@ exports.setLoading = setLoading;
1380
2602
  exports.setUser = setUser;
1381
2603
  exports.store = store;
1382
2604
  exports.toISOString = toISOString;
2605
+ exports.useChatContainer = useChatContainer;
2606
+ exports.useConversationList = useConversationList;
1383
2607
  exports.useGoogleSignOut = useGoogleSignOut;
2608
+ exports.usePusherConversation = usePusherConversation;
1384
2609
  exports.utcToLocalTime = utcToLocalTime;
1385
2610
  //# sourceMappingURL=index.js.map