Skip to content

Commit 7dc6b27

Browse files
committed
made changes as requested
1 parent d42d6e9 commit 7dc6b27

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

src/App.tsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { InputValidatorBlock } from "./types/InputValidatorBlock";
55
import { validateFile } from "./utils/validateFile";
66

77
const App = () => {
8-
// initialize example plugin
8+
// Initialize the plugin
99
const plugins = [RcbPlugin()];
1010

11-
// example flow for testing
11+
// Example flow for testing
1212
const flow: Flow = {
1313
start: {
1414
message: "Hey there! Please enter your age.",
1515
path: "age_validation",
16-
validateInput: (userInput?: string) => {
16+
validateTextInput: (userInput?: string) => {
1717
if (userInput && !Number.isNaN(Number(userInput))) {
1818
return { success: true };
1919
}
@@ -28,23 +28,23 @@ const App = () => {
2828
} as InputValidatorBlock,
2929

3030
age_validation: {
31-
message: "Great! Now please upload a profile picture (JPEG or PNG).",
31+
message:
32+
"Great! Now please upload a profile picture (JPEG or PNG) or provide a URL.",
3233
path: "file_upload_validation",
33-
validateInput: (userInput?: string) => {
34-
console.log("validateInput called with userInput:", userInput);
34+
chatDisabled: true, // Set to true if you want to disable text input
35+
validateTextInput: (userInput?: string) => {
36+
console.log("validateTextInput called with userInput:", userInput);
3537

36-
37-
if (
38-
userInput &&
39-
/\.(jpg|jpeg|png)$/i.test(userInput.trim())
40-
) {
38+
if (userInput && userInput.trim().length > 0) {
39+
// Optionally, validate if the input is a valid URL
40+
// For simplicity, we'll accept any non-empty text
4141
return { success: true };
4242
}
4343

44-
// Disallow other text inputs
4544
return {
4645
success: false,
47-
promptContent: "Please upload a valid file (JPEG or PNG). Empty inputs are not allowed.",
46+
promptContent:
47+
"Please provide a valid URL or upload a file.",
4848
promptDuration: 3000,
4949
promptType: "error",
5050
};
@@ -54,7 +54,7 @@ const App = () => {
5454
},
5555
file: async ({ files }) => {
5656
console.log("Files received:", files);
57-
57+
5858
if (files && files[0]) {
5959
const validationResult = validateFile(files[0]);
6060
if (!validationResult.success) {
@@ -67,23 +67,18 @@ const App = () => {
6767
console.error("No file provided.");
6868
}
6969
},
70-
7170
} as InputValidatorBlock,
7271

7372
file_upload_validation: {
7473
message:
75-
"Thank you! Your picture has been uploaded successfully. You passed the file upload validation!",
74+
"Thank you! Your input has been received. You passed the validation!",
7675
path: "start",
7776
},
78-
}
77+
};
7978

8079
return (
81-
<ChatBot
82-
id="chatbot-id"
83-
plugins={plugins}
84-
flow={flow}
85-
></ChatBot>
86-
);
87-
}
80+
<ChatBot id="chatbot-id" plugins={plugins} flow={flow}></ChatBot>
81+
);
82+
};
8883

8984
export default App;

src/core/useRcbPlugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ const useRcbPlugin = (pluginConfig?: PluginConfig) => {
4040
const rcbEvent = event as RcbUserSubmitTextEvent;
4141

4242
// Get validator and if no validator, return
43-
const validator = getValidator(rcbEvent, getBotId(), getFlow());
43+
const validator = getValidator<string>(
44+
rcbEvent,
45+
getBotId(),
46+
getFlow(),
47+
"validateTextInput"
48+
);
4449
if (!validator) {
4550
return;
4651
}

src/types/InputValidatorBlock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ValidationResult } from "./ValidationResult";
88
*/
99

1010
export type InputValidatorBlock = Omit<Block, "file"> & {
11-
file?: (params: { files?: FileList }) => void | Promise<void>; // Updated
12-
validateInput?: (userInput?: string) => ValidationResult;
11+
file?: (params: { files?: FileList }) => void | Promise<void>;
12+
validateTextInput?: (userInput?: string) => ValidationResult;
1313
validateFileInput?: (file?: File) => ValidationResult;
1414
};

src/utils/getValidator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ type RcbUserEvent = RcbUserSubmitTextEvent | RcbUserUploadFileEvent;
1818
* @returns The validator function if it exists, otherwise undefined.
1919
*/
2020
export const getValidator = <T = string | File>(
21-
event: RcbUserEvent,
22-
currBotId: string | null,
23-
currFlow: Flow,
24-
validatorType: "validateInput" | "validateFileInput" = "validateInput"
25-
): ((input: T) => ValidationResult) | undefined => {
21+
event: RcbUserEvent,
22+
currBotId: string | null,
23+
currFlow: Flow,
24+
validatorType: "validateTextInput" | "validateFileInput" = "validateTextInput"
25+
): ((input: T) => ValidationResult) | undefined => {
2626
if (!event.detail) {
2727
return;
2828
}

0 commit comments

Comments
 (0)