import { Button } from '@/components/ui'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/Card'; import Checkbox from '@/components/ui/Checkbox'; import ImageUpload from '@/components/ui/ImageUpload'; import Input from '@/components/ui/Input'; import { Stack } from '@/components/ui/layout'; import MapPicker from '@/components/ui/MapPicker'; import Select from '@/components/ui/Select'; import Textarea from '@/components/ui/Textarea'; import { useCreateOrganization, useUpdateOrganization } from '@/hooks/api/useOrganizationsAPI.ts'; import { useTranslation } from '@/hooks/useI18n.tsx'; import { useOrganizations } from '@/hooks/useOrganizations.ts'; import { useToast } from '@/hooks/useToast.ts'; import { Organization } from '@/types.ts'; import { zodResolver } from '@hookform/resolvers/zod'; import { ArrowLeft, Save } from 'lucide-react'; import { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { useNavigate, useParams } from 'react-router-dom'; import { z } from 'zod'; // Form validation schema const organizationSchema = z.object({ name: z.string().min(1, 'Name is required'), sector: z.string().min(1, 'Sector is required'), subtype: z.string().optional(), description: z.string().optional(), website: z.string().url().optional().or(z.literal('')), address: z .object({ street: z.string().optional(), city: z.string().optional(), state: z.string().optional(), zip: z.string().optional(), country: z.string().optional(), }) .optional(), coordinates: z .object({ lat: z.number(), lng: z.number(), }) .optional(), needs: z.array(z.string()).optional(), offers: z.array(z.string()).optional(), logo: z.string().optional(), verified: z.boolean().optional(), }); type OrganizationFormData = z.infer; const AdminOrganizationEditPage = () => { const { t } = useTranslation(); const navigate = useNavigate(); const { id } = useParams<{ id: string }>(); const isEditing = !!id; const { organizations } = useOrganizations(); const { success, error: showError } = useToast(); const createOrganization = useCreateOrganization(); const updateOrganization = useUpdateOrganization(); const [currentOrg, setCurrentOrg] = useState(null); const [isLoading, setIsLoading] = useState(false); const form = useForm({ resolver: zodResolver(organizationSchema), defaultValues: { name: '', sector: '', subtype: '', description: '', website: '', address: { street: '', city: '', state: '', zip: '', country: '', }, coordinates: undefined, needs: [], offers: [], logo: '', verified: false, }, }); // Load organization data for editing useEffect(() => { if (isEditing && organizations.length > 0) { const org = organizations.find((o) => o.ID === id); if (org) { setCurrentOrg(org); form.reset({ name: org.Name || '', sector: org.Sector || '', subtype: org.Subtype || '', description: org.Description || '', website: org.Website || '', address: { street: org.Address?.split(',')[0]?.trim() || '', city: org.Address?.split(',')[1]?.trim() || '', state: org.Address?.split(',')[2]?.split(' ')[0]?.trim() || '', zip: org.Address?.split(',')[2]?.split(' ')[1]?.trim() || '', country: 'Russia', // Default }, coordinates: org.Coordinates ? { lat: org.Coordinates.lat, lng: org.Coordinates.lng, } : undefined, needs: org.Needs || [], offers: org.Offers || [], logo: org.Logo || '', verified: org.Verified || false, }); } } }, [id, isEditing, organizations, form]); const onSubmit = async (data: OrganizationFormData) => { setIsLoading(true); try { const payload = { name: data.name, sector: data.sector, subtype: data.subtype, description: data.description, website: data.website, address: data.address ? `${data.address.street}, ${data.address.city}, ${data.address.state} ${data.address.zip}`.trim() : '', coordinates: data.coordinates, needs: data.needs, offers: data.offers, logo: data.logo, verified: data.verified, }; if (isEditing && currentOrg) { await updateOrganization.mutateAsync({ id: currentOrg.ID, data: payload, }); success('Organization updated successfully'); } else { await createOrganization.mutateAsync(payload); success('Organization created successfully'); } navigate('/admin/organizations'); } catch (err) { console.error('Error saving organization:', err); showError('Failed to save organization', { title: 'An error occurred while saving the organization', }); } finally { setIsLoading(false); } }; const sectors = [ { value: 'technology', label: 'Technology' }, { value: 'manufacturing', label: 'Manufacturing' }, { value: 'agriculture', label: 'Agriculture' }, { value: 'energy', label: 'Energy' }, { value: 'construction', label: 'Construction' }, { value: 'transportation', label: 'Transportation' }, { value: 'healthcare', label: 'Healthcare' }, { value: 'education', label: 'Education' }, { value: 'finance', label: 'Finance' }, { value: 'retail', label: 'Retail' }, { value: 'hospitality', label: 'Hospitality' }, { value: 'other', label: 'Other' }, ]; const subtypes = [ { value: 'company', label: 'Company' }, { value: 'nonprofit', label: 'Non-profit Organization' }, { value: 'government', label: 'Government Agency' }, { value: 'educational', label: 'Educational Institution' }, { value: 'research', label: 'Research Institution' }, ]; const commonResources = [ 'Raw materials', 'Energy', 'Water', 'Waste management', 'Transportation', 'Equipment', 'Technology services', 'Consulting', 'Training', 'Financial services', ]; return (
{/* Header */}

{isEditing ? 'Edit Organization' : 'Create New Organization'}

{isEditing ? 'Update organization details' : 'Add a new organization to the ecosystem'}

{/* Basic Information */} Basic Information
form.setValue('subtype', value)} options={subtypes} placeholder="Select organization type" />