Update NewChatForm.tsx

This commit is contained in:
NotII
2025-03-18 04:05:16 +01:00
parent e0752fd7bb
commit e069ca865c

View File

@@ -179,11 +179,18 @@ export default function NewChatForm() {
const handleSubmit = async (e: React.FormEvent) => { const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
if (!buyerId || !selectedStore) { if (!buyerId) {
toast.error("Please fill all required fields"); toast.error("Please select a customer");
return; return;
} }
if (vendorStores.length === 0) {
toast.error("No store available. Please create a store first.");
return;
}
const storeId = vendorStores[0]._id;
setLoading(true); setLoading(true);
try { try {
const authAxios = getAuthAxios(); const authAxios = getAuthAxios();
@@ -195,7 +202,7 @@ export default function NewChatForm() {
const response = await authAxios.post("/chats/create", { const response = await authAxios.post("/chats/create", {
buyerId, buyerId,
storeId: selectedStore, storeId: storeId,
initialMessage: initialMessage.trim() || undefined initialMessage: initialMessage.trim() || undefined
}); });
@@ -305,43 +312,6 @@ export default function NewChatForm() {
)} )}
</div> </div>
<div className="space-y-2">
<Label htmlFor="store">Store</Label>
{vendorStores.length === 0 ? (
<div>
<p className="text-sm text-muted-foreground p-2 border rounded-md">
No store available. Please create a store first.
</p>
</div>
) : vendorStores.length === 1 ? (
<div className="p-2 border rounded-md bg-muted/20">
<p className="text-sm font-medium">
{vendorStores[0].name}
</p>
<input
type="hidden"
value={vendorStores[0]._id}
name="storeId"
/>
</div>
) : (
<select
id="store"
value={selectedStore}
onChange={(e) => setSelectedStore(e.target.value)}
className="w-full rounded-md border border-input bg-background px-3 py-2"
required
>
<option value="" disabled>Select a store</option>
{vendorStores.map((store) => (
<option key={store._id} value={store._id}>
{store.name}
</option>
))}
</select>
)}
</div>
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="initialMessage">Initial Message (Optional)</Label> <Label htmlFor="initialMessage">Initial Message (Optional)</Label>
<Textarea <Textarea
@@ -362,7 +332,7 @@ export default function NewChatForm() {
> >
Cancel Cancel
</Button> </Button>
<Button type="submit" disabled={loading || !buyerId || !selectedStore}> <Button type="submit" disabled={loading || !buyerId || vendorStores.length === 0}>
{loading ? ( {loading ? (
<> <>
<RefreshCw className="mr-2 h-4 w-4 animate-spin" /> <RefreshCw className="mr-2 h-4 w-4 animate-spin" />