()
| 20 | import { X } from 'lucide-react'; |
| 21 | |
| 22 | export const NewPostDialog = () => { |
| 23 | const { theme } = useTheme(); |
| 24 | const formRef = useRef<ElementRef<'form'>>(null); |
| 25 | const searchParam = useSearchParams(); |
| 26 | const paramsObject = searchParamsToObject(searchParam as any); // build fix (eslint) |
| 27 | const path = usePathname(); |
| 28 | const router = useRouter(); |
| 29 | const tagInputRef = useRef<HTMLInputElement | null>(null); |
| 30 | const [value, setValue] = useState<string>('**Hello world!!!**'); |
| 31 | const [tags, setTags] = useState<string[]>([]); |
| 32 | const containerRef = useRef<HTMLDivElement>(null); |
| 33 | const { ref, onOpen, onClose } = useModal(); |
| 34 | const handleMarkdownChange = (newValue?: string) => { |
| 35 | if (typeof newValue === 'string') { |
| 36 | setValue(newValue); |
| 37 | } |
| 38 | }; |
| 39 | useEffect(() => { |
| 40 | let timeoutId: any; |
| 41 | if (paramsObject.newPost === 'open') { |
| 42 | onOpen(); |
| 43 | |
| 44 | // Cleanup function to clear the timeout |
| 45 | } else { |
| 46 | onClose(); |
| 47 | } |
| 48 | return () => { |
| 49 | clearTimeout(timeoutId); |
| 50 | }; |
| 51 | }, [onClose, onOpen, paramsObject.newPost]); |
| 52 | |
| 53 | const { execute, fieldErrors, setFieldErrors } = useAction(createQuestion, { |
| 54 | onSuccess: (data) => { |
| 55 | toast.success(`Question "${data.title}" created`); |
| 56 | formRef?.current?.reset(); |
| 57 | setValue(''); |
| 58 | router.push(`/question/${data.slug}`); |
| 59 | setTags([]); |
| 60 | handleOnCloseClick(); |
| 61 | }, |
| 62 | onError: (error) => { |
| 63 | toast.error(error); |
| 64 | handleOnCloseClick(); |
| 65 | }, |
| 66 | }); |
| 67 | |
| 68 | const handleOnCloseClick = () => { |
| 69 | router.push(getUpdatedUrl(`${path}/`, paramsObject, { newPost: 'close' })); |
| 70 | if (fieldErrors?.content || fieldErrors?.title || fieldErrors?.tags) { |
| 71 | setFieldErrors({}); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | const onSubmit = (event: React.FormEvent<HTMLFormElement>) => { |
| 76 | event.preventDefault(); |
| 77 | const formData = new FormData(event.currentTarget); |
| 78 | const title = formData.get('title'); |
| 79 | execute({ |
nothing calls this directly
no test coverage detected