|
| 1 | +// EditUser.jsx |
| 2 | +import React, { useState, useRef } from 'react'; |
| 3 | +import { useForm } from '@inertiajs/react'; |
| 4 | +import { Modal, ModalLink } from 'inertiaui/modal'; |
| 5 | + |
| 6 | +export default function EditUser({ user, roles }) { |
| 7 | + const [message, setMessage] = useState(''); |
| 8 | + const modalRef = useRef(null); |
| 9 | + |
| 10 | + const { data, setData, put, errors } = useForm({ |
| 11 | + name: user.name, |
| 12 | + email: user.email, |
| 13 | + role_id: user.role_id, |
| 14 | + }); |
| 15 | + |
| 16 | + const submit = (e) => { |
| 17 | + e.preventDefault(); |
| 18 | + put(`/users/${user.id}`, { |
| 19 | + onSuccess: () => { |
| 20 | + modalRef.current.close(); |
| 21 | + }, |
| 22 | + }); |
| 23 | + }; |
| 24 | + |
| 25 | + const onMessage = (newMessage) => { |
| 26 | + setMessage(newMessage); |
| 27 | + modalRef.current.getChildModal().emit('greeting', `Thanks from ${user.name}`); |
| 28 | + }; |
| 29 | + |
| 30 | + return ( |
| 31 | + <Modal |
| 32 | + ref={modalRef} |
| 33 | + onMessage={onMessage} |
| 34 | + > |
| 35 | + {({ close, reload, emit }) => ( |
| 36 | + <> |
| 37 | + <div> |
| 38 | + <h2 className="text-lg font-medium text-gray-900">Edit User</h2> |
| 39 | + {message && <p dusk="message" className="text-sm text-gray-500">{message}</p>} |
| 40 | + </div> |
| 41 | + |
| 42 | + <button |
| 43 | + type="button" |
| 44 | + onClick={() => emit('user-greets', 'Hello from EditUser')} |
| 45 | + className="mt-4 inline-flex items-center px-4 py-2 border border-transparent 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" |
| 46 | + > |
| 47 | + Send Message |
| 48 | + </button> |
| 49 | + |
| 50 | + <form onSubmit={submit} className="mt-8 space-y-6"> |
| 51 | + <div className="grid grid-cols-3 gap-6"> |
| 52 | + <div className="col-span-6 sm:col-span-3"> |
| 53 | + <label htmlFor="name" className="block text-sm font-medium text-gray-700">Name</label> |
| 54 | + <input |
| 55 | + value={data.name} |
| 56 | + onChange={e => setData('name', e.target.value)} |
| 57 | + type="text" |
| 58 | + id="name" |
| 59 | + name="name" |
| 60 | + autoComplete="off" |
| 61 | + className="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" |
| 62 | + /> |
| 63 | + {errors.name && <p className="mt-2 text-sm text-red-600">{errors.name}</p>} |
| 64 | + </div> |
| 65 | + |
| 66 | + <div className="col-span-6 sm:col-span-3"> |
| 67 | + <label htmlFor="email" className="block text-sm font-medium text-gray-700">Email</label> |
| 68 | + <input |
| 69 | + value={data.email} |
| 70 | + onChange={e => setData('email', e.target.value)} |
| 71 | + type="email" |
| 72 | + id="email" |
| 73 | + name="email" |
| 74 | + autoComplete="off" |
| 75 | + className="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md" |
| 76 | + /> |
| 77 | + {errors.email && <p className="mt-2 text-sm text-red-600">{errors.email}</p>} |
| 78 | + </div> |
| 79 | + |
| 80 | + <div className="col-span-6 sm:col-span-3"> |
| 81 | + <label htmlFor="role" className="block text-sm font-medium text-gray-700">Role</label> |
| 82 | + <select |
| 83 | + value={data.role_id} |
| 84 | + onChange={e => setData('role_id', e.target.value)} |
| 85 | + id="role" |
| 86 | + name="role" |
| 87 | + autoComplete="off" |
| 88 | + className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" |
| 89 | + > |
| 90 | + {Object.entries(roles).map(([id, role]) => ( |
| 91 | + <option key={id} value={id}>{role}</option> |
| 92 | + ))} |
| 93 | + </select> |
| 94 | + |
| 95 | + <ModalLink |
| 96 | + fragment="add-role" |
| 97 | + onClose={() => reload({ only: ['roles'] })} |
| 98 | + href="/roles/create" |
| 99 | + className="mt-2 text-sm text-indigo-600 hover:text-indigo-500 bg-transparent border border-indigo-500 rounded-md py-1 px-2 inline-flex items-center" |
| 100 | + > |
| 101 | + <svg className="h-4 w-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> |
| 102 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path> |
| 103 | + </svg> |
| 104 | + Add Role |
| 105 | + </ModalLink> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + |
| 109 | + <div className="flex justify-end"> |
| 110 | + <button |
| 111 | + type="button" |
| 112 | + onClick={close} |
| 113 | + className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-gray-600 hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500" |
| 114 | + > |
| 115 | + Cancel |
| 116 | + </button> |
| 117 | + <button |
| 118 | + type="submit" |
| 119 | + className="ml-3 inline-flex items-center px-4 py-2 border border-transparent 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" |
| 120 | + > |
| 121 | + Save |
| 122 | + </button> |
| 123 | + </div> |
| 124 | + </form> |
| 125 | + </> |
| 126 | + )} |
| 127 | + </Modal> |
| 128 | + ); |
| 129 | +}; |
0 commit comments