|
| 1 | +import {Head, Form, usePage} from '@inertiajs/react' |
| 2 | +import AppLayout from '../../layouts/AppLayout' |
| 3 | +import LexicalRichTextEditor from '../../components/LexicalRichTextEditor' |
| 4 | + |
| 5 | +interface TopicFormProps { |
| 6 | + topic?: { |
| 7 | + title?: string |
| 8 | + category_id?: number |
| 9 | + messages_attributes?: Array<{ body: string }> |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +const TopicsNew = ({topic}: TopicFormProps) => { |
| 14 | + const {categories} = usePage().props |
| 15 | + |
| 16 | + return ( |
| 17 | + <AppLayout> |
| 18 | + <Head title="New Topic - Pups & Pourovers"/> |
| 19 | + |
| 20 | + <div className="max-w-4xl mx-auto"> |
| 21 | + <div className="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6"> |
| 22 | + <h1 className="text-2xl font-bold text-gray-900 mb-6">New Topic</h1> |
| 23 | + <Form |
| 24 | + action="/topics" |
| 25 | + method="post" |
| 26 | + disableWhileProcessing |
| 27 | + className="inert:opacity-50 inert:pointer-events-none" |
| 28 | + > |
| 29 | + {({errors, processing}) => ( |
| 30 | + <> |
| 31 | + <div className="mb-6"> |
| 32 | + <label htmlFor="title" className="block text-sm font-medium text-gray-700 mb-2"> |
| 33 | + Topic Title |
| 34 | + </label> |
| 35 | + <input |
| 36 | + type="text" |
| 37 | + id="title" |
| 38 | + name="topic[title]" |
| 39 | + defaultValue={topic?.title} |
| 40 | + className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-sky-500 focus:border-sky-500" |
| 41 | + placeholder="Enter topic title..." |
| 42 | + /> |
| 43 | + {(errors.title) && ( |
| 44 | + <p className="mt-1 text-sm text-red-600"> |
| 45 | + Title {errors.title} |
| 46 | + </p> |
| 47 | + )} |
| 48 | + </div> |
| 49 | + |
| 50 | + <div className="mb-6"> |
| 51 | + <label htmlFor="category_id" className="block text-sm font-medium text-gray-700 mb-2"> |
| 52 | + Category |
| 53 | + </label> |
| 54 | + <select |
| 55 | + id="category_id" |
| 56 | + name="topic[category_id]" |
| 57 | + defaultValue={topic?.category_id} |
| 58 | + className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-sky-500 focus:border-sky-500" |
| 59 | + > |
| 60 | + <option value="">Select a category...</option> |
| 61 | + {categories.map((category) => ( |
| 62 | + <option key={category.id} value={category.id}> |
| 63 | + {category.name} |
| 64 | + </option> |
| 65 | + ))} |
| 66 | + </select> |
| 67 | + {(errors.category) && ( |
| 68 | + <p className="mt-1 text-sm text-red-600"> |
| 69 | + Category {errors.category} |
| 70 | + </p> |
| 71 | + )} |
| 72 | + </div> |
| 73 | + |
| 74 | + <div className="mb-6"> |
| 75 | + <label className="block text-sm font-medium text-gray-700 mb-2"> |
| 76 | + Message |
| 77 | + </label> |
| 78 | + <div className="mb-2"> |
| 79 | + <LexicalRichTextEditor |
| 80 | + name="topic[messages_attributes][0][body]" |
| 81 | + placeholder="Write your message..." |
| 82 | + /> |
| 83 | + </div> |
| 84 | + {(errors['messages[0].body']) && ( |
| 85 | + <p className="mt-1 text-sm text-red-600"> |
| 86 | + {errors['messages[0].body']} |
| 87 | + </p> |
| 88 | + )} |
| 89 | + {(errors.messages) && ( |
| 90 | + <p className="mt-1 text-sm text-red-600"> |
| 91 | + Messages {errors.messages} |
| 92 | + </p> |
| 93 | + )} |
| 94 | + </div> |
| 95 | + |
| 96 | + <div className="flex items-center justify-end gap-4"> |
| 97 | + <button |
| 98 | + type="submit" |
| 99 | + disabled={processing} |
| 100 | + className="px-6 py-3 bg-sky-600 text-white font-medium rounded-lg hover:bg-sky-700 focus:outline-none focus:ring-2 focus:ring-sky-500 focus:ring-offset-2 transition-colors duration-150 disabled:opacity-50 disabled:cursor-not-allowed" |
| 101 | + > |
| 102 | + {processing ? 'Creating Topic...' : 'Create Topic'} |
| 103 | + </button> |
| 104 | + </div> |
| 105 | + </> |
| 106 | + )} |
| 107 | + </Form> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + </AppLayout> |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +export default TopicsNew |
0 commit comments