@@ -2,48 +2,11 @@ import React from "react";
22import ChatBot , { Flow } from "react-chatbotify" ;
33import RcbPlugin from "./factory/RcbPluginFactory" ;
44import { InputValidatorBlock } from "./types/InputValidatorBlock" ;
5+ import { validateFile } from "./utils/validateFile" ;
56
67const App = ( ) => {
7- // Initialize the plugin
88 const plugins = [ RcbPlugin ( ) ] ;
99
10- const handleUpload = ( params : { files ?: FileList } ) => {
11- const files = params . files ;
12-
13- if ( ! files || files . length === 0 ) {
14- return { success : false , promptContent : "No file selected." } ;
15- }
16-
17- const file = files [ 0 ] ;
18- const maxSize = 5 * 1024 * 1024 ; // 5MB
19-
20- // Debugging log for file details
21- console . log ( "Uploaded file details:" , {
22- name : file . name ,
23- type : file . type ,
24- size : file . size ,
25- } ) ;
26-
27- // Adjusted MIME type checking
28- if ( ! file . type . match ( / ^ i m a g e \/ ( j p e g | j p g | p n g ) $ / ) ) {
29- return {
30- success : false ,
31- promptContent : "Only JPEG and PNG files are allowed." ,
32- } ;
33- }
34-
35- if ( file . size > maxSize ) {
36- return {
37- success : false ,
38- promptContent : "File size must be less than 5MB." ,
39- } ;
40- }
41-
42- // If all checks pass
43- console . log ( "File validation passed:" , file . name ) ;
44- return { success : true } ;
45- } ;
46-
4710 const flow : Flow = {
4811 start : {
4912 message : "Hey there! Please enter your age." ,
@@ -61,21 +24,61 @@ const App = () => {
6124 } ;
6225 } ,
6326 } as InputValidatorBlock ,
27+
6428 age_validation : {
6529 message : "Great! Now please upload a profile picture (JPEG or PNG)." ,
66- file : ( params ) => handleUpload ( params ) ,
6730 path : "file_upload_validation" ,
68- // Removed validateInput
69- } ,
31+ validateInput : ( userInput ?: string ) => {
32+ console . log ( "validateInput called with userInput:" , userInput ) ;
33+
34+ // Allow empty input or file names with allowed extensions
35+ if (
36+ ! userInput ||
37+ / \. ( j p g | j p e g | p n g ) $ / i. test ( userInput . trim ( ) )
38+ ) {
39+ return { success : true } ;
40+ }
41+
42+ // Disallow other text inputs
43+ return {
44+ success : false ,
45+ promptContent : "Please upload a file." ,
46+ promptDuration : 3000 ,
47+ promptType : "error" ,
48+ } ;
49+ } ,
50+ validateFile : ( file ?: File ) => {
51+ return validateFile ( file ) ; // Validate file input
52+ } ,
53+ file : async ( { files } ) => {
54+ console . log ( "Files received:" , files ) ;
55+
56+ if ( files && files [ 0 ] ) {
57+ const validationResult = validateFile ( files [ 0 ] ) ;
58+ if ( ! validationResult . success ) {
59+ console . error ( validationResult . promptContent ) ;
60+ return ;
61+ }
62+ console . log ( "File uploaded successfully:" , files [ 0 ] ) ;
63+ } else {
64+ console . error ( "No file provided." ) ;
65+ }
66+ } ,
67+ } as InputValidatorBlock ,
68+
7069 file_upload_validation : {
71- message : "Thank you! Your profile picture has been uploaded successfully." ,
70+ message :
71+ "Thank you! Your profile picture has been uploaded successfully." ,
7272 path : "end" ,
7373 } ,
74+
7475 end : {
7576 message : "This is the end of the flow. Thank you!" ,
7677 } ,
7778 } ;
7879
80+
81+
7982 return < ChatBot id = "chatbot-id" plugins = { plugins } flow = { flow } /> ;
8083} ;
8184
0 commit comments