Skip to content

Commit 9005e73

Browse files
committed
toast functionality
1 parent 5b94578 commit 9005e73

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

src/frontend_react/src/components/content/HomeInput.tsx

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
Card,
88
Text,
99
Title2,
10+
Toast,
11+
ToastBody,
12+
ToastTitle,
1013
} from "@fluentui/react-components";
1114
import {
1215
Send20Regular,
@@ -27,6 +30,9 @@ const HomeInput: React.FC<HomeInputProps> = ({
2730
onQuickTaskSelect,
2831
}) => {
2932
const [input, setInput] = useState("");
33+
const [submitting, setSubmitting] = useState(false);
34+
const [showToast, setShowToast] = useState(false);
35+
const [error, setError] = useState<string | null>(null);
3036
const textareaRef = useRef<HTMLTextAreaElement>(null);
3137
const navigate = useNavigate();
3238

@@ -49,22 +55,35 @@ const HomeInput: React.FC<HomeInputProps> = ({
4955

5056
const handleSubmit = async () => {
5157
if (input.trim()) {
58+
setSubmitting(true);
59+
setShowToast(true);
60+
setError(null);
5261
try {
5362
// Submit the input task using TaskService
5463
const response = await TaskService.submitInputTask(input.trim());
5564

5665
// Clear the input field
57-
setInput("");
66+
//setInput("");
5867
if (textareaRef.current) {
5968
textareaRef.current.style.height = "auto";
6069
}
6170

62-
// Navigate to the plan page using the plan_id from the response
63-
navigate(`/plan/${response.plan_id}`);
71+
console.log('Task response', response);
72+
if (response.plan_id != null) {
73+
// plan_id is valid (not null or undefined)
74+
navigate(`/plan/${response.plan_id}`);
75+
} else {
76+
// plan_id is not valid, handle accordingly
77+
console.log('Invalid plan:', response.status);
78+
setShowToast(false);
79+
setError("Failed to create task. Please try again.");
80+
}
6481

65-
} catch (error) {
66-
console.error('Failed to create task:', error);
67-
// You can add error handling here if needed
82+
} catch (error: any) {
83+
console.log('Failed to create task:', error);
84+
setError(error.message || "Failed to create task.");
85+
} finally {
86+
setSubmitting(false);
6887
}
6988
}
7089
};
@@ -101,12 +120,13 @@ const HomeInput: React.FC<HomeInputProps> = ({
101120
<ChatInput
102121
value={input}
103122
placeholder="Describe what you'd like to do or use / to reference files, people, and more" onChange={setInput}
123+
disabledChat={submitting}
104124
>
105125
<Button
106126
appearance="subtle"
107127
className="home-input-send-button"
108128
onClick={handleSubmit}
109-
disabled={!input.trim()}
129+
disabled={submitting}
110130
icon={<Send20Regular />}
111131
/>
112132
</ChatInput>
@@ -137,6 +157,23 @@ const HomeInput: React.FC<HomeInputProps> = ({
137157
))}
138158
</div>
139159
</div>
160+
{/* Toast appears after quick tasks */}
161+
{showToast && (
162+
<div style={{ marginTop: 16 }}>
163+
<Toast>
164+
<ToastTitle>Task submitted!</ToastTitle>
165+
<ToastBody>Your task is processing.</ToastBody>
166+
</Toast>
167+
</div>
168+
)}
169+
{error && (
170+
<div style={{ marginTop: 16 }}>
171+
<Toast>
172+
<ToastTitle>Task failed!</ToastTitle>
173+
<ToastBody>{error}</ToastBody>
174+
</Toast>
175+
</div>
176+
)}
140177
</div>
141178
</div>
142179
</div>

src/frontend_react/src/services/TaskService.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,19 @@ export class TaskService {
147147
description: description
148148
};
149149

150-
return await apiService.submitInputTask(inputTask);
150+
try {
151+
return await apiService.submitInputTask(inputTask);
152+
} catch (error: any) {
153+
// You can customize this logic as needed
154+
let message = "Failed to create task.";
155+
if (error?.response?.data?.message) {
156+
message = error.response.data.message;
157+
} else if (error?.message) {
158+
message = error.message;
159+
}
160+
// Throw a new error with a user-friendly message
161+
throw new Error(message);
162+
}
151163
}
152164
}
153165

0 commit comments

Comments
 (0)