2025-04-15 16:28:03 +05:30
|
|
|
import React from 'react';
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import { Loader2 } from "lucide-react";
|
2025-07-30 13:22:06 +05:30
|
|
|
import { useForum } from "@/contexts/useForum";
|
|
|
|
|
import { useAuth } from "@/contexts/useAuth";
|
2025-04-15 16:28:03 +05:30
|
|
|
import {
|
|
|
|
|
Form,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
|
|
|
|
} from "@/components/ui/form";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
DialogTrigger,
|
|
|
|
|
} from "@/components/ui/dialog";
|
2025-06-28 07:13:32 +05:30
|
|
|
import { useToast } from "@/hooks/use-toast";
|
|
|
|
|
import { urlLoads } from "@/lib/utils/urlLoads";
|
2025-04-15 16:28:03 +05:30
|
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
|
title: z.string().min(3, "Title must be at least 3 characters").max(50, "Title must be less than 50 characters"),
|
|
|
|
|
description: z.string().min(10, "Description must be at least 10 characters").max(200, "Description must be less than 200 characters"),
|
2025-06-28 07:13:32 +05:30
|
|
|
icon: z
|
|
|
|
|
.string()
|
|
|
|
|
.optional()
|
|
|
|
|
.refine((val) => !val || val.length === 0 || URL.canParse(val), {
|
|
|
|
|
message: "Must be a valid URL"
|
|
|
|
|
}),
|
2025-04-15 16:28:03 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export function CreateCellDialog() {
|
2025-04-22 10:39:32 +05:30
|
|
|
const { createCell, isPostingCell } = useForum();
|
2025-04-15 16:28:03 +05:30
|
|
|
const { isAuthenticated } = useAuth();
|
2025-06-28 07:13:32 +05:30
|
|
|
const { toast } = useToast();
|
2025-04-15 16:28:03 +05:30
|
|
|
const [open, setOpen] = React.useState(false);
|
|
|
|
|
|
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
|
defaultValues: {
|
|
|
|
|
title: "",
|
|
|
|
|
description: "",
|
2025-06-28 07:13:32 +05:30
|
|
|
icon: undefined,
|
2025-04-15 16:28:03 +05:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const onSubmit = async (values: z.infer<typeof formSchema>) => {
|
2025-06-28 07:13:32 +05:30
|
|
|
// Validate icon URL if provided
|
|
|
|
|
if (values.icon && values.icon.trim()) {
|
|
|
|
|
const ok = await urlLoads(values.icon, 5000);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
toast({
|
|
|
|
|
title: "Icon URL Error",
|
|
|
|
|
description: "Icon URL could not be loaded. Please check the URL and try again.",
|
|
|
|
|
variant: "destructive",
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cell = await createCell(values.title, values.description, values.icon || undefined);
|
2025-04-15 16:28:03 +05:30
|
|
|
if (cell) {
|
|
|
|
|
setOpen(false);
|
|
|
|
|
form.reset();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!isAuthenticated) return null;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
|
|
|
<DialogTrigger asChild>
|
|
|
|
|
<Button variant="outline" className="w-full">Create New Cell</Button>
|
|
|
|
|
</DialogTrigger>
|
|
|
|
|
<DialogContent className="sm:max-w-[425px]">
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Create a New Cell</DialogTitle>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<Form {...form}>
|
|
|
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="title"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel>Title</FormLabel>
|
|
|
|
|
<FormControl>
|
2025-04-22 10:39:32 +05:30
|
|
|
<Input placeholder="Enter cell title" {...field} disabled={isPostingCell} />
|
2025-04-15 16:28:03 +05:30
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="description"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel>Description</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Textarea
|
|
|
|
|
placeholder="Enter cell description"
|
|
|
|
|
{...field}
|
2025-04-22 10:39:32 +05:30
|
|
|
disabled={isPostingCell}
|
2025-04-15 16:28:03 +05:30
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="icon"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
2025-06-28 07:13:32 +05:30
|
|
|
<FormLabel>Icon URL (optional)</FormLabel>
|
2025-04-15 16:28:03 +05:30
|
|
|
<FormControl>
|
|
|
|
|
<Input
|
2025-06-28 07:13:32 +05:30
|
|
|
placeholder="Enter icon URL (optional)"
|
2025-04-15 16:28:03 +05:30
|
|
|
type="url"
|
|
|
|
|
{...field}
|
2025-06-28 07:13:32 +05:30
|
|
|
value={field.value || ""}
|
2025-04-22 10:39:32 +05:30
|
|
|
disabled={isPostingCell}
|
2025-04-15 16:28:03 +05:30
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="w-full"
|
2025-04-22 10:39:32 +05:30
|
|
|
disabled={isPostingCell}
|
2025-04-15 16:28:03 +05:30
|
|
|
>
|
2025-04-22 10:39:32 +05:30
|
|
|
{isPostingCell && (
|
2025-04-15 16:28:03 +05:30
|
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
|
|
|
|
)}
|
|
|
|
|
Create Cell
|
|
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|