Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/responsive input #209

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 62 additions & 52 deletions frontend/src/components/gemini/LuckyAdviser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ function LuckyAdviser() {
const [inputValue, setInputValue] = useState('');
const [promptResponses, setPromptResponses] = useState([]);
const [loading, setLoading] = useState(false);
const [showResponses, setShowResponses] = useState(true); // State to manage response box visibility
const [showResponses, setShowResponses] = useState(true);

// Create axios instance with base URL and authorization token
const axiosInstance = axios.create({
baseURL: "http://localhost:4000",
headers: { Authorization: `Bearer ${localStorage.getItem("token")}` },
});

const getResponseForGivenPrompt = async () => {
const getResponseForGivenPrompt = async (e) => {
e.preventDefault();
if (!inputValue.trim()) {
console.error("Input value is required.");
return;
Expand Down Expand Up @@ -64,57 +65,66 @@ function LuckyAdviser() {
setShowResponses(false); // Close the response box
};

return (
<div className="container mx-auto p-4">
<div className="flex space-x-4 mb-4">
<div className="flex-1">
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder="Ask for financial advice or education..."
className="border border-gray-300 rounded-md p-2 w-full focus:outline-none focus:ring-2 focus:ring-primary-green"
/>
return (
<div className="container mx-auto p-4">
<div className="flex flex-col mb-4">
<div className="relative flex items-end space-x-2">
<input
id="prompt"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder="Ask for financial advice or education..."
className="block w-full p-2 text-sm border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary-green resize-none"
required
disabled={loading} // Disable button when loading
/>
<button
onClick={getResponseForGivenPrompt}
type="submit" disabled={loading}
className={`bg-white text-primary-green rounded-md border border-primary-green hover:bg-gray-200 transition
font-medium text-sm px-3 py-2 ${loading ? 'opacity-50' : ''}`}>
Send
</button>
</div>
</div>
{loading ? (
<div className="text-center mt-3">
<div className="spinner-border text-primary">
<span className="visually-hidden">
<LoadingSpinner />
</span>
</div>
</div>
) : (
showResponses && promptResponses.length > 0 && (
<div className="flex flex-col mt-8 mb-4 p-4 border border-gray-300 rounded-md shadow-md">
{promptResponses.map((promptResponse, index) => (
<div key={index}>
<div className="font-bold text-lg mb-2">{promptResponse.title}</div>
<ul>
{promptResponse.content.map((item, idx) => (
<li key={idx} className="mb-1">{item}</li>
))}
</ul>
</div>
))}
<button
onClick={handleCloseResponses}
className="self-end bg-white text-primary-green rounded-md px-4 py-2 border border-primary-green hover:bg-gray-200 transition"
>
Close
</button>
</div>
)
)}
<div className="flex justify-end mt-2">
<LuckyButton
onGetAdvice={handleLuckyAdvice}
loading={loading} // Disable when loading
/>
</div>
</div>
<div className="col-auto">
<button
onClick={getResponseForGivenPrompt}
className="bg-white text-primary-green rounded-md px-4 py-2 border border-primary-green hover:bg-gray-200 transition">
Send</button>
</div>
<LuckyButton onGetAdvice={handleLuckyAdvice} />
</div>
{loading ? (
<div className="text-center mt-3">
<div className="spinner-border text-primary">
<span className="visually-hidden">
<LoadingSpinner />
</span>
</div>
</div>
) : (
showResponses && promptResponses.length > 0 && (
<div className="flex flex-col mb-4 p-4 border border-gray-300 rounded-md shadow-md">
{promptResponses.map((promptResponse, index) => (
<div key={index}>
<div className="font-bold text-lg mb-2">{promptResponse.title}</div>
<ul>
{promptResponse.content.map((item, idx) => (
<li key={idx} className="mb-1">{item}</li>
))}
</ul>
</div>
))}
<button
onClick={handleCloseResponses}
className="self-end bg-white text-primary-green rounded-md px-4 py-2 border border-primary-green hover:bg-gray-200 transition"
>
Close
</button>
</div>
))}
</div>
);
);
}

export default LuckyAdviser;
9 changes: 7 additions & 2 deletions frontend/src/components/gemini/LuckyButton.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// LuckyAdviceButton.jsx
import React from 'react';

function LuckyButton({ onGetAdvice }) {
function LuckyButton({ onGetAdvice, loading }) {
console.log("loading",loading)
const handleClick = () => {
onGetAdvice();
};

return (
<button onClick={handleClick} className="bg-primary-green text-white rounded-md px-4 py-2 hover:bg-primary-green-dark transition">
<button onClick={handleClick}
className={`bg-primary-green text-white rounded-md px-4 py-2 hover:bg-primary-green-dark transition
${loading ? 'opacity-50' : ''}`}
disabled={loading} // Disable button when loading
>
I'm Feeling Lucky!
</button>
);
Expand Down