import { notification } from "antd"; import parsePhoneNumber from "libphonenumber-js"; import React from "react"; import { useTranslation } from "react-i18next"; import { connect } from "react-redux"; import { openChatByPhone } from "../../redux/messaging/messaging.actions"; import PhoneNumberFormatter from "../../utils/PhoneFormatter"; import { createStructuredSelector } from "reselect"; import { selectBodyshop } from "../../redux/user/user.selectors"; const mapStateToProps = createStructuredSelector({ bodyshop: selectBodyshop, }); const mapDispatchToProps = (dispatch) => ({ openChatByPhone: (phone) => dispatch(openChatByPhone(phone)), }); export function ChatOpenButton({ bodyshop, phone, jobid, openChatByPhone }) { const { t } = useTranslation(); if (!phone) return <>; if (!bodyshop.messagingservicesid) return {phone}; return ( { e.stopPropagation(); const p = parsePhoneNumber(phone, "CA"); if (p && p.isValid()) { openChatByPhone({ phone_num: p.formatInternational(), jobid: jobid }); } else { notification["error"]({ message: t("messaging.error.invalidphone") }); } }} > {phone} ); } export default connect(mapStateToProps, mapDispatchToProps)(ChatOpenButton);