Skip to content

Commit 6b7c798

Browse files
committed
file upload working, still some more validation to do
1 parent 8659c06 commit 6b7c798

File tree

1 file changed

+66
-63
lines changed

1 file changed

+66
-63
lines changed

src/App.tsx

Lines changed: 66 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,82 @@
1-
// src/App.tsx
2-
31
import React from "react";
42
import ChatBot, { Flow } from "react-chatbotify";
5-
63
import RcbPlugin from "./factory/RcbPluginFactory";
74
import { InputValidatorBlock } from "./types/InputValidatorBlock";
8-
import { ValidationResult } from "./types/ValidationResult";
95

106
const App = () => {
11-
// Initialize the plugin
12-
const plugins = [RcbPlugin()];
7+
// Initialize the plugin
8+
const plugins = [RcbPlugin()];
139

14-
// Define the validation function for file uploads
15-
const validateFile = (userInput?: File): ValidationResult => {
16-
if (!userInput) {
17-
return { success: false, promptContent: "No file selected.", promptType: "error" };
18-
}
10+
const handleUpload = (params: { files?: FileList }) => {
11+
const files = params.files;
1912

20-
const allowedTypes = ["image/jpeg", "image/png"];
21-
const maxSizeInBytes = 5 * 1024 * 1024; // 5MB
13+
if (!files || files.length === 0) {
14+
return { success: false, promptContent: "No file selected." };
15+
}
2216

23-
if (!allowedTypes.includes(userInput.type)) {
24-
return {
25-
success: false,
26-
promptContent: "Only JPEG and PNG images are allowed.",
27-
promptType: "error",
28-
};
29-
}
17+
const file = files[0];
18+
const maxSize = 5 * 1024 * 1024; // 5MB
3019

31-
if (userInput.size > maxSizeInBytes) {
32-
return {
33-
success: false,
34-
promptContent: "File size must be less than 5MB.",
35-
promptType: "error",
36-
};
37-
}
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(/^image\/(jpeg|jpg|png)$/)) {
29+
return {
30+
success: false,
31+
promptContent: "Only JPEG and PNG files are allowed.",
32+
};
33+
}
3834

39-
return { success: true };
40-
};
35+
if (file.size > maxSize) {
36+
return {
37+
success: false,
38+
promptContent: "File size must be less than 5MB.",
39+
};
40+
}
4141

42-
// Define the flow
43-
const flow: Flow = {
44-
start: {
45-
message: "Hey there! Please enter your age.",
46-
path: "age_validation",
47-
validateInput: (userInput?: string) => {
48-
if (userInput && !Number.isNaN(Number(userInput))) {
49-
return { success: true };
50-
}
51-
return {
52-
success: false,
53-
promptContent: "Age must be a number!",
54-
promptDuration: 3000,
55-
promptType: "error",
56-
highlightTextArea: true,
57-
};
58-
},
59-
} as InputValidatorBlock,
60-
age_validation: {
61-
message: "Great! Now please upload a profile picture (JPEG or PNG).",
62-
path: "file_upload_validation",
63-
validateInput: validateFile,
64-
} as InputValidatorBlock,
65-
file_upload_validation: {
66-
message: "Thank you! Your profile picture has been uploaded successfully.",
67-
path: "end",
68-
},
69-
end: {
70-
message: "This is the end of the flow. Thank you!",
71-
},
72-
};
42+
// If all checks pass
43+
console.log("File validation passed:", file.name);
44+
return { success: true };
45+
};
46+
47+
const flow: Flow = {
48+
start: {
49+
message: "Hey there! Please enter your age.",
50+
path: "age_validation",
51+
validateInput: (userInput?: string) => {
52+
if (userInput && !Number.isNaN(Number(userInput))) {
53+
return { success: true };
54+
}
55+
return {
56+
success: false,
57+
promptContent: "Age must be a number!",
58+
promptDuration: 3000,
59+
promptType: "error",
60+
highlightTextArea: true,
61+
};
62+
},
63+
} as InputValidatorBlock,
64+
age_validation: {
65+
message: "Great! Now please upload a profile picture (JPEG or PNG).",
66+
file: (params) => handleUpload(params),
67+
path: "file_upload_validation",
68+
// Removed validateInput
69+
},
70+
file_upload_validation: {
71+
message: "Thank you! Your profile picture has been uploaded successfully.",
72+
path: "end",
73+
},
74+
end: {
75+
message: "This is the end of the flow. Thank you!",
76+
},
77+
};
7378

74-
return (
75-
<ChatBot id="chatbot-id" plugins={plugins} flow={flow} />
76-
);
79+
return <ChatBot id="chatbot-id" plugins={plugins} flow={flow} />;
7780
};
7881

7982
export default App;

0 commit comments

Comments
 (0)