pager-widget 0.0.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/App.jsx CHANGED
@@ -12,6 +12,9 @@ import { convertToMDText } from "./md/md_format_converter";
12
12
  import { formatTimeAMPM } from "./lib";
13
13
  import styled from "styled-components";
14
14
  import useWidgetService from "./useWidgetService";
15
+ import InputConverter from "./InputConverter";
16
+ import ConsentPopup from "./ConsentPopup";
17
+ import axios from "axios";
15
18
 
16
19
  const switchSize = (size, small, medium, large) => {
17
20
  // ;
@@ -2374,6 +2377,7 @@ const IconSpan = styled.span`
2374
2377
  transition: 0.3s;
2375
2378
  }
2376
2379
  `;
2380
+
2377
2381
  const ButtonWrapper = styled.div`
2378
2382
  width: 100%;
2379
2383
  float: left;
@@ -2462,6 +2466,13 @@ const App = ({
2462
2466
  style,
2463
2467
  }) => {
2464
2468
  // ;
2469
+
2470
+ const [sourceState, setSourceState] = useState(widget?.source_id);
2471
+
2472
+ const [enableConsent, setEnableConsent] = useState(false);
2473
+ const [workspace, setWorkspace] = useState(widget?.workspace);
2474
+
2475
+ const [fields, setFields] = useState();
2465
2476
  const {
2466
2477
  connected,
2467
2478
  messages,
@@ -2478,10 +2489,44 @@ const App = ({
2478
2489
  authToken,
2479
2490
  newToken,
2480
2491
  userDetails,
2492
+ sourceState,
2493
+
2494
+ // enableConsent,
2481
2495
  });
2496
+
2497
+ const conversationId = messages[0]?.message?.system?.conversationId;
2498
+
2482
2499
  const [open, setOpen] = useState(openByDefault);
2500
+ const [isVisible, setIsVisible] = useState(false);
2501
+ const [formSubmit, setFormSubmit] = useState(false);
2483
2502
 
2484
2503
  const containerEl = useRef(null);
2504
+
2505
+ useEffect(() => {
2506
+ if (widget?.source_id !== undefined) {
2507
+ setSourceState(widget?.source_id);
2508
+ }
2509
+ }, [widget]);
2510
+
2511
+ useEffect(() => {
2512
+ if (!widget?.id) return; // Prevent API call if widget ID is undefined
2513
+
2514
+
2515
+
2516
+ axios
2517
+ .get(
2518
+ `http://dev.pagergpt.ai/wgt-hook/public/${widget?.workspace}/${widget.id}/form`
2519
+ )
2520
+ .then((res) => {
2521
+
2522
+ setFields(res.data);
2523
+
2524
+ })
2525
+ .catch((err) => {
2526
+ console.error("Error fetching form:", err);
2527
+ });
2528
+ }, [widget?.id, formSubmit]);
2529
+
2485
2530
  useEffect(() => {
2486
2531
  if (containerEl.current) {
2487
2532
  containerEl.current.scroll(0, containerEl.current.scrollHeight);
@@ -2507,16 +2552,43 @@ const App = ({
2507
2552
  />
2508
2553
  <ChatContentWrapper
2509
2554
  size={widget_size}
2510
- className="widget_chat_content"
2555
+ clsassName="widget_chat_content"
2511
2556
  ref={containerEl}
2512
2557
  >
2513
2558
  <BodyWrapper
2559
+ sourceState={sourceState}
2514
2560
  {...{ messages }}
2515
2561
  sendMessage={sendMessage}
2516
2562
  sendFeedback={sendFeedback}
2517
2563
  widget={widget}
2518
2564
  style={style}
2519
2565
  />
2566
+ {sourceState === "new_user" && widget.user_consent == true && (
2567
+ <ConsentPopup
2568
+ formData={fields}
2569
+ conversationId={conversationId}
2570
+ widget={widget}
2571
+ enableConsent={enableConsent}
2572
+ setEnableConsent={setEnableConsent}
2573
+ isVisible={isVisible}
2574
+ setIsVisible={setIsVisible}
2575
+ setSourceState={setSourceState}
2576
+ />
2577
+ )}
2578
+
2579
+ {((sourceState &&
2580
+ typeof sourceState === "string" &&
2581
+ sourceState.includes("browser") &&
2582
+ fields && widget?.lead_form_id !== null) || widget.user_consent == false && sourceState == "new_user" && widget?.lead_form_id !== null && fields)? (
2583
+ <InputConverter
2584
+ formData={fields}
2585
+ conversationId={conversationId}
2586
+ widgetId={widget_id}
2587
+ workspace={widget?.workspace}
2588
+ setFormSubmit={setFormSubmit}
2589
+ setSourceState={setSourceState}
2590
+ />
2591
+ ) : null}
2520
2592
  </ChatContentWrapper>
2521
2593
  <Footer
2522
2594
  {...{ sendMessage, onFileUpload }}
@@ -2591,6 +2663,7 @@ const Footer = ({
2591
2663
  }
2592
2664
  }, [textareaRef.current]);
2593
2665
  const scale = useScaleContext();
2666
+
2594
2667
  return (
2595
2668
  <>
2596
2669
  {typing && (
@@ -2678,43 +2751,34 @@ const Footer = ({
2678
2751
  };
2679
2752
 
2680
2753
  const BodyWrapper = ({
2754
+ sourceState,
2681
2755
  messages,
2682
2756
  sendMessage,
2683
2757
  sendFeedback,
2684
2758
  widget,
2685
2759
  style,
2686
2760
  }) => {
2687
- // const isSmall = useMedia({ maxWidth: "520px" });
2688
2761
 
2689
- // const containerEl = useRef(null);
2690
- // useEffect(() => {
2691
- // if (containerEl.current) {
2692
- // containerEl.current.scroll(0, containerEl.current.scrollHeight);
2693
- // }
2694
- // });
2762
+
2695
2763
  return (
2696
2764
  <>
2697
- {/* <div
2698
- className={`overflow-y-scroll p-3 ${
2699
- isSmall ? "body_container_mobile" : "body_container"
2700
- }`}
2701
- style={
2702
- isSmall ? { height: "calc(100vh - 132px)" } : { height: "395px" }
2703
- }
2704
- ref={containerEl}
2705
- > */}
2706
2765
  {messages.map((message, key) => (
2707
- <Message
2708
- {...{ key, ...message }}
2709
- sendMessage={sendMessage}
2710
- sendFeedback={sendFeedback}
2711
- widget={widget}
2712
- timestamp={message.time}
2713
- style={style}
2714
- key={key}
2715
- />
2766
+ <>
2767
+ {((sourceState == "new_user" && widget.user_consent == false) ||
2768
+ sourceState !== "new_user") && (
2769
+ <Message
2770
+ {...{ key, ...message }}
2771
+ sendMessage={sendMessage}
2772
+ sendFeedback={sendFeedback}
2773
+ widget={widget}
2774
+ timestamp={message.time}
2775
+ style={style}
2776
+ key={key}
2777
+ />
2778
+ )}
2779
+ </>
2716
2780
  ))}
2717
- {/* </div> */}
2781
+
2718
2782
  </>
2719
2783
  );
2720
2784
  };
@@ -0,0 +1,175 @@
1
+ import React from "react";
2
+ import {
3
+ browserName,
4
+ osName,
5
+ deviceType,
6
+ getUA,
7
+ windowWidth,
8
+ windowHeight,
9
+ } from "react-device-detect";
10
+ import axios from "axios";
11
+
12
+ import { useState, useEffect } from "react";
13
+ const ConsentPopup = ({
14
+ formData,
15
+ conversationId,
16
+ widget,
17
+ enableConsent,
18
+ setEnableConsent,
19
+ isVisible,
20
+ setIsVisible,
21
+ setSourceState,
22
+ }) => {
23
+ console.log("consent form data", formData);
24
+ const [deviceLocation, setDeviceLocation] = useState({
25
+ lat: "",
26
+ long: "",
27
+ });
28
+
29
+
30
+ const handleCheckboxChange = (event) => {
31
+ setIsVisible(event.target.checked);
32
+ };
33
+ useEffect(() => {
34
+ if (navigator.geolocation) {
35
+ navigator.geolocation.getCurrentPosition(
36
+ (position) => {
37
+ setDeviceLocation({
38
+ lat: position.coords.latitude,
39
+ long: position.coords.longitude,
40
+ });
41
+ },
42
+ (error) => {
43
+ console.error("Error getting location:", error.message);
44
+ }
45
+ );
46
+ } else {
47
+ console.error("Geolocation is not supported by this browser.");
48
+ }
49
+ }, []);
50
+
51
+ const handleSubmit = () => {
52
+
53
+
54
+ const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
55
+ const language = navigator.language;
56
+
57
+ const systemDetails = [
58
+ { field_id: "SYS_browser", value: browserName },
59
+ { field_id: "SYS_browser_os", value: osName },
60
+ { field_id: "SYS_browser_language", value: language },
61
+ {
62
+ field_id: "SYS_screen_resolution",
63
+ value: `${window.screen.width} x ${window.screen.height}`,
64
+ },
65
+ { field_id: "SYS_time_zone", value: timeZone },
66
+ { field_id: "SYS_device_type", value: deviceType },
67
+
68
+ {
69
+ field_id: "SYS_location",
70
+ value: `Lat: ${deviceLocation.lat}, Long: ${deviceLocation.long}`,
71
+ }, // Correctly updated location
72
+ ];
73
+
74
+ const submissionPayload = {
75
+ form_id:
76
+ formData == "No forms attached to this widget"
77
+ ? ""
78
+ : formData.form_data.form_id,
79
+ conversation_id: conversationId,
80
+ lead_data: {
81
+ name:
82
+ formData == "No forms attached to this widget"
83
+ ? ""
84
+ : formData.form_data.name,
85
+ fields: systemDetails,
86
+ ws:
87
+ formData == "No forms attached to this widget"
88
+ ? widget.workspace
89
+ : formData.form_data.ws,
90
+ user_consent: widget.user_consent,
91
+ },
92
+ type: "browser",
93
+ };
94
+
95
+ axios
96
+ .post(
97
+ `https://dev.pagergpt.ai/wgt-hook/${widget.workspace}/${widget.id}/lead`,
98
+ submissionPayload,
99
+ { withCredentials: true }
100
+ )
101
+ .then((res) => {
102
+ setSourceState(res.data.source_id);
103
+ setEnableConsent(true);
104
+ });
105
+ };
106
+
107
+ return (
108
+ <>
109
+ <input
110
+ type="checkbox"
111
+ id="consentCheckbox"
112
+ checked={isVisible}
113
+ onChange={handleCheckboxChange}
114
+ />
115
+ <label htmlFor="consentCheckbox" style={{ marginLeft: "8px" }}>
116
+ I am part of the AI services and I am still learning my way around.
117
+ Clicking "Submit" will confirm your understanding and agreement that use
118
+ of this feature is subject to our Privacy Policy
119
+ </label>
120
+ <div>
121
+ <button
122
+ disabled={isVisible == false ? true : false}
123
+ style={popupStyles.button}
124
+ onClick={handleSubmit}
125
+ >
126
+ Submit
127
+ </button>
128
+ </div>
129
+ </>
130
+ );
131
+
132
+ };
133
+
134
+ const popupStyles = {
135
+ overlay: {
136
+ position: "fixed",
137
+ top: 0,
138
+ left: 0,
139
+ width: "100%",
140
+ height: "100%",
141
+ backgroundColor: "rgba(0, 0, 0, 0.5)",
142
+ display: "flex",
143
+ justifyContent: "center",
144
+ alignItems: "center",
145
+ zIndex: 1000,
146
+ },
147
+ popup: {
148
+ background: "white",
149
+ padding: "20px",
150
+ borderRadius: "8px",
151
+ textAlign: "center",
152
+ boxShadow: "0 4px 8px rgba(0, 0, 0, 0.2)",
153
+ width: "300px",
154
+ },
155
+ title: {
156
+ marginBottom: "20px",
157
+ fontSize: "18px",
158
+ color: "#333",
159
+ },
160
+ buttonContainer: {
161
+ display: "flex",
162
+ justifyContent: "space-around",
163
+ },
164
+ button: {
165
+ padding: "10px 20px",
166
+ fontSize: "16px",
167
+ border: "none",
168
+ borderRadius: "4px",
169
+ cursor: "pointer",
170
+ backgroundColor: "#007bff",
171
+ color: "white",
172
+ },
173
+ };
174
+
175
+ export default ConsentPopup;