bheng8200

WebChat

Mar 17th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useState } from "react";
  2. import { SketchPicker } from "react-color";
  3. import { Col, Row, Container } from "../../components/Grid";
  4. import Card from "react-bootstrap/Card";
  5. import Button from "react-bootstrap/Button";
  6. import axios from "axios";
  7. import "../../css/App.css";
  8. import { LoadingModal } from "../../components/Modal";
  9. import { AppsModal } from "../../components/Modal";
  10. import { useHistory } from "react-router-dom";
  11.  
  12. export function WebChat() {
  13.  
  14.   const [install, setInstall] = useState(true);
  15.   const [uninstall, setUninstall] = useState(false);
  16.   const [textColor, setTextColor] = useState();
  17.   const [buttonColor, setButtonColor] = useState();
  18.   const [color, setColor] = useState();
  19.   const [displayColorPicker, setDisplayColorPicker] = useState(false);
  20.   const [displayColorPicker1, setDisplayColorPicker1] = useState(false);
  21.   const [displayColorPicker2, setDisplayColorPicker2] = useState(false);
  22.   const [loader, setLoader] = useState(false);
  23.   const [modalShow, setModalShow] = useState(false);
  24.   const [appAction, setAppAction] = useState("");
  25.  
  26.   const history = useHistory();
  27.  
  28.   useEffect(() => {
  29.     const thryv_id = localStorage.getItem("business_id");
  30.     axios
  31.       .get(
  32.         `${process.env.REACT_APP_BACKEND_URL}/api/webmessenger/thryvUser/getusertableinfo`,
  33.         {
  34.           params: {
  35.             id: thryv_id,
  36.           },
  37.         }
  38.       )
  39.       .then((response) => {
  40.         if (response?.data?.length > 0) {
  41.           setUninstall(true);
  42.           setInstall(false);
  43.           setColor(response.data[0].brand_color); // ==>brand_color from db
  44.           setTextColor(response.data[0].conversation_color); // ==> conversation_color from db
  45.           setButtonColor(response.data[0].action_color); //==> action_color from db
  46.           localStorage.setItem("color", response.data[0].brand_color);
  47.           localStorage.setItem(
  48.             "textColor",
  49.             response.data[0].conversation_color
  50.           );
  51.           localStorage.setItem("buttonColor", response.data[0].action_color);
  52.         }
  53.       })
  54.       .catch((error) => {
  55.         console.log(error);
  56.       });
  57.   },[]);
  58.  
  59.   useEffect(() => {
  60.     const access_token = localStorage.getItem("access_token");
  61.     const thryv_business_id = localStorage.getItem("business_id");
  62.  
  63.     const authHeader = {
  64.       headers: {
  65.         Authorization: `Bearer ${access_token}`,
  66.       },
  67.     };
  68.  
  69.     axios
  70.       .get(
  71.         `https://api.thryv.com/platform/v1/businesses/${thryv_business_id}`,
  72.         authHeader
  73.       )
  74.       .then((response) => {
  75.         if (response?.status === 200) {
  76.           setColor(response?.data?.data?.business?.meta?.branding?.color);
  77.         }
  78.       })
  79.       .catch((error) => {
  80.         console.log(error);
  81.       });
  82.   },[]);
  83.  
  84.   const pickHeaderColor = () => {
  85.     setDisplayColorPicker(!displayColorPicker);
  86.   };
  87.  
  88.   const handleCloseHeader = () => {
  89.     setDisplayColorPicker(false);
  90.   };
  91.  
  92.   const handleHeaderColorChange = (color) => {
  93.     setColor(color.hex);
  94.   };
  95.  
  96.   const pickTextColor = () => {
  97.     setDisplayColorPicker1(!displayColorPicker1);
  98.   };
  99.  
  100.   const handleCloseText = () => {
  101.     setDisplayColorPicker1(false);
  102.   };
  103.  
  104.   const handleTextColorChange = (color) => {
  105.     setTextColor(color.hex);
  106.   };
  107.  
  108.   const pickButtonColor = () => {
  109.     setDisplayColorPicker2(!displayColorPicker2);
  110.   };
  111.  
  112.   const handleCloseButton = () => {
  113.     setDisplayColorPicker2(false);
  114.   };
  115.  
  116.   const handleButtonColorChange = (color) => {
  117.     setButtonColor(color.hex);
  118.   };
  119.  
  120.   const installColorSetting = () => {
  121.     setLoader(true);
  122.  
  123.     const colorPayload = {
  124.       thryv_id: localStorage.getItem("business_id"),
  125.       method: "install",
  126.       type: "web",
  127.       thryv_access_token: localStorage.getItem("access_token"),
  128.       brand_color: color,
  129.       conversation_color: textColor,
  130.       action_color: buttonColor,
  131.     };
  132.  
  133.     axios
  134.       .post(
  135.         `${process.env.REACT_APP_BACKEND_URL}/api/webmessenger/thryvUser/installColorSetting`,
  136.         {
  137.           payload: colorPayload,
  138.         }
  139.       )
  140.       .then((response) => {
  141.         localStorage.setItem("Script", response?.data?.payload);
  142.         if (response?.status === 200) {
  143.           setLoader(false);
  144.           history.push("/webMessengerNext");
  145.         }
  146.       })
  147.       .catch((error) => {
  148.         console.log(error);
  149.       });
  150.   };
  151.  
  152.   const updateColorSetting = () => {
  153.     setLoader(true);
  154.  
  155.     const colorPayload = {
  156.       thryv_id: localStorage.getItem("business_id"),
  157.       method: "update",
  158.       type: "web",
  159.       thryv_access_token: localStorage.getItem("access_token"),
  160.       brand_color: color,
  161.       conversation_color: textColor,
  162.       action_color: buttonColor,
  163.     };
  164.  
  165.     axios
  166.       .post(
  167.         `${process.env.REACT_APP_BACKEND_URL}/api/webmessenger/thryvUser/installColorSetting`,
  168.         {
  169.           payload: colorPayload,
  170.         }
  171.       )
  172.       .then((response) => {
  173.         localStorage.setItem("Script", response?.data?.payload);
  174.         if (response?.status === 200) {
  175.           setAppAction("Updated");
  176.           setModalShow(true);
  177.           setLoader(false);
  178.         }
  179.       })
  180.       .catch((error) => {
  181.         console.log(error);
  182.       });
  183.   };
  184.  
  185.   const uninstallSetting = () => {
  186.     setLoader(true);
  187.     setModalShow(true);
  188.  
  189.     const colorPayload = {
  190.       thryv_id: localStorage.getItem("business_id"),
  191.       method: "uninstall",
  192.       type: "web",
  193.     };
  194.  
  195.     axios
  196.       .post(
  197.         `${process.env.REACT_APP_BACKEND_URL}/api/webmessenger/thryvUser/installColorSetting`,
  198.         {
  199.           payload: colorPayload,
  200.         }
  201.       )
  202.       .then((response) => {
  203.         localStorage.setItem("Script", response?.data?.payload);
  204.         if (response?.status === 200) {
  205.           setAppAction("Uninstalled");
  206.           setModalShow(true);
  207.           setLoader(false);
  208.         }
  209.       })
  210.       .catch((error) => {
  211.         console.log(error);
  212.       });
  213.   };
  214.  
  215.   const dontUninstall = () => {
  216.     window.location.href = "https://go.thryv.com/app/app-market";
  217.   };
  218.  
  219.   const popover = {
  220.     position: "absolute",
  221.     zIndex: "2",
  222.   };
  223.   const cover = {
  224.     position: "fixed",
  225.     top: "0px",
  226.     right: "0px",
  227.     bottom: "0px",
  228.     left: "0px",
  229.   };
  230.  
  231.   return (
  232.     <Container fluid>
  233.       {loader && <LoadingModal show={loader} />}
  234.       {modalShow && (
  235.         <AppsModal
  236.           show={modalShow}
  237.           appName="WebMessenger"
  238.           appAction={appAction}
  239.           onHide={() => {
  240.             setModalShow(false);
  241.             window.location.href = "https://go.thryv.com/app";
  242.           }}
  243.         />
  244.       )}
  245.       <div class="container-Fluid" style={{ paddingBottom: "50px" }}>
  246.         <div
  247.           style={{
  248.             height: "301px",
  249.             clear: "both",
  250.             paddingTop: "120px",
  251.             textAlign: "center",
  252.             backgroundColor: "rgb(236, 238, 239)",
  253.             marginBottom: "55px",
  254.           }}
  255.         >
  256.           <p class="lead mt-2 step1" style={{ fontSize: "x-large" }}>
  257.             You can choose the color combination below which suits to your
  258.             website.
  259.           </p>
  260.           <p>
  261.             Please customize the below colors to best suite your website. Don’t
  262.             worry, if you don’t like your initial selection this can always be
  263.             updated!
  264.           </p>
  265.         </div>
  266.       </div>
  267.       <Row>
  268.         <Col size="md-6">
  269.           <div>
  270.             <div
  271.               class="container d-flex justify-content-center"
  272.               style={{ width: "400px" }}
  273.             >
  274.               <div class="card" style={{ border: "none" }}>
  275.                 {install && (
  276.                   <div
  277.                     class="d-flex flex-row justify-content-between p-3 adiv text-white"
  278.                     style={{ backgroundColor: color }}
  279.                   >
  280.                     {" "}
  281.                     <i className="fa fa-arrow-left"></i>{" "}
  282.                     <span class="pb-3">Thryv chat</span>{" "}
  283.                     <i class="fa fa-times"></i>{" "}
  284.                   </div>
  285.                 )}
  286.                 {uninstall && (
  287.                   <div
  288.                     class="d-flex flex-row justify-content-between p-3 adiv text-white"
  289.                     style={{ backgroundColor: color }}
  290.                   >
  291.                     {" "}
  292.                     <i className="fa fa-arrow-left"></i>{" "}
  293.                     <span class="pb-3">Thryv chat</span>{" "}
  294.                     <i class="fa fa-times"></i>{" "}
  295.                   </div>
  296.                 )}
  297.                 <div class="d-flex flex-row p-3">
  298.                   {install && (
  299.                     <div
  300.                       class="chat mr-2 p-3"
  301.                       style={{ backgroundColor: textColor }}
  302.                     >
  303.                       Hello and thank you for using the Thryv.{" "}
  304.                     </div>
  305.                   )}
  306.                   {uninstall && (
  307.                     <div
  308.                       class="chat mr-2 p-3"
  309.                       style={{ backgroundColor: textColor }}
  310.                     >
  311.                       Hello and thank you for using the Thryv.{" "}
  312.                     </div>
  313.                   )}
  314.                   <img
  315.                     src="https://img.icons8.com/color/48/000000/circled-user-male-skin-type-7.png"
  316.                     alt="male-user"
  317.                     width="30"
  318.                     height="30"
  319.                   ></img>
  320.                 </div>
  321.                 <div class="d-flex flex-row p-3">
  322.                   <img
  323.                     src="https://img.icons8.com/color/48/000000/circled-user-female-skin-type-7.png"
  324.                     alt="female-user"
  325.                     width="30"
  326.                     height="30"
  327.                   ></img>
  328.                   <div
  329.                     class="chat ml-2 p-3"
  330.                     style={{ backgroundColor: "rgba(0, 0, 0, 0.03)" }}
  331.                   >
  332.                     I would like to have a introductory demo call if possible.
  333.                   </div>
  334.                 </div>
  335.                 <div class="d-flex flex-row p-3">
  336.                   {install && (
  337.                     <div
  338.                       class="chat mr-2 p-3"
  339.                       style={{ backgroundColor: textColor }}
  340.                     >
  341.                       Absolutely, please use below book button to do so.{" "}
  342.                     </div>
  343.                   )}
  344.                   {uninstall && (
  345.                     <div
  346.                       class="chat mr-2 p-3"
  347.                       style={{ backgroundColor: textColor }}
  348.                     >
  349.                       Absolutely, please use below book button to do so.{" "}
  350.                     </div>
  351.                   )}
  352.                   <img
  353.                     src="https://img.icons8.com/color/48/000000/circled-user-male-skin-type-7.png"
  354.                     alt="female-user"
  355.                     width="30"
  356.                     height="30"
  357.                   ></img>
  358.                 </div>
  359.                 <div class="d-flex flex-row p-3">
  360.                   <div
  361.                     class="chat ml-5 p-3"
  362.                     style={{ backgroundColor: textColor }}
  363.                   >
  364.                     <p> Introductory Phone </p>
  365.                     <center>
  366.                       {install && (
  367.                         <Button
  368.                           style={{
  369.                             backgroundColor: buttonColor,
  370.                             border: "none",
  371.                             color: "white",
  372.                             borderRadius: "50px",
  373.                           }}
  374.                         >
  375.                           Book
  376.                         </Button>
  377.                       )}
  378.                       {uninstall && (
  379.                         <Button
  380.                           style={{
  381.                             backgroundColor: buttonColor,
  382.                             border: "none",
  383.                             color: "white",
  384.                             borderRadius: "50px",
  385.                           }}
  386.                         >
  387.                           Book
  388.                         </Button>
  389.                       )}
  390.                     </center>
  391.                   </div>
  392.                 </div>
  393.                 <div class="d-flex flex-row p-3">
  394.                   <img
  395.                     src="https://img.icons8.com/color/48/000000/circled-user-female-skin-type-7.png"
  396.                     width="30"
  397.                     height="30"
  398.                     alt="female-user"
  399.                   ></img>
  400.                   <div
  401.                     class="chat ml-2 p-3"
  402.                     style={{ backgroundColor: "rgba(0, 0, 0, 0.03)" }}
  403.                   >
  404.                     Thank you!
  405.                   </div>
  406.                 </div>
  407.                 <div class="form-group px-3">
  408.                   {" "}
  409.                   <textarea
  410.                     class="form-control"
  411.                     style={{ borderRadius: "100px", height: "40px" }}
  412.                     rows="5"
  413.                     placeholder="Type your message"
  414.                   ></textarea>{" "}
  415.                 </div>
  416.               </div>
  417.             </div>
  418.           </div>
  419.         </Col>
  420.         <Col size="md-2">
  421.           <div>
  422.             <Card border="light" style={{ width: "40rem" }}>
  423.               <Card.Header style={{ textAlign: "center" }}>
  424.                 Color Setting
  425.               </Card.Header>
  426.               <Card.Body>
  427.                 <Card.Title>Header Color</Card.Title>
  428.                 <Card.Text>
  429.                   Pick a messenger header color from color pallete which suits
  430.                   to your website.
  431.                   <br></br> <br></br>
  432.                   <p style={{ fontSize: "smaller" }}>
  433.                     - We’ve defaulted this to the brand color you specified on
  434.                     your Thryv profile! However, if this color doesn’t suit your
  435.                     website feel free to change it!
  436.                   </p>
  437.                   <br></br>
  438.                   <center>
  439.                     <Button
  440.                       variant="primary"
  441.                       style={{
  442.                         backgroundColor: "rgba(0,0,0,.03)",
  443.                         border: "none",
  444.                         color: "black",
  445.                       }}
  446.                       onClick={pickHeaderColor}
  447.                     >
  448.                       Pick Header Color
  449.                     </Button>
  450.                   </center>
  451.                   <div className="siderbar">
  452.                     {displayColorPicker ? (
  453.                       <div style={popover}>
  454.                         <div style={cover} onClick={handleCloseHeader} />
  455.                         <SketchPicker
  456.                           color={color}
  457.                           onChange={handleHeaderColorChange}
  458.                           onSwatchHover={(color, e) => {
  459.                             console.log("color", color);
  460.                           }}
  461.                         />
  462.                       </div>
  463.                     ) : null}
  464.                   </div>
  465.                   <br></br>
  466.                   <Card.Title>Text Color</Card.Title>
  467.                   Pick a messenger text color from color pallete which suits to
  468.                   your website.
  469.                   <br></br> <br></br>
  470.                   <p style={{ fontSize: "smaller" }}>
  471.                     - Pick a color that best suits your website!
  472.                   </p>
  473.                   <br></br>
  474.                   <center>
  475.                     <Button
  476.                       variant="primary"
  477.                       style={{
  478.                         backgroundColor: "rgba(0,0,0,.03)",
  479.                         border: "none",
  480.                         color: "black",
  481.                       }}
  482.                       onClick={pickTextColor}
  483.                     >
  484.                       Pick Text Color
  485.                     </Button>
  486.                   </center>
  487.                   <div className="siderbar">
  488.                     {displayColorPicker1 ? (
  489.                       <div style={popover}>
  490.                         <div style={cover} onClick={handleCloseText} />
  491.                         <SketchPicker
  492.                           color={textColor}
  493.                           onChange={handleTextColorChange}
  494.                           onSwatchHover={(color, e) => {
  495.                             console.log("color", color);
  496.                           }}
  497.                         />
  498.                       </div>
  499.                     ) : null}
  500.                   </div>
  501.                   <br></br>
  502.                   <Card.Title>Button Color</Card.Title>
  503.                   Pick a messenger text color from color pallete which suits to
  504.                   your website.
  505.                   <br></br> <br></br>
  506.                   <p style={{ fontSize: "smaller" }}>
  507.                     - Pick a color that best suits your website!
  508.                   </p>
  509.                   <br></br>
  510.                   <center>
  511.                     <Button
  512.                       variant="primary"
  513.                       style={{
  514.                         backgroundColor: "rgba(0,0,0,.03)",
  515.                         border: "none",
  516.                         color: "black",
  517.                       }}
  518.                       onClick={pickButtonColor}
  519.                     >
  520.                       Pick Button Color
  521.                     </Button>
  522.                   </center>
  523.                   <div className="siderbar">
  524.                     {displayColorPicker2 ? (
  525.                       <div style={popover}>
  526.                         <div style={cover} onClick={handleCloseButton} />
  527.                         <SketchPicker
  528.                           color={buttonColor}
  529.                           onChange={handleButtonColorChange}
  530.                           onSwatchHover={(color, e) => {
  531.                             console.log("color", color);
  532.                           }}
  533.                         />
  534.                       </div>
  535.                     ) : null}
  536.                   </div>{" "}
  537.                   <br></br> <br></br>
  538.                 </Card.Text>
  539.               </Card.Body>
  540.             </Card>
  541.             <br />{" "}
  542.           </div>
  543.         </Col>
  544.       </Row>
  545.       <Row>
  546.         <Col size="12">
  547.           <center>
  548.             {install && (
  549.               <Button
  550.                 variant="primary"
  551.                 style={{
  552.                   backgroundColor: "rgb(255, 80, 0)",
  553.                   width: "150px",
  554.                   fontSize: "18px",
  555.                   border: "none",
  556.                   height: "50px",
  557.                   color: "white",
  558.                 }}
  559.                 onClick={installColorSetting}
  560.               >
  561.                 Install
  562.               </Button>
  563.             )}
  564.             {uninstall && (
  565.               <Button
  566.                 variant="primary"
  567.                 style={{
  568.                   backgroundColor: "rgb(255, 80, 0)",
  569.                   width: "150px",
  570.                   fontSize: "18px",
  571.                   border: "none",
  572.                   height: "50px",
  573.                   color: "white",
  574.                   marginRight: "15px",
  575.                 }}
  576.                 onClick={updateColorSetting}
  577.               >
  578.                 Update
  579.               </Button>
  580.             )}
  581.             {uninstall && (
  582.               <Button
  583.                 variant="primary"
  584.                 style={{
  585.                   backgroundColor: "rgb(255, 80, 0)",
  586.                   width: "150px",
  587.                   fontSize: "18px",
  588.                   border: "none",
  589.                   height: "50px",
  590.                   color: "white",
  591.                   marginRight: "15px",
  592.                 }}
  593.                 onClick={uninstallSetting}
  594.               >
  595.                 Uninstall
  596.               </Button>
  597.             )}
  598.             {uninstall && (
  599.               <Button
  600.                 variant="primary"
  601.                 style={{
  602.                   backgroundColor: "rgb(255, 80, 0)",
  603.                   width: "150px",
  604.                   fontSize: "18px",
  605.                   border: "none",
  606.                   height: "50px",
  607.                   color: "white",
  608.                   marginRight: "15px",
  609.                 }}
  610.                 onClick={dontUninstall}
  611.               >
  612.                 Go Back to thryv
  613.               </Button>
  614.             )}
  615.           </center>
  616.         </Col>
  617.       </Row>
  618.     </Container>
  619.   );
  620. }
Add Comment
Please, Sign In to add comment