|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 5 | +import { useHookFormAction } from '@next-safe-action/adapter-react-hook-form/hooks'; |
| 6 | +import { createTodo } from '../actions'; |
| 7 | +import { createTodoSchema } from '../schemas'; |
| 8 | +import { toast } from 'sonner'; |
| 9 | +import { useEventBus } from '@/hooks/use-event-bus'; |
| 10 | +import { useRouter } from 'next/navigation'; |
| 11 | + |
| 12 | +export default function CreateTodoForm(props: { userId: string }) { |
| 13 | + const [isFormOpen, setIsFormOpen] = useState(false); |
| 14 | + const router = useRouter(); |
| 15 | + |
| 16 | + useEventBus({ |
| 17 | + channelName: `user/${props.userId}/jobs`, |
| 18 | + onReceived: (data) => { |
| 19 | + console.log('received', data); |
| 20 | + router.refresh(); |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
| 24 | + const { |
| 25 | + form: { register, formState, reset }, |
| 26 | + action, |
| 27 | + handleSubmitWithAction, |
| 28 | + } = useHookFormAction(createTodo, zodResolver(createTodoSchema), { |
| 29 | + actionProps: { |
| 30 | + onSuccess: () => { |
| 31 | + toast.success('Todo created successfully'); |
| 32 | + reset(); |
| 33 | + setIsFormOpen(false); |
| 34 | + }, |
| 35 | + onError: ({ error }) => { |
| 36 | + toast.error(typeof error === 'string' ? error : 'Failed to create todo'); |
| 37 | + }, |
| 38 | + }, |
| 39 | + formProps: { |
| 40 | + defaultValues: { |
| 41 | + title: '', |
| 42 | + description: '', |
| 43 | + }, |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + if (!isFormOpen) { |
| 48 | + return ( |
| 49 | + <div className="mb-6"> |
| 50 | + <button |
| 51 | + onClick={() => setIsFormOpen(true)} |
| 52 | + className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" |
| 53 | + > |
| 54 | + + Add New Todo |
| 55 | + </button> |
| 56 | + </div> |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <div className="bg-white shadow-md rounded-lg p-6 mb-6"> |
| 62 | + <h2 className="text-lg font-medium text-gray-900 mb-4">Create New Todo</h2> |
| 63 | + <form onSubmit={handleSubmitWithAction} className="space-y-4"> |
| 64 | + <div> |
| 65 | + <label htmlFor="title" className="block text-sm font-medium text-gray-700"> |
| 66 | + Title |
| 67 | + </label> |
| 68 | + <input |
| 69 | + id="title" |
| 70 | + {...register('title')} |
| 71 | + placeholder="Your TODO item title." |
| 72 | + className="mt-1 p-2 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" |
| 73 | + /> |
| 74 | + {formState.errors.title && <p className="mt-1 text-sm text-red-600">{formState.errors.title.message}</p>} |
| 75 | + </div> |
| 76 | + |
| 77 | + <div> |
| 78 | + <label htmlFor="description" className="block text-sm font-medium text-gray-700"> |
| 79 | + Description |
| 80 | + </label> |
| 81 | + <textarea |
| 82 | + id="description" |
| 83 | + {...register('description')} |
| 84 | + rows={3} |
| 85 | + placeholder="Describe your TODO item." |
| 86 | + className="mt-1 p-2 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" |
| 87 | + /> |
| 88 | + {formState.errors.description && ( |
| 89 | + <p className="mt-1 text-sm text-red-600">{formState.errors.description.message}</p> |
| 90 | + )} |
| 91 | + </div> |
| 92 | + |
| 93 | + <div className="flex justify-end space-x-2"> |
| 94 | + <button |
| 95 | + type="button" |
| 96 | + onClick={() => setIsFormOpen(false)} |
| 97 | + className="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-gray-700 bg-gray-200 hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500" |
| 98 | + > |
| 99 | + Cancel |
| 100 | + </button> |
| 101 | + <button |
| 102 | + type="submit" |
| 103 | + disabled={action.isExecuting} |
| 104 | + className="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" |
| 105 | + > |
| 106 | + {action.isExecuting ? 'Creating...' : 'Create Todo'} |
| 107 | + </button> |
| 108 | + </div> |
| 109 | + </form> |
| 110 | + </div> |
| 111 | + ); |
| 112 | +} |
0 commit comments