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
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
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';
|
2020-08-21 13:38:50 +00:00
|
|
|
import { withTheme } from '../../../theme';
|
|
|
|
import UnreadBadge from '../../../presentation/UnreadBadge';
|
|
|
|
import RocketChat from '../../../lib/rocketchat';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { changeLivechatStatus, isOmnichannelStatusAvailable } from '../lib';
|
2020-08-21 13:38:50 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const OmnichannelStatus = memo(({ searching, goQueue, theme, queueSize, inquiryEnabled, user }) => {
|
2020-08-21 13:38:50 +00:00
|
|
|
if (searching > 0 || !(RocketChat.isOmnichannelModuleAvailable() && user?.roles?.includes('livechat-agent'))) {
|
|
|
|
return null;
|
|
|
|
}
|
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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
OmnichannelStatus.propTypes = {
|
|
|
|
searching: PropTypes.bool,
|
|
|
|
goQueue: PropTypes.func,
|
|
|
|
queueSize: PropTypes.number,
|
|
|
|
inquiryEnabled: PropTypes.bool,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
user: PropTypes.shape({
|
|
|
|
roles: PropTypes.array,
|
|
|
|
statusLivechat: PropTypes.string
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withTheme(OmnichannelStatus);
|