|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState } from 'react' |
| 4 | +import { Organization } from '@prisma/client' |
| 5 | +import { updateOrganization } from '@/app/organizations/organizationActions' |
| 6 | + |
| 7 | +interface OrganizationInfoProps { |
| 8 | + organization: Organization |
| 9 | + isOwner: boolean |
| 10 | +} |
| 11 | + |
| 12 | +export default function OrganizationInfo({ organization, isOwner }: OrganizationInfoProps) { |
| 13 | + const [isEditing, setIsEditing] = useState(false) |
| 14 | + const [editedOrg, setEditedOrg] = useState(organization) |
| 15 | + |
| 16 | + const handleEdit = async (e: React.FormEvent) => { |
| 17 | + e.preventDefault() |
| 18 | + try { |
| 19 | + const updatedOrg = await updateOrganization(organization.id, editedOrg) |
| 20 | + setEditedOrg(updatedOrg) |
| 21 | + setIsEditing(false) |
| 22 | + } catch (error) { |
| 23 | + console.error('Error updating organization:', error) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { |
| 28 | + const { name, value } = e.target |
| 29 | + setEditedOrg((prev) => ({ ...prev, [name]: value })) |
| 30 | + } |
| 31 | + |
| 32 | + if (isEditing) { |
| 33 | + return ( |
| 34 | + <form onSubmit={handleEdit} className="space-y-4"> |
| 35 | + <div> |
| 36 | + <label htmlFor="name" className="block text-sm font-medium text-gray-700">Name</label> |
| 37 | + <input |
| 38 | + type="text" |
| 39 | + id="name" |
| 40 | + name="name" |
| 41 | + value={editedOrg.name} |
| 42 | + onChange={handleInputChange} |
| 43 | + className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" |
| 44 | + /> |
| 45 | + </div> |
| 46 | + <div> |
| 47 | + <label htmlFor="description" className="block text-sm font-medium text-gray-700">Description</label> |
| 48 | + <textarea |
| 49 | + id="description" |
| 50 | + name="description" |
| 51 | + value={editedOrg.description || ''} |
| 52 | + onChange={handleInputChange} |
| 53 | + rows={3} |
| 54 | + className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" |
| 55 | + ></textarea> |
| 56 | + </div> |
| 57 | + <div> |
| 58 | + <label htmlFor="email" className="block text-sm font-medium text-gray-700">Email</label> |
| 59 | + <input |
| 60 | + type="email" |
| 61 | + id="email" |
| 62 | + name="email" |
| 63 | + value={editedOrg.email || ''} |
| 64 | + onChange={handleInputChange} |
| 65 | + className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" |
| 66 | + /> |
| 67 | + </div> |
| 68 | + <div> |
| 69 | + <label htmlFor="telephone" className="block text-sm font-medium text-gray-700">Telephone</label> |
| 70 | + <input |
| 71 | + type="tel" |
| 72 | + id="telephone" |
| 73 | + name="telephone" |
| 74 | + value={editedOrg.telephone || ''} |
| 75 | + onChange={handleInputChange} |
| 76 | + className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" |
| 77 | + /> |
| 78 | + </div> |
| 79 | + <button type="submit" 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"> |
| 80 | + Save |
| 81 | + </button> |
| 82 | + </form> |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className="space-y-4"> |
| 88 | + <p className="text-gray-700">{organization.description}</p> |
| 89 | + <p className="text-gray-700">Email: {organization.email}</p> |
| 90 | + <p className="text-gray-700">Phone: {organization.telephone}</p> |
| 91 | + {isOwner && ( |
| 92 | + <button |
| 93 | + onClick={() => setIsEditing(true)} |
| 94 | + 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" |
| 95 | + > |
| 96 | + Edit Organization |
| 97 | + </button> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + ) |
| 101 | +} |
0 commit comments