feat: Implement company management plan

This commit is contained in:
gpt-engineer-app[bot]
2025-09-30 03:01:53 +00:00
parent 93f86fc503
commit 556d1c5a97
11 changed files with 1938 additions and 9 deletions

View File

@@ -0,0 +1,207 @@
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import * as z from 'zod';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Label } from '@/components/ui/label';
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Building2, Save, X } from 'lucide-react';
import { Combobox } from '@/components/ui/combobox';
import { useCompanyHeadquarters } from '@/hooks/useAutocompleteData';
const propertyOwnerSchema = z.object({
name: z.string().min(1, 'Name is required'),
slug: z.string().min(1, 'Slug is required'),
description: z.string().optional(),
person_type: z.enum(['company', 'individual', 'firm', 'organization']),
website_url: z.string().url().optional().or(z.literal('')),
founded_year: z.number().min(1800).max(new Date().getFullYear()).optional(),
headquarters_location: z.string().optional()
});
type PropertyOwnerFormData = z.infer<typeof propertyOwnerSchema>;
interface PropertyOwnerFormProps {
onSubmit: (data: PropertyOwnerFormData) => void;
onCancel: () => void;
initialData?: Partial<PropertyOwnerFormData>;
}
export function PropertyOwnerForm({ onSubmit, onCancel, initialData }: PropertyOwnerFormProps) {
const { headquarters } = useCompanyHeadquarters();
const {
register,
handleSubmit,
setValue,
watch,
formState: { errors }
} = useForm<PropertyOwnerFormData>({
resolver: zodResolver(propertyOwnerSchema),
defaultValues: {
name: initialData?.name || '',
slug: initialData?.slug || '',
description: initialData?.description || '',
person_type: initialData?.person_type || 'company',
website_url: initialData?.website_url || '',
founded_year: initialData?.founded_year || undefined,
headquarters_location: initialData?.headquarters_location || ''
}
});
const generateSlug = (name: string) => {
return name
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.trim();
};
const handleNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const name = e.target.value;
const slug = generateSlug(name);
setValue('slug', slug);
};
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Building2 className="w-5 h-5" />
{initialData ? 'Edit Property Owner' : 'Create New Property Owner'}
</CardTitle>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-6">
{/* Basic Information */}
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<div className="space-y-2">
<Label htmlFor="name">Name *</Label>
<Input
id="name"
{...register('name')}
onChange={(e) => {
register('name').onChange(e);
handleNameChange(e);
}}
placeholder="Enter property owner name"
/>
{errors.name && (
<p className="text-sm text-destructive">{errors.name.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="slug">URL Slug *</Label>
<Input
id="slug"
{...register('slug')}
placeholder="property-owner-slug"
/>
{errors.slug && (
<p className="text-sm text-destructive">{errors.slug.message}</p>
)}
</div>
</div>
{/* Description */}
<div className="space-y-2">
<Label htmlFor="description">Description</Label>
<Textarea
id="description"
{...register('description')}
placeholder="Describe the property owner..."
rows={3}
/>
</div>
{/* Person Type */}
<div className="space-y-2">
<Label>Entity Type *</Label>
<RadioGroup
value={watch('person_type')}
onValueChange={(value) => setValue('person_type', value as any)}
className="grid grid-cols-2 md:grid-cols-4 gap-4"
>
<div className="flex items-center space-x-2">
<RadioGroupItem value="company" id="company" />
<Label htmlFor="company" className="cursor-pointer">Company</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="individual" id="individual" />
<Label htmlFor="individual" className="cursor-pointer">Individual</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="firm" id="firm" />
<Label htmlFor="firm" className="cursor-pointer">Firm</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="organization" id="organization" />
<Label htmlFor="organization" className="cursor-pointer">Organization</Label>
</div>
</RadioGroup>
</div>
{/* Additional Details */}
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<div className="space-y-2">
<Label htmlFor="founded_year">Founded Year</Label>
<Input
id="founded_year"
type="number"
min="1800"
max={new Date().getFullYear()}
{...register('founded_year', { valueAsNumber: true })}
placeholder="e.g. 1972"
/>
{errors.founded_year && (
<p className="text-sm text-destructive">{errors.founded_year.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="headquarters_location">Headquarters Location</Label>
<Combobox
options={headquarters}
value={watch('headquarters_location')}
onValueChange={(value) => setValue('headquarters_location', value)}
placeholder="Select or type location"
searchPlaceholder="Search locations..."
emptyText="No locations found"
/>
</div>
</div>
{/* Website */}
<div className="space-y-2">
<Label htmlFor="website_url">Website URL</Label>
<Input
id="website_url"
type="url"
{...register('website_url')}
placeholder="https://example.com"
/>
{errors.website_url && (
<p className="text-sm text-destructive">{errors.website_url.message}</p>
)}
</div>
{/* Actions */}
<div className="flex gap-3 justify-end">
<Button type="button" variant="outline" onClick={onCancel}>
<X className="w-4 h-4 mr-2" />
Cancel
</Button>
<Button type="submit">
<Save className="w-4 h-4 mr-2" />
Save Property Owner
</Button>
</div>
</form>
</CardContent>
</Card>
);
}