Skip to content

Commit 0689e81

Browse files
refactor: rewrite/restructure chatbot modal
1 parent 9e54ffc commit 0689e81

File tree

7 files changed

+94
-28
lines changed

7 files changed

+94
-28
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@testing-library/user-event": "^13.5.0",
1313
"@types/jest": "^27.5.2",
1414
"@types/node": "^16.18.34",
15-
"@types/react": "^18.2.8",
15+
"@types/react": "^18.2.52",
1616
"@types/react-dom": "^18.2.4",
1717
"lodash": "^4.17.21",
1818
"react": "^18.2.0",

src/Component/ChatFlow/TextMessageNode.tsx

+22-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,26 @@ const TextMessageNode = ({ data, isConnectable, id }: NodeProps) => {
1010
const dataLabel = data?.label
1111
.split("\n")
1212
.map((paragraph: any, index: number) => <p key={index}>{paragraph}</p>);
13+
14+
const color =
15+
data?.type === "Start"
16+
? "bg-sky-500/80"
17+
: data?.type === "Keyword"
18+
? "bg-orange-400/80"
19+
: "bg-lime-600/80";
20+
21+
const footerColor =
22+
data?.type === "Start"
23+
? "bg-sky-500/30"
24+
: data?.type === "Keyword"
25+
? "bg-orange-400/30"
26+
: "bg-lime-600/30";
27+
1328
return (
14-
<div className="w-[200px]">
15-
<div className="py-2 px-3 flex justify-start text-white bg-lime-600/80 text-[10px] items-center gap-1 font-bold rounded-t-md">
29+
<div className="w-[200px] bg-">
30+
<div
31+
className={`py-2 px-3 flex justify-start text-white text-[10px] items-center gap-1 font-bold rounded-t-md ${color}`}
32+
>
1633
<WhatsAppIcon className="w-4 h-4" />
1734
<div>{data?.type}</div>
1835
</div>
@@ -33,7 +50,9 @@ const TextMessageNode = ({ data, isConnectable, id }: NodeProps) => {
3350
</div>
3451
<Handle type="source" position={Position.Bottom} />
3552
</div>
36-
<div className="text-left py-2 px-3 flex justify-between text-white bg-blue-100 text-[10px] items-center gap-1 font-bold rounded-b-md">
53+
<div
54+
className={`text-left py-2 px-3 flex justify-between text-white text-[10px] items-center gap-1 font-bold rounded-b-md ${footerColor}`}
55+
>
3756
<div id="messageID" className="italic line-clamp-1 text-blue-600">
3857
{id}
3958
</div>

src/Component/ChatFlow/index.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import Navbar from "Component/Common/Navbar";
55
import NodePanel from "../ChatFlow/NodePanel";
66
import NodeSetting from "Component/ChatFlow/NodeSetting";
77
import { nodeTypes } from "Component/ChatFlow/constant";
8+
import { useCallback, useEffect, useState } from "react";
9+
// import { useEffect, useState } from "react";
810

911
const BasicFlow = () => {
12+
// const [datas, setDatas] = useState([]);
13+
1014
const {
1115
reactFlowWrapper,
1216
setReactFlowInstance,
@@ -24,9 +28,22 @@ const BasicFlow = () => {
2428
handleEdgeValidation,
2529
saveFlow,
2630
} = useChatFlow();
31+
32+
// useEffect(() => {
33+
// const dataFromLocalStorage = localStorage.getItem("example-flow");
34+
// const response = dataFromLocalStorage
35+
// ? JSON.parse(dataFromLocalStorage)
36+
// : null;
37+
// const nodes = response?.nodes;
38+
// }, []);
39+
40+
// console.log(edges, "On Connect");
41+
2742
return (
2843
<div className="h-full">
2944
<Navbar
45+
nodes={nodes}
46+
edges={edges}
3047
onSave={() => {
3148
saveFlow();
3249
}}

src/Component/Common/Navbar.tsx

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
import { FunctionComponent } from "react";
1+
import { FunctionComponent, useEffect, useState } from "react";
22
import { Save } from "@mui/icons-material";
33
import WhatsAppIcon from "@mui/icons-material/WhatsApp";
44
import Modal from "../Modal";
5+
import { Node, Edge } from "reactflow";
6+
import useChatFlow from "Component/ChatFlow/hook/useChatFlow";
57

68
interface Props {
79
onSave: () => void;
10+
nodes?: Node<any, string | undefined>[] | undefined;
11+
edges?: Edge<any>[] | undefined;
812
}
913

10-
const Navbar: FunctionComponent<Props> = ({ onSave }) => {
14+
const Navbar: FunctionComponent<Props> = ({ onSave, nodes, edges }) => {
15+
// const { edges, nodes } = useChatFlow();
16+
// const [datas, setDatas] = useState([]);
17+
18+
useEffect(() => {
19+
// const dataFromLocalStorage = localStorage.getItem("example-flow");
20+
// const response = dataFromLocalStorage
21+
// ? JSON.parse(dataFromLocalStorage)
22+
// : null;
23+
// const nodes = response?.nodes;
24+
// setDatas(nodes);
25+
console.log("Edges", edges);
26+
console.log("Nodes", nodes);
27+
}, [edges, nodes]);
1128
return (
1229
<div className="fixed top-0 left-0 w-full border-b border-gray-300 p-2 bg-white z-20 px-4">
1330
<div className="flex justify-between gap-4">
@@ -19,7 +36,7 @@ const Navbar: FunctionComponent<Props> = ({ onSave }) => {
1936
</div>
2037
</div>
2138
<div className="space-x-4">
22-
<Modal />
39+
{edges && edges.length > 0 && <Modal nodes={nodes} edges={edges} />}
2340
<Save
2441
onClick={onSave}
2542
className="text-gray-500 text-2xl cursor-pointer"

src/Component/CopyToClipboardButton.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const CopyToClipboardButton: React.FC<CopyToClipboardButtonProps> = ({
1919

2020
return (
2121
<CopyToClipboard text={text} onCopy={handleCopy}>
22-
<div className="px-2 cursor-default">
22+
<div className="px-1 cursor-default">
2323
<CopyAll className="h-3 w-3 text-blue-600" />
2424
</div>
2525
</CopyToClipboard>

src/Component/Modal/ChatsBot.js

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
import { ThemeProvider } from "styled-components";
22
import ChatBot from "react-simple-chatbot";
33

4-
const ChatsBot = () => {
5-
const dataFromLocalStorage = localStorage.getItem("example-flow");
6-
const response = dataFromLocalStorage
7-
? JSON.parse(dataFromLocalStorage)
8-
: null;
9-
const datas = response.nodes;
10-
11-
if (!datas || datas.length === 0) {
12-
return null;
13-
}
14-
15-
const steps = datas.flatMap((node, index) => {
4+
const ChatsBot = ({ nodes, edges }) => {
5+
const steps = nodes?.flatMap((node, index) => {
166
const dataLabel = node.data.label
177
.split("\n")
188
.map((paragraph, index) => <p key={index}>{paragraph}</p>);
199

20-
if (index === datas.length - 1) {
10+
if (index === nodes.length - 1 && nodes.length === 2) {
11+
return [
12+
{
13+
id: node.id,
14+
user: true,
15+
end: true,
16+
validator: (value) => {
17+
if (value === node.data.label) {
18+
return true;
19+
} else {
20+
return "Keyword yang anda masukan salah";
21+
}
22+
},
23+
},
24+
];
25+
} else if (index === nodes.length - 1 && nodes.length > 2) {
2126
return [
2227
{
2328
id: node.id,
@@ -32,7 +37,7 @@ const ChatsBot = () => {
3237
id: node.id,
3338
component: <div>{dataLabel}</div>,
3439
asMessage: true,
35-
trigger: datas[index + 1].id,
40+
trigger: nodes[index + 1].id,
3641
},
3742
];
3843
} else {
@@ -41,7 +46,7 @@ const ChatsBot = () => {
4146
{
4247
id: node.id,
4348
user: true,
44-
trigger: datas[index + 1].id,
49+
trigger: nodes[index + 1].id,
4550
validator: (value) => {
4651
if (value === node.data.label) {
4752
return true;
@@ -59,12 +64,12 @@ const ChatsBot = () => {
5964
{
6065
value: "Nasi Padang",
6166
label: "Nasi Padang",
62-
trigger: datas[index + 1].id,
67+
trigger: nodes[index + 1].id,
6368
},
6469
{
6570
value: "Soto",
6671
label: "Soto",
67-
trigger: datas[index + 1].id,
72+
trigger: nodes[index + 1].id,
6873
},
6974
],
7075
},

src/Component/Modal/index.js src/Component/Modal/index.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ import * as React from "react";
22
import Dialog from "@mui/material/Dialog";
33
import { PlayArrowRounded } from "@mui/icons-material";
44
import ChatsBot from "./ChatsBot";
5+
import { Edge, Node } from "reactflow";
56

6-
export default function AlertDialog() {
7+
interface Props {
8+
nodes?: Node<any, string | undefined>[] | undefined;
9+
edges?: Edge<any>[] | undefined;
10+
}
11+
12+
const AlertDialog: React.FunctionComponent<Props> = ({ nodes, edges }) => {
713
const [open, setOpen] = React.useState(false);
814

915
const handleClickOpen = () => {
@@ -21,8 +27,10 @@ export default function AlertDialog() {
2127
onClick={handleClickOpen}
2228
/>
2329
<Dialog open={open} onClose={handleClose}>
24-
<ChatsBot />
30+
<ChatsBot nodes={nodes} edges={edges} />
2531
</Dialog>
2632
</React.Fragment>
2733
);
28-
}
34+
};
35+
36+
export default AlertDialog;

0 commit comments

Comments
 (0)