import React, { useEffect, useState } from 'react' import { Messages as MessagesController, useSession, useUtils, useLanguage, ToastType, useToast } from 'ordering-components/native' import { useTheme } from 'styled-components/native'; import { launchImageLibrary } from 'react-native-image-picker' import { GiftedChat, Actions, ActionsProps, InputToolbar, Composer, Send, Bubble, MessageImage, InputToolbarProps, ComposerProps } from 'react-native-gifted-chat' import { USER_TYPE } from '../../config/constants' import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons' import { OIcon, OIconButton, OText } from '../shared' import { TouchableOpacity, ActivityIndicator, StyleSheet, View, Platform, Keyboard, I18nManager } from 'react-native' import { Header, TitleHeader, Wrapper } from './styles' import { MessagesParams } from '../../types' import { useWindowDimensions } from 'react-native' import { useSafeAreaInsets } from 'react-native-safe-area-context' import { getOrderStatus } from '../../utils'; const MessagesUI = (props: MessagesParams) => { const { type, order, messages, image, message, messagesToShow, sendMessage, setMessage, handleSend, setImage, readMessages, onClose, } = props const [{ user }] = useSession() const [{ parseDate }] = useUtils() const [, t] = useLanguage() const [, { showToast }] = useToast(); const [formattedMessages, setFormattedMessages] = useState>([]) const [isKeyboardShow, setIsKeyboardShow] = useState(false) const { height } = useWindowDimensions(); const { top, bottom } = useSafeAreaInsets(); const theme = useTheme(); const onChangeMessage = (val: string) => { setMessage && setMessage(val) } const removeImage = () => { setImage && setImage(null) } const imgOptions = { mediaType: 'photo', maxHeight: 300, maxWidth: 300, includeBase64: true, } const handleImagePicker = () => { launchImageLibrary(imgOptions, (response: any) => { if (response.didCancel) { console.log('User cancelled image picker'); } else if (response.errorMessage) { console.log('ImagePicker Error: ', response.errorMessage); showToast(ToastType.Error, response.errorMessage); } else { if (response?.assets?.length > 0) { const image = response?.assets[0] const url = `data:${image.type};base64,${image.base64}` setImage && setImage(url); } else { showToast(ToastType.Error, t('IMAGE_NOT_FOUND', 'Image not found')); } } }); }; const onSubmit = (values: any) => { handleSend && handleSend() setImage && setImage(null) setMessage && setMessage('') } const messageConsole = (message: any) => { return message.change?.attribute !== 'driver_id' ? `${t('ORDER', 'Order')} ${message.change.attribute} ${t('CHANGED_FROM', 'Changed from')} ${message.change.old !== null && t(getOrderStatus(parseInt(message.change.old, 10)))} ${t('TO', 'to')} ${t(getOrderStatus(parseInt(message.change.new, 10)))}` : message.change.new ? `${message.driver?.name} ${message.driver?.lastname !== null ? message.driver.lastname : ''} ${t('WAS_ASSIGNED_AS_DRIVER', 'Was assigned as driver')} ${message.comment ? message.comment.length : ''}` : `${t('DRIVER_UNASSIGNED', 'Driver unassigned')}` } useEffect(() => { let newMessages: Array = [] const console = `${t('ORDER_PLACED_FOR', 'Order placed for')} ${parseDate(order?.created_at)} ${t('VIA', 'Via')} ${order?.app_id ? t(order?.app_id.toUpperCase(), order?.app_id) : t('OTHER', 'Other')}` const firstMessage = { _id: 0, text: console, createdAt: order?.created_at, system: true } messages.messages.map((message: any) => { let newMessage if (message.type !== 0 && (messagesToShow?.messages?.length || (message?.can_see?.includes('2')) || (message?.can_see?.includes('4') && type === USER_TYPE.DRIVER))) { newMessage = { _id: message.id, text: message.type === 1 ? messageConsole(message) : message.comment, createdAt: message.type !== 0 && message.created_at, image: message.source, system: message.type === 1, user: { _id: message.author.id, name: message.author.name, avatar: message.author.id !== user.id && type === USER_TYPE.DRIVER ? order?.driver?.photo : order?.business?.logo } } } if (message.type === 0) { newMessage = firstMessage } newMessages = [...newMessages, newMessage] }) setFormattedMessages([...newMessages.reverse()]) }, [messages.messages.length]) useEffect(() => { const keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', () => { setIsKeyboardShow(true) }) const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => { setIsKeyboardShow(false) }) return () => { keyboardDidShowListener.remove() keyboardDidHideListener.remove() } }, []) const RenderActions = (props: any) => { return ( handleImagePicker(), }} containerStyle={styles.containerActions} optionTintColor='#222845' icon={() => ( <> {image && ( removeImage()} > )} )} /> ) } const renderInputToolbar = (props: any) => ( ) const renderComposer = (props: any) => ( ) const renderSend = (props: any) => ( : } disabled={(sendMessage?.loading || (message === '' && !image) || messages?.loading)} disabledColor={theme.colors.white} /> ) const renderBubble = (props: any) => ( ) const renderMessageImage = (props: any) => ( ) const renderScrollToBottomComponent = () => ( ) return (
{type === USER_TYPE.DRIVER ? order?.driver?.name : order?.business?.name} {type === USER_TYPE.DRIVER ? t('DRIVER', 'Driver') : t('BUSINESS', 'Business')}
renderScrollToBottomComponent()} messagesContainerStyle={{ paddingVertical: 18, paddingHorizontal: 28, }} isLoadingEarlier={messages.loading} renderLoading={() => } keyboardShouldPersistTaps='handled' />
) } const styles = StyleSheet.create({ containerActions: { width: 44, height: 44, alignItems: 'center', justifyContent: 'center', marginHorizontal: 4, marginBottom: 0, }, containerSend: { width: 64, height: 44, alignItems: 'center', justifyContent: 'center', marginHorizontal: 4 } }) export const Messages = (props: MessagesParams) => { const MessagesProps = { ...props, UIComponent: MessagesUI } return }