updated modal

This commit is contained in:
Krishav Raj Singh 2025-07-01 22:53:42 +05:30
parent 21181fb8b9
commit a6dc89666a
1 changed files with 42 additions and 27 deletions

View File

@ -4,68 +4,83 @@ import { ThumbsDown, ThumbsUp } from "lucide-react";
import { useState } from "react"; import { useState } from "react";
import { Textarea } from "../ui/textarea"; import { Textarea } from "../ui/textarea";
import { toast } from "sonner"; import { toast } from "sonner";
import { apiClient } from '@/lib/api-client';
interface FeedbackProps {
messageId: string;
}
export default function Feedback({ messageId }: { messageId: string }) { export default function Feedback({ messageId }: FeedbackProps) {
const [open, setOpen] = useState<boolean>(false); const [open, setOpen] = useState<boolean>(false);
const [feedback, setFeedback] = useState<'positive' | 'negative' | null>(null); const [responseIsGood, setResponseIsGood] = useState<boolean | null>(null);
const [comment, setComment] = useState<string>(''); const [feedback, setFeedback] = useState<string>('');
const [submitting, setSubmitting] = useState(false);
const handleClick = (feedback: 'positive' | 'negative') => { const handleClick = (isGood: boolean) => {
setFeedback(feedback); setResponseIsGood(isGood);
setOpen(true); setOpen(true);
} };
const handleSubmit = () => { const handleSubmit = async () => {
if(!feedback) return; if (responseIsGood === null) return;
console.log(messageId, feedback, comment); setSubmitting(true);
const { success } = await apiClient.post('/api/feedback', {
message_id: messageId,
response_is_good: responseIsGood,
comment: feedback.trim() || null,
});
setSubmitting(false);
if (success) {
toast.success('Feedback submitted - thank you!');
setOpen(false); setOpen(false);
setFeedback(null); setFeedback('');
setComment(''); setResponseIsGood(null);
toast.success('Feedback submitted');
} }
};
return ( return (
<div className="flex items-center gap-1"> <div className="flex items-center gap-1">
<Button <Button
className="opacity-70 hover:opacity-100" className="h-7 w-7 opacity-70 hover:opacity-100"
variant="ghost" variant="ghost"
onClick={() => handleClick('positive')} onClick={() => handleClick(true)}
> >
<ThumbsUp /> <ThumbsUp className="h-4 w-4" />
<span className="sr-only">Positive feedback</span> <span className="sr-only">Good response</span>
</Button> </Button>
<Button <Button
className="opacity-70 hover:opacity-100" className="h-7 w-7 opacity-70 hover:opacity-100"
size="icon" size="icon"
variant="ghost" variant="ghost"
onClick={() => handleClick('negative')} onClick={() => handleClick(false)}
> >
<ThumbsDown /> <ThumbsDown className="h-4 w-4" />
<span className="sr-only">Negative feedback</span> <span className="sr-only">Bad response</span>
</Button> </Button>
<Dialog open={open} onOpenChange={setOpen}> <Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="max-w-md"> <DialogContent className="max-w-md">
<DialogHeader> <DialogHeader>
<DialogTitle>Feedback</DialogTitle> <DialogTitle>{responseIsGood ? 'Good response' : 'Bad response'}</DialogTitle>
</DialogHeader> </DialogHeader>
<span <span
className="text-sm text-muted-foreground" className="text-sm text-muted-foreground"
> >
{`What was ${feedback === 'negative' ? 'un' : ''}satisifying about this response?`} {`What was ${responseIsGood === false ? 'un' : ''}satisifying about this response?`}
</span> </span>
<Textarea <Textarea
className="resize-none my-2" className="resize-none my-2"
placeholder="Please provide feedback..." placeholder="Please provide feedback..."
value={comment} value={feedback}
onChange={(e) => setComment(e.target.value)} onChange={(e) => setFeedback(e.target.value)}
/> />
<DialogFooter className="gap-2"> <DialogFooter className="gap-2">
<DialogClose asChild> <DialogClose asChild>
<Button variant="outline">Cancel</Button> <Button variant="outline">Cancel</Button>
</DialogClose> </DialogClose>
<Button onClick={handleSubmit}>Submit</Button> <Button onClick={handleSubmit} disabled={submitting || responseIsGood === null}>
{submitting ? 'Submitting...' : 'Submit'}
</Button>
</DialogFooter> </DialogFooter>
</DialogContent> </DialogContent>
</Dialog> </Dialog>