From 55120c0ab027beb4655685e048e61318ae0e9347 Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Tue, 23 Mar 2021 15:20:09 -0700 Subject: [PATCH] CSI updates. --- ...bs-detail-header-actions.csi.component.jsx | 168 +++++++++++++----- 1 file changed, 119 insertions(+), 49 deletions(-) diff --git a/client/src/components/jobs-detail-header-actions/jobs-detail-header-actions.csi.component.jsx b/client/src/components/jobs-detail-header-actions/jobs-detail-header-actions.csi.component.jsx index b01e0b352..b2ab847ac 100644 --- a/client/src/components/jobs-detail-header-actions/jobs-detail-header-actions.csi.component.jsx +++ b/client/src/components/jobs-detail-header-actions/jobs-detail-header-actions.csi.component.jsx @@ -14,6 +14,11 @@ import { TemplateList } from "../../utils/TemplateConstants"; import { logImEXEvent } from "../../firebase/firebase.utils"; import { DateTimeFormatter } from "../../utils/DateFormatter"; import { Link } from "react-router-dom"; +import parsePhoneNumber from "libphonenumber-js"; +import { + openChatByPhone, + setMessage, +} from "../../redux/messaging/messaging.actions"; const mapStateToProps = createStructuredSelector({ //currentUser: selectCurrentUser' @@ -21,12 +26,16 @@ const mapStateToProps = createStructuredSelector({ }); const mapDispatchToProps = (dispatch) => ({ setEmailOptions: (e) => dispatch(setEmailOptions(e)), + openChatByPhone: (phone) => dispatch(openChatByPhone(phone)), + setMessage: (text) => dispatch(setMessage(text)), }); export function JobsDetailHeaderCsi({ setEmailOptions, bodyshop, job, + openChatByPhone, + setMessage, ...props }) { const { t } = useTranslation(); @@ -36,52 +45,90 @@ export function JobsDetailHeaderCsi({ const handleCreateCsi = async (e) => { logImEXEvent("job_create_csi"); - const questionSetResult = await client.query({ - query: GET_CURRENT_QUESTIONSET_ID, - }); + //Is tehre already a CSI? + if (job.csi_invites.length === 0) { + const questionSetResult = await client.query({ + query: GET_CURRENT_QUESTIONSET_ID, + }); - if (questionSetResult.data.csiquestions.length > 0) { - const result = await insertCsi({ - variables: { - csiInput: { - jobid: job.id, - bodyshopid: bodyshop.id, - questionset: questionSetResult.data.csiquestions[0].id, - relateddata: { - job: { - id: job.id, - ownr_fn: job.ownr_fn, - ro_number: job.ro_number, - v_model_yr: job.v_model_yr, - v_make_desc: job.v_make_desc, - v_model_desc: job.v_model_desc, - }, - bodyshop: { - city: bodyshop.city, - email: bodyshop.email, - state: bodyshop.state, - country: bodyshop.country, - address1: bodyshop.address1, - address2: bodyshop.address2, - shopname: bodyshop.shopname, - zip_post: bodyshop.zip_post, - logo_img_path: bodyshop.logo_img_path, + if (questionSetResult.data.csiquestions.length > 0) { + const result = await insertCsi({ + variables: { + csiInput: { + jobid: job.id, + bodyshopid: bodyshop.id, + questionset: questionSetResult.data.csiquestions[0].id, + relateddata: { + job: { + id: job.id, + ownr_fn: job.ownr_fn, + ro_number: job.ro_number, + v_model_yr: job.v_model_yr, + v_make_desc: job.v_make_desc, + v_model_desc: job.v_model_desc, + }, + bodyshop: { + city: bodyshop.city, + email: bodyshop.email, + state: bodyshop.state, + country: bodyshop.country, + address1: bodyshop.address1, + address2: bodyshop.address2, + shopname: bodyshop.shopname, + zip_post: bodyshop.zip_post, + logo_img_path: bodyshop.logo_img_path, + }, }, }, }, - }, - }); + }); - if (!!!result.errors) { - notification["success"]({ message: t("csi.successes.created") }); + if (!!!result.errors) { + notification["success"]({ message: t("csi.successes.created") }); + } else { + notification["error"]({ + message: t("csi.errors.creating", { + message: JSON.stringify(result.errors), + }), + }); + return; + } + if (e.key === "email") + setEmailOptions({ + messageOptions: { + to: [job.ownr_ea], + replyTo: bodyshop.email, + }, + template: { + name: TemplateList("job").csi_invitation.key, + variables: { + id: result.data.insert_csi.returning[0].id, + }, + }, + }); + + if (e.key === "text") { + const p = parsePhoneNumber(job.ownr_ph1, "CA"); + if (p && p.isValid()) { + // openChatByPhone({ + // phone_num: p.formatInternational(), + // jobid: job.id, + // }); + setMessage( + `${window.location.protocol}//${window.location.host}/csi/${result.data.insert_csi.returning[0].id}` + ); + } else { + notification["error"]({ + message: t("messaging.error.invalidphone"), + }); + } + } } else { notification["error"]({ - message: t("csi.errors.creating", { - message: JSON.stringify(result.errors), - }), + message: t("csi.errors.notconfigured"), }); - return; } + } else { if (e.key === "email") setEmailOptions({ messageOptions: { @@ -91,17 +138,27 @@ export function JobsDetailHeaderCsi({ template: { name: TemplateList("job").csi_invitation.key, variables: { - id: result.data.insert_csi.returning[0].id, + id: job.csi_invites[0].id, }, }, }); if (e.key === "text") { + const p = parsePhoneNumber(job.ownr_ph1, "CA"); + if (p && p.isValid()) { + // openChatByPhone({ + // phone_num: p.formatInternational(), + // jobid: job.id, + // }); + setMessage( + `${window.location.protocol}//${window.location.host}/csi/${job.csi_invites[0].id}` + ); + } else { + notification["error"]({ + message: t("messaging.error.invalidphone"), + }); + } } - } else { - notification["error"]({ - message: t("csi.errors.notconfigured"), - }); } }; @@ -122,13 +179,26 @@ export function JobsDetailHeaderCsi({ {t("general.labels.text")} - {job.csiinvites.map((item, idx) => ( - - - {item.completedon} - - - ))} + {job.csiinvites.map((item, idx) => { + return item.completedon ? ( + + + {item.completedon} + + + ) : ( + { + navigator.clipboard.writeText( + `${window.location.protocol}//${window.location.host}/csi/${item.id}` + ); + }} + > + {t("general.actions.copylink")} + + ); + })} ); }