2021-09-13 20:41:05 +00:00
|
|
|
import React, { memo, useEffect, useState } from 'react';
|
|
|
|
import { Switch, View } from 'react-native';
|
2020-08-21 13:38:50 +00:00
|
|
|
|
2021-01-21 19:52:26 +00:00
|
|
|
import * as List from '../../../containers/List';
|
2020-08-28 19:41:08 +00:00
|
|
|
import styles from '../../../views/RoomsListView/styles';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { SWITCH_TRACK_COLOR, themes } from '../../../constants/colors';
|
2022-03-07 21:16:20 +00:00
|
|
|
import { useTheme } from '../../../theme';
|
2020-08-21 13:38:50 +00:00
|
|
|
import UnreadBadge from '../../../presentation/UnreadBadge';
|
|
|
|
import RocketChat from '../../../lib/rocketchat';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { changeLivechatStatus, isOmnichannelStatusAvailable } from '../lib';
|
2022-03-07 21:16:20 +00:00
|
|
|
import { IUser } from '../../../definitions/IUser';
|
2020-08-21 13:38:50 +00:00
|
|
|
|
2022-03-07 21:16:20 +00:00
|
|
|
interface IOmnichannelStatus {
|
|
|
|
searching: boolean;
|
|
|
|
goQueue: () => void;
|
|
|
|
queueSize: number;
|
|
|
|
inquiryEnabled: boolean;
|
|
|
|
user: IUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
const OmnichannelStatus = memo(({ searching, goQueue, queueSize, inquiryEnabled, user }: IOmnichannelStatus) => {
|
|
|
|
if (searching || !(RocketChat.isOmnichannelModuleAvailable() && user?.roles?.includes('livechat-agent'))) {
|
2020-08-21 13:38:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-03-07 21:16:20 +00:00
|
|
|
const { theme } = useTheme();
|
2020-08-28 19:41:08 +00:00
|
|
|
const [status, setStatus] = useState(isOmnichannelStatusAvailable(user));
|
2020-08-21 13:38:50 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-08-28 19:41:08 +00:00
|
|
|
setStatus(isOmnichannelStatusAvailable(user));
|
2020-08-21 13:38:50 +00:00
|
|
|
}, [user.statusLivechat]);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const toggleLivechat = async () => {
|
2020-08-21 13:38:50 +00:00
|
|
|
try {
|
|
|
|
setStatus(v => !v);
|
2020-08-28 19:41:08 +00:00
|
|
|
await changeLivechatStatus();
|
2020-08-21 13:38:50 +00:00
|
|
|
} catch {
|
|
|
|
setStatus(v => !v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-01-21 19:52:26 +00:00
|
|
|
<>
|
|
|
|
<List.Item
|
|
|
|
title='Omnichannel'
|
|
|
|
left={() => <List.Icon name='omnichannel' />}
|
|
|
|
color={themes[theme].auxiliaryText}
|
|
|
|
onPress={goQueue}
|
|
|
|
right={() => (
|
|
|
|
<View style={styles.omnichannelRightContainer}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{inquiryEnabled ? <UnreadBadge style={styles.queueIcon} unread={queueSize} /> : null}
|
|
|
|
<Switch value={status} trackColor={SWITCH_TRACK_COLOR} onValueChange={toggleLivechat} />
|
2021-01-21 19:52:26 +00:00
|
|
|
</View>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
2020-08-21 13:38:50 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-03-07 21:16:20 +00:00
|
|
|
export default OmnichannelStatus;
|