2022-04-15 03:11:36 +00:00
|
|
|
import React, { memo, useEffect, useState } from 'react';
|
|
|
|
import { Switch, View } from 'react-native';
|
|
|
|
|
|
|
|
import * as List from '../../../../containers/List';
|
|
|
|
import styles from './styles';
|
|
|
|
import { SWITCH_TRACK_COLOR, themes } from '../../../../lib/constants';
|
|
|
|
import { useTheme } from '../../../../theme';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { IUser } from '../../../../definitions';
|
|
|
|
import { showConfirmationAlert } from '../../../../lib/methods/helpers/info';
|
2022-04-15 03:11:36 +00:00
|
|
|
import I18n from '../../../../i18n';
|
|
|
|
import { changeLivechatStatus, isOmnichannelStatusAvailable } from '../../lib';
|
|
|
|
import OmnichannelQueue from './OmnichannelQueue';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { isOmnichannelModuleAvailable } from '../../../../lib/methods';
|
2022-04-15 03:11:36 +00:00
|
|
|
|
|
|
|
interface IOmnichannelStatus {
|
|
|
|
searching: boolean;
|
|
|
|
goQueue: () => void;
|
|
|
|
queueSize: number;
|
|
|
|
inquiryEnabled: boolean;
|
|
|
|
user: IUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
const OmnichannelStatus = memo(({ searching, goQueue, queueSize, user }: IOmnichannelStatus) => {
|
|
|
|
const { theme } = useTheme();
|
|
|
|
const [status, setStatus] = useState(isOmnichannelStatusAvailable(user));
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setStatus(isOmnichannelStatusAvailable(user));
|
|
|
|
}, [user.statusLivechat]);
|
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
if (searching || !(isOmnichannelModuleAvailable() && user?.roles?.includes('livechat-agent'))) {
|
2022-04-15 03:11:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const toggleLivechat = async () => {
|
|
|
|
// if not-available, prompt to change to available
|
|
|
|
if (!isOmnichannelStatusAvailable(user)) {
|
|
|
|
showConfirmationAlert({
|
|
|
|
message: I18n.t('Omnichannel_enable_alert'),
|
|
|
|
confirmationText: I18n.t('Yes'),
|
|
|
|
onPress: async () => {
|
|
|
|
try {
|
|
|
|
await changeLivechatStatus();
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
setStatus(v => !v);
|
|
|
|
await changeLivechatStatus();
|
|
|
|
} catch {
|
|
|
|
setStatus(v => !v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<List.Item
|
|
|
|
title='Omnichannel'
|
|
|
|
color={themes[theme].bodyText}
|
|
|
|
onPress={toggleLivechat}
|
|
|
|
right={() => (
|
|
|
|
<View style={styles.omnichannelRightContainer}>
|
|
|
|
<Switch value={status} trackColor={SWITCH_TRACK_COLOR} onValueChange={toggleLivechat} />
|
|
|
|
</View>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
{status ? <OmnichannelQueue queueSize={queueSize} onPress={goQueue} /> : null}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default OmnichannelStatus;
|