2021-09-13 20:41:05 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-02-07 15:18:37 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { RouteProp } from '@react-navigation/native';
|
2022-03-14 13:23:20 +00:00
|
|
|
import { ScrollView, StyleSheet, Text } from 'react-native';
|
2020-05-08 17:36:10 +00:00
|
|
|
import { connect } from 'react-redux';
|
2022-05-26 14:07:17 +00:00
|
|
|
import { BlockContext } from '@rocket.chat/ui-kit';
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, withTheme } from '../theme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2022-06-27 18:46:59 +00:00
|
|
|
import { FormTextInput } from '../containers/TextInput';
|
2022-04-13 20:43:56 +00:00
|
|
|
import KeyboardView from '../containers/KeyboardView';
|
2020-05-08 17:36:10 +00:00
|
|
|
import I18n from '../i18n';
|
|
|
|
import { LISTENER } from '../containers/Toast';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../lib/methods/helpers/events';
|
|
|
|
import scrollPersistTaps from '../lib/methods/helpers/scrollPersistTaps';
|
2020-05-08 17:36:10 +00:00
|
|
|
import { getUserSelector } from '../selectors/login';
|
|
|
|
import Button from '../containers/Button';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2021-08-23 14:08:45 +00:00
|
|
|
import { MultiSelect } from '../containers/UIKit/MultiSelect';
|
2022-03-14 13:23:20 +00:00
|
|
|
import { ICustomFields, IInputsRefs, TParams, ITitle, ILivechat } from '../definitions/ILivechatEditView';
|
2022-05-03 01:19:28 +00:00
|
|
|
import { IApplicationState, IUser } from '../definitions';
|
2022-02-07 15:18:37 +00:00
|
|
|
import { ChatsStackParamList } from '../stacks/types';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from './Styles';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
2022-06-13 20:36:54 +00:00
|
|
|
import { usePermissions } from '../lib/hooks';
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
padding: 16
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
fontSize: 20,
|
|
|
|
paddingVertical: 10,
|
|
|
|
...sharedStyles.textMedium
|
2021-08-23 14:08:45 +00:00
|
|
|
},
|
|
|
|
label: {
|
|
|
|
marginBottom: 10,
|
|
|
|
fontSize: 14,
|
|
|
|
...sharedStyles.textSemibold
|
|
|
|
},
|
|
|
|
multiSelect: {
|
|
|
|
marginBottom: 10
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-07 15:18:37 +00:00
|
|
|
interface ILivechatEditViewProps {
|
2022-05-03 01:19:28 +00:00
|
|
|
user: IUser;
|
2022-02-07 15:18:37 +00:00
|
|
|
navigation: StackNavigationProp<ChatsStackParamList, 'LivechatEditView'>;
|
|
|
|
route: RouteProp<ChatsStackParamList, 'LivechatEditView'>;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-02-17 13:06:31 +00:00
|
|
|
editOmnichannelContact: string[] | undefined;
|
|
|
|
editLivechatRoomCustomfields: string[] | undefined;
|
2022-02-07 15:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Title = ({ title, theme }: ITitle) =>
|
2021-09-13 20:41:05 +00:00
|
|
|
title ? <Text style={[styles.title, { color: themes[theme].titleText }]}>{title}</Text> : null;
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2022-06-13 20:36:54 +00:00
|
|
|
const LivechatEditView = ({ user, navigation, route, theme }: ILivechatEditViewProps) => {
|
2022-02-07 15:18:37 +00:00
|
|
|
const [customFields, setCustomFields] = useState<ICustomFields>({});
|
|
|
|
const [availableUserTags, setAvailableUserTags] = useState<string[]>([]);
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2022-02-07 15:18:37 +00:00
|
|
|
const params = {} as TParams;
|
|
|
|
const inputs = {} as IInputsRefs;
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2022-02-07 15:18:37 +00:00
|
|
|
const livechat = (route.params?.room ?? {}) as ILivechat;
|
2020-06-15 14:00:46 +00:00
|
|
|
const visitor = route.params?.roomUser ?? {};
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2022-06-13 20:36:54 +00:00
|
|
|
const [editOmnichannelContactPermission, editLivechatRoomCustomFieldsPermission] = usePermissions(
|
|
|
|
['edit-omnichannel-contact', 'edit-livechat-room-customfields'],
|
|
|
|
livechat.rid
|
|
|
|
);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getCustomFields = async () => {
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = await Services.getCustomFields();
|
2020-05-08 17:36:10 +00:00
|
|
|
if (result.success && result.customFields?.length) {
|
|
|
|
const visitorCustomFields = result.customFields
|
2022-03-08 14:37:38 +00:00
|
|
|
.filter(field => field.visibility !== 'hidden' && field.scope === 'visitor')
|
|
|
|
.map(field => ({ [field._id]: (visitor.livechatData && visitor.livechatData[field._id]) || '' }))
|
|
|
|
.reduce((ret, field) => ({ ...field, ...ret }), {});
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
const livechatCustomFields = result.customFields
|
2022-03-08 14:37:38 +00:00
|
|
|
.filter(field => field.visibility !== 'hidden' && field.scope === 'room')
|
|
|
|
.map(field => ({ [field._id]: (livechat.livechatData && livechat.livechatData[field._id]) || '' }))
|
|
|
|
.reduce((ret, field) => ({ ...field, ...ret }), {});
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
return setCustomFields({ visitor: visitorCustomFields, livechat: livechatCustomFields });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const [tagParam, setTags] = useState(livechat?.tags || []);
|
2021-08-23 14:08:45 +00:00
|
|
|
const [tagParamSelected, setTagParamSelected] = useState(livechat?.tags || []);
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-08-23 14:08:45 +00:00
|
|
|
const arr = [...tagParam, ...availableUserTags];
|
|
|
|
const uniqueArray = arr.filter((val, i) => arr.indexOf(val) === i);
|
|
|
|
setTags(uniqueArray);
|
2020-05-08 17:36:10 +00:00
|
|
|
}, [availableUserTags]);
|
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
const handleGetTagsList = async (agentDepartments: string[]) => {
|
|
|
|
const tags = await Services.getTagsList();
|
2022-05-03 01:19:28 +00:00
|
|
|
const isAdmin = ['admin', 'livechat-manager'].find(role => user.roles?.includes(role));
|
2020-05-08 17:36:10 +00:00
|
|
|
const availableTags = tags
|
2021-09-13 20:41:05 +00:00
|
|
|
.filter(({ departments }) => isAdmin || departments.length === 0 || departments.some(i => agentDepartments.indexOf(i) > -1))
|
2020-05-08 17:36:10 +00:00
|
|
|
.map(({ name }) => name);
|
|
|
|
setAvailableUserTags(availableTags);
|
|
|
|
};
|
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
const handleGetAgentDepartments = async () => {
|
|
|
|
const result = await Services.getAgentDepartments(visitor?._id);
|
2020-05-08 17:36:10 +00:00
|
|
|
if (result.success) {
|
2022-03-08 15:04:41 +00:00
|
|
|
const agentDepartments = result.departments.map(dept => dept.departmentId);
|
2022-04-28 20:37:25 +00:00
|
|
|
handleGetTagsList(agentDepartments);
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const submit = async () => {
|
2022-02-07 15:18:37 +00:00
|
|
|
const userData = { _id: visitor?._id } as TParams;
|
|
|
|
|
|
|
|
const { rid } = livechat;
|
|
|
|
const sms = livechat?.sms;
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2022-02-07 15:18:37 +00:00
|
|
|
const roomData = { _id: rid } as TParams;
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
if (params.name) {
|
|
|
|
userData.name = params.name;
|
|
|
|
}
|
|
|
|
if (params.email) {
|
|
|
|
userData.email = params.email;
|
|
|
|
}
|
|
|
|
if (params.phone) {
|
|
|
|
userData.phone = params.phone;
|
|
|
|
}
|
|
|
|
|
|
|
|
userData.livechatData = {};
|
|
|
|
Object.entries(customFields?.visitor || {}).forEach(([key]) => {
|
|
|
|
if (params[key] || params[key] === '') {
|
|
|
|
userData.livechatData[key] = params[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (params.topic) {
|
|
|
|
roomData.topic = params.topic;
|
|
|
|
}
|
|
|
|
|
2021-08-23 14:08:45 +00:00
|
|
|
roomData.tags = tagParamSelected;
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
roomData.livechatData = {};
|
|
|
|
Object.entries(customFields?.livechat || {}).forEach(([key]) => {
|
|
|
|
if (params[key] || params[key] === '') {
|
|
|
|
roomData.livechatData[key] = params[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (sms) {
|
|
|
|
delete userData.phone;
|
|
|
|
}
|
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
const { error } = await Services.editLivechat(userData, roomData);
|
2020-05-08 17:36:10 +00:00
|
|
|
if (error) {
|
|
|
|
EventEmitter.emit(LISTENER, { message: error });
|
|
|
|
} else {
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Saved') });
|
|
|
|
navigation.goBack();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-07 15:18:37 +00:00
|
|
|
const onChangeText = (key: string, text: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
params[key] = text;
|
|
|
|
};
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-02-07 15:18:37 +00:00
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Edit')
|
|
|
|
});
|
2022-04-28 20:37:25 +00:00
|
|
|
handleGetAgentDepartments();
|
2020-05-08 17:36:10 +00:00
|
|
|
getCustomFields();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<KeyboardView
|
|
|
|
style={{ backgroundColor: themes[theme].auxiliaryBackground }}
|
|
|
|
contentContainerStyle={sharedStyles.container}
|
2021-09-13 20:41:05 +00:00
|
|
|
keyboardVerticalOffset={128}>
|
2020-06-15 14:00:46 +00:00
|
|
|
<ScrollView {...scrollPersistTaps} style={styles.container}>
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Title title={visitor?.username} theme={theme} />
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2020-05-08 17:36:10 +00:00
|
|
|
label={I18n.t('Name')}
|
|
|
|
defaultValue={visitor?.name}
|
|
|
|
onChangeText={text => onChangeText('name', text)}
|
2021-09-13 20:41:05 +00:00
|
|
|
onSubmitEditing={() => {
|
2022-02-07 15:18:37 +00:00
|
|
|
inputs.name?.focus();
|
2021-09-13 20:41:05 +00:00
|
|
|
}}
|
2022-06-13 20:36:54 +00:00
|
|
|
editable={!!editOmnichannelContactPermission}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2020-05-08 17:36:10 +00:00
|
|
|
label={I18n.t('Email')}
|
2021-09-13 20:41:05 +00:00
|
|
|
inputRef={e => {
|
|
|
|
inputs.name = e;
|
|
|
|
}}
|
2020-05-08 17:36:10 +00:00
|
|
|
defaultValue={visitor?.visitorEmails && visitor?.visitorEmails[0]?.address}
|
|
|
|
onChangeText={text => onChangeText('email', text)}
|
2021-09-13 20:41:05 +00:00
|
|
|
onSubmitEditing={() => {
|
2022-02-07 15:18:37 +00:00
|
|
|
inputs.phone?.focus();
|
2021-09-13 20:41:05 +00:00
|
|
|
}}
|
2022-06-13 20:36:54 +00:00
|
|
|
editable={!!editOmnichannelContactPermission}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2020-05-08 17:36:10 +00:00
|
|
|
label={I18n.t('Phone')}
|
2021-09-13 20:41:05 +00:00
|
|
|
inputRef={e => {
|
|
|
|
inputs.phone = e;
|
|
|
|
}}
|
2020-05-08 17:36:10 +00:00
|
|
|
defaultValue={visitor?.phone && visitor?.phone[0]?.phoneNumber}
|
|
|
|
onChangeText={text => onChangeText('phone', text)}
|
|
|
|
onSubmitEditing={() => {
|
|
|
|
const keys = Object.keys(customFields?.visitor || {});
|
|
|
|
if (keys.length > 0) {
|
2021-08-23 13:43:51 +00:00
|
|
|
const key = keys[0];
|
2022-02-07 15:18:37 +00:00
|
|
|
inputs[key]?.focus();
|
2020-05-08 17:36:10 +00:00
|
|
|
} else {
|
2022-02-07 15:18:37 +00:00
|
|
|
inputs.topic?.focus();
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
|
|
|
}}
|
2022-06-13 20:36:54 +00:00
|
|
|
editable={!!editOmnichannelContactPermission}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
{Object.entries(customFields?.visitor || {}).map(([key, value], index, array) => (
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2020-05-08 17:36:10 +00:00
|
|
|
label={key}
|
|
|
|
defaultValue={value}
|
2021-09-13 20:41:05 +00:00
|
|
|
inputRef={e => {
|
|
|
|
inputs[key] = e;
|
|
|
|
}}
|
2020-05-08 17:36:10 +00:00
|
|
|
onChangeText={text => onChangeText(key, text)}
|
|
|
|
onSubmitEditing={() => {
|
|
|
|
if (array.length - 1 > index) {
|
2022-02-07 15:18:37 +00:00
|
|
|
return inputs[array[index + 1][0]]?.focus();
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2022-02-07 15:18:37 +00:00
|
|
|
inputs.topic?.focus();
|
2020-05-08 17:36:10 +00:00
|
|
|
}}
|
2022-06-13 20:36:54 +00:00
|
|
|
editable={!!editOmnichannelContactPermission}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
))}
|
2021-09-13 20:41:05 +00:00
|
|
|
<Title title={I18n.t('Conversation')} theme={theme} />
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2020-05-08 17:36:10 +00:00
|
|
|
label={I18n.t('Topic')}
|
2021-09-13 20:41:05 +00:00
|
|
|
inputRef={e => {
|
|
|
|
inputs.topic = e;
|
|
|
|
}}
|
2020-05-08 17:36:10 +00:00
|
|
|
defaultValue={livechat?.topic}
|
|
|
|
onChangeText={text => onChangeText('topic', text)}
|
2022-06-13 20:36:54 +00:00
|
|
|
editable={!!editLivechatRoomCustomFieldsPermission}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
<Text style={[styles.label, { color: themes[theme].titleText }]}>{I18n.t('Tags')}</Text>
|
2021-08-23 14:08:45 +00:00
|
|
|
<MultiSelect
|
2022-02-07 15:18:37 +00:00
|
|
|
options={tagParam.map((tag: string) => ({ text: { text: tag }, value: tag }))}
|
|
|
|
onChange={({ value }: { value: string[] }) => {
|
2021-08-23 14:08:45 +00:00
|
|
|
setTagParamSelected([...value]);
|
2020-05-08 17:36:10 +00:00
|
|
|
}}
|
2021-08-23 14:08:45 +00:00
|
|
|
placeholder={{ text: I18n.t('Tags') }}
|
|
|
|
value={tagParamSelected}
|
2022-05-26 14:07:17 +00:00
|
|
|
context={BlockContext.FORM}
|
2021-08-23 14:08:45 +00:00
|
|
|
multiselect
|
2022-06-13 20:36:54 +00:00
|
|
|
disabled={!editLivechatRoomCustomFieldsPermission}
|
2021-08-23 14:08:45 +00:00
|
|
|
inputStyle={styles.multiSelect}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
|
2022-02-07 15:18:37 +00:00
|
|
|
{Object.entries(customFields?.livechat || {}).map(([key, value], index, array: any) => (
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2020-05-08 17:36:10 +00:00
|
|
|
label={key}
|
|
|
|
defaultValue={value}
|
2021-09-13 20:41:05 +00:00
|
|
|
inputRef={e => {
|
|
|
|
inputs[key] = e;
|
|
|
|
}}
|
2020-05-08 17:36:10 +00:00
|
|
|
onChangeText={text => onChangeText(key, text)}
|
|
|
|
onSubmitEditing={() => {
|
|
|
|
if (array.length - 1 > index) {
|
2022-02-07 15:18:37 +00:00
|
|
|
return inputs[array[index + 1]]?.focus();
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
|
|
|
submit();
|
|
|
|
}}
|
2022-06-13 20:36:54 +00:00
|
|
|
editable={!!editLivechatRoomCustomFieldsPermission}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
|
2022-05-20 16:37:57 +00:00
|
|
|
<Button title={I18n.t('Save')} onPress={submit} />
|
2020-05-08 17:36:10 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
</ScrollView>
|
|
|
|
</KeyboardView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-07 15:18:37 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-05-08 17:36:10 +00:00
|
|
|
server: state.server.server,
|
2022-06-13 20:36:54 +00:00
|
|
|
user: getUserSelector(state)
|
2020-05-08 17:36:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withTheme(LivechatEditView));
|