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';
|
|
|
|
import { ScrollView, StyleSheet, Text, TextInput as RNTextInput } from 'react-native';
|
2020-05-08 17:36:10 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-08-23 14:08:45 +00:00
|
|
|
import { BLOCK_CONTEXT } from '@rocket.chat/ui-kit';
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import TextInput from '../containers/TextInput';
|
|
|
|
import KeyboardView from '../presentation/KeyboardView';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { LISTENER } from '../containers/Toast';
|
|
|
|
import EventEmitter from '../utils/events';
|
|
|
|
import scrollPersistTaps from '../utils/scrollPersistTaps';
|
|
|
|
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-02-07 15:18:37 +00:00
|
|
|
import { IVisitor } from '../definitions/IVisitor';
|
|
|
|
import { ITagsOmnichannel } from '../definitions/ITagsOmnichannel';
|
|
|
|
import { IApplicationState, ISubscription } from '../definitions';
|
|
|
|
import { ChatsStackParamList } from '../stacks/types';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from './Styles';
|
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 ITitle {
|
|
|
|
title: string;
|
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IField {
|
|
|
|
_id: string;
|
|
|
|
visibility: string;
|
|
|
|
scope: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IInputs {
|
|
|
|
[key: string]: string | string[] | undefined;
|
|
|
|
name: string;
|
|
|
|
email: string;
|
|
|
|
phone?: string;
|
|
|
|
topic: string;
|
|
|
|
tag: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
type TParams = IVisitor & IInputs;
|
|
|
|
|
|
|
|
interface ILivechat extends ISubscription {
|
|
|
|
// Param dynamic depends on server
|
|
|
|
sms?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IInputsRefs {
|
|
|
|
[index: string]: RNTextInput | null;
|
|
|
|
name: RNTextInput | null;
|
|
|
|
phone: RNTextInput | null;
|
|
|
|
topic: RNTextInput | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ICustomFields {
|
|
|
|
visitor?: { [key: string]: string };
|
|
|
|
livechat?: { [key: string]: string };
|
|
|
|
}
|
|
|
|
interface ILivechatEditViewProps {
|
|
|
|
// TODO: Refactor when migrate models
|
|
|
|
user: any;
|
|
|
|
navigation: StackNavigationProp<ChatsStackParamList, 'LivechatEditView'>;
|
|
|
|
route: RouteProp<ChatsStackParamList, 'LivechatEditView'>;
|
|
|
|
theme: string;
|
|
|
|
editOmnichannelContact: string[];
|
|
|
|
editLivechatRoomCustomfields: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-07 15:18:37 +00:00
|
|
|
const LivechatEditView = ({
|
|
|
|
user,
|
|
|
|
navigation,
|
|
|
|
route,
|
|
|
|
theme,
|
|
|
|
editOmnichannelContact,
|
|
|
|
editLivechatRoomCustomfields
|
|
|
|
}: ILivechatEditViewProps) => {
|
|
|
|
const [customFields, setCustomFields] = useState<ICustomFields>({});
|
|
|
|
const [availableUserTags, setAvailableUserTags] = useState<string[]>([]);
|
2021-08-23 13:42:09 +00:00
|
|
|
const [permissions, setPermissions] = useState([]);
|
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
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getCustomFields = async () => {
|
2022-02-15 18:50:55 +00:00
|
|
|
const result: any = await RocketChat.getCustomFields();
|
2020-05-08 17:36:10 +00:00
|
|
|
if (result.success && result.customFields?.length) {
|
|
|
|
const visitorCustomFields = result.customFields
|
2022-02-07 15:18:37 +00:00
|
|
|
.filter((field: IField) => field.visibility !== 'hidden' && field.scope === 'visitor')
|
|
|
|
.map((field: IField) => ({ [field._id]: (visitor.livechatData && visitor.livechatData[field._id]) || '' }))
|
|
|
|
.reduce((ret: IField, field: IField) => ({ ...field, ...ret }));
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
const livechatCustomFields = result.customFields
|
2022-02-07 15:18:37 +00:00
|
|
|
.filter((field: IField) => field.visibility !== 'hidden' && field.scope === 'room')
|
|
|
|
.map((field: IField) => ({ [field._id]: (livechat.livechatData && livechat.livechatData[field._id]) || '' }))
|
|
|
|
.reduce((ret: IField, field: IField) => ({ ...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-02-07 15:18:37 +00:00
|
|
|
const getTagsList = async (agentDepartments: string[]) => {
|
|
|
|
const tags: ITagsOmnichannel[] = await RocketChat.getTagsList();
|
2020-05-08 17:36:10 +00:00
|
|
|
const isAdmin = ['admin', 'livechat-manager'].find(role => user.roles.includes(role));
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getAgentDepartments = async () => {
|
2020-05-08 17:36:10 +00:00
|
|
|
const result = await RocketChat.getAgentDepartments(visitor?._id);
|
|
|
|
if (result.success) {
|
2022-02-07 15:18:37 +00:00
|
|
|
const agentDepartments = result.departments.map((dept: { departmentId: string }) => dept.departmentId);
|
2020-05-08 17:36:10 +00:00
|
|
|
getTagsList(agentDepartments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { error } = await RocketChat.editLivechat(userData, roomData);
|
|
|
|
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
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getPermissions = async () => {
|
2021-08-23 13:42:09 +00:00
|
|
|
const permissionsArray = await RocketChat.hasPermission([editOmnichannelContact, editLivechatRoomCustomfields], livechat.rid);
|
|
|
|
setPermissions(permissionsArray);
|
|
|
|
};
|
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
useEffect(() => {
|
2022-02-07 15:18:37 +00:00
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Edit')
|
|
|
|
});
|
2020-05-08 17:36:10 +00:00
|
|
|
getAgentDepartments();
|
|
|
|
getCustomFields();
|
2021-08-23 13:42:09 +00:00
|
|
|
getPermissions();
|
2020-05-08 17:36:10 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
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} />
|
2020-05-08 17:36:10 +00:00
|
|
|
<TextInput
|
|
|
|
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
|
|
|
}}
|
2020-05-08 17:36:10 +00:00
|
|
|
theme={theme}
|
2021-08-23 13:42:09 +00:00
|
|
|
editable={!!permissions[0]}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
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
|
|
|
}}
|
2020-05-08 17:36:10 +00:00
|
|
|
theme={theme}
|
2021-08-23 13:42:09 +00:00
|
|
|
editable={!!permissions[0]}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
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
|
|
|
}
|
|
|
|
}}
|
|
|
|
theme={theme}
|
2021-08-23 13:42:09 +00:00
|
|
|
editable={!!permissions[0]}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
{Object.entries(customFields?.visitor || {}).map(([key, value], index, array) => (
|
|
|
|
<TextInput
|
|
|
|
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
|
|
|
}}
|
|
|
|
theme={theme}
|
2021-08-23 13:42:09 +00:00
|
|
|
editable={!!permissions[0]}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
))}
|
2021-09-13 20:41:05 +00:00
|
|
|
<Title title={I18n.t('Conversation')} theme={theme} />
|
2020-05-08 17:36:10 +00:00
|
|
|
<TextInput
|
|
|
|
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)}
|
|
|
|
theme={theme}
|
2021-08-23 13:42:09 +00:00
|
|
|
editable={!!permissions[1]}
|
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}
|
|
|
|
context={BLOCK_CONTEXT.FORM}
|
|
|
|
multiselect
|
2020-05-08 17:36:10 +00:00
|
|
|
theme={theme}
|
2021-08-23 14:08:45 +00:00
|
|
|
disabled={!permissions[1]}
|
|
|
|
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) => (
|
2020-05-08 17:36:10 +00:00
|
|
|
<TextInput
|
|
|
|
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();
|
|
|
|
}}
|
|
|
|
theme={theme}
|
2021-08-23 13:42:09 +00:00
|
|
|
editable={!!permissions[1]}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
<Button title={I18n.t('Save')} onPress={submit} theme={theme} />
|
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,
|
2021-08-23 13:42:09 +00:00
|
|
|
user: getUserSelector(state),
|
|
|
|
editOmnichannelContact: state.permissions['edit-omnichannel-contact'],
|
|
|
|
editLivechatRoomCustomfields: state.permissions['edit-livechat-room-customfields']
|
2020-05-08 17:36:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withTheme(LivechatEditView));
|