erp-platform/ui/src/views/crm/components/CustomerFormNew.tsx

519 lines
20 KiB
TypeScript
Raw Normal View History

2025-09-15 14:13:20 +00:00
import React, { useState, useEffect, useCallback } from 'react'
import { useNavigate, useParams } from 'react-router-dom'
2025-09-15 09:31:47 +00:00
import {
FaSave,
FaTimes,
FaBuilding,
FaUser,
FaMapMarkerAlt,
FaCreditCard,
FaEnvelope,
2025-09-15 14:13:20 +00:00
} from 'react-icons/fa'
import LoadingSpinner from '../../../components/common/LoadingSpinner'
2025-09-17 09:46:58 +00:00
import {
BusinessParty,
BusinessPartyIndustryEnum,
BusinessPartyStatusEnum,
PaymentTerms,
} from '../../../types/common'
2025-09-15 14:13:20 +00:00
import { mockBusinessPartyNew } from '../../../mocks/mockBusinessParties'
import { Container } from '@/components/shared'
2025-09-16 12:33:57 +00:00
import { ROUTES_ENUM } from '@/routes/route.constant'
2025-09-17 09:46:58 +00:00
import { mockCurrencies } from '@/mocks/mockCurrencies'
import { CustomerSegmentEnum, CustomerTypeEnum } from '@/types/crm'
import {
getBusinessPartyIndustryText,
getBusinessPartyStatusText,
getCustomerSegmentText,
getCustomerTypeText,
getPaymentTermsText,
} from '@/utils/erp'
2025-09-15 09:31:47 +00:00
interface ValidationErrors {
2025-09-15 14:13:20 +00:00
[key: string]: string
2025-09-15 09:31:47 +00:00
}
const CustomerForm: React.FC = () => {
2025-09-15 14:13:20 +00:00
const navigate = useNavigate()
const { id } = useParams<{ id: string }>()
const isEdit = Boolean(id)
2025-09-15 09:31:47 +00:00
2025-09-15 14:13:20 +00:00
const [loading, setLoading] = useState(false)
const [saving, setSaving] = useState(false)
const [errors, setErrors] = useState<ValidationErrors>({})
2025-09-15 09:31:47 +00:00
2025-09-15 14:13:20 +00:00
const [formData, setFormData] = useState<BusinessParty>(mockBusinessPartyNew)
2025-09-15 09:31:47 +00:00
const loadFormData = useCallback(async () => {
2025-09-15 14:13:20 +00:00
setLoading(true)
2025-09-15 09:31:47 +00:00
try {
if (isEdit && id) {
// Simulate API call
2025-09-15 14:13:20 +00:00
await new Promise((resolve) => setTimeout(resolve, 1000))
2025-09-15 09:31:47 +00:00
2025-09-15 14:13:20 +00:00
setFormData(mockBusinessPartyNew)
2025-09-15 09:31:47 +00:00
}
} catch (error) {
2025-09-15 14:13:20 +00:00
console.error('Error loading form data:', error)
2025-09-15 09:31:47 +00:00
} finally {
2025-09-15 14:13:20 +00:00
setLoading(false)
2025-09-15 09:31:47 +00:00
}
2025-09-15 14:13:20 +00:00
}, [isEdit, id])
2025-09-15 09:31:47 +00:00
useEffect(() => {
2025-09-15 14:13:20 +00:00
loadFormData()
}, [loadFormData])
2025-09-15 09:31:47 +00:00
const validateForm = (): boolean => {
2025-09-15 14:13:20 +00:00
const newErrors: ValidationErrors = {}
2025-09-15 09:31:47 +00:00
if (!formData.code.trim()) {
2025-09-15 14:13:20 +00:00
newErrors.code = 'Müşteri kodu zorunludur'
2025-09-15 09:31:47 +00:00
}
if (!formData.name.trim()) {
2025-09-15 14:13:20 +00:00
newErrors.name = 'Şirket adı zorunludur'
2025-09-15 09:31:47 +00:00
}
if (formData.creditLimit < 0) {
2025-09-15 14:13:20 +00:00
newErrors.creditLimit = "Kredi limiti 0'dan küçük olamaz"
2025-09-15 09:31:47 +00:00
}
2025-09-15 14:13:20 +00:00
setErrors(newErrors)
return Object.keys(newErrors).length === 0
}
2025-09-15 09:31:47 +00:00
2025-09-15 14:13:20 +00:00
const handleInputChange = (field: keyof BusinessParty, value: string | number | boolean) => {
2025-09-15 09:31:47 +00:00
setFormData((prev) => ({
...prev,
[field]: value,
2025-09-15 14:13:20 +00:00
}))
2025-09-15 09:31:47 +00:00
// Clear error when user starts typing
if (errors[field]) {
setErrors((prev) => ({
...prev,
2025-09-15 14:13:20 +00:00
[field]: '',
}))
2025-09-15 09:31:47 +00:00
}
2025-09-15 14:13:20 +00:00
}
2025-09-15 09:31:47 +00:00
const handleSubmit = async (e: React.FormEvent) => {
2025-09-15 14:13:20 +00:00
e.preventDefault()
2025-09-15 09:31:47 +00:00
if (!validateForm()) {
2025-09-15 14:13:20 +00:00
return
2025-09-15 09:31:47 +00:00
}
2025-09-15 14:13:20 +00:00
setSaving(true)
2025-09-15 09:31:47 +00:00
try {
// Simulate API call
2025-09-15 14:13:20 +00:00
await new Promise((resolve) => setTimeout(resolve, 2000))
2025-09-15 09:31:47 +00:00
2025-09-15 14:13:20 +00:00
console.log('Customer data:', {
2025-09-15 09:31:47 +00:00
...formData,
id: isEdit ? id : undefined,
2025-09-15 14:13:20 +00:00
})
2025-09-15 09:31:47 +00:00
// Show success message
2025-09-15 14:13:20 +00:00
alert(isEdit ? 'Müşteri başarıyla güncellendi!' : 'Müşteri başarıyla oluşturuldu!')
2025-09-15 09:31:47 +00:00
// Navigate back to list
2025-09-16 12:33:57 +00:00
navigate(ROUTES_ENUM.protected.crm.customers)
2025-09-15 09:31:47 +00:00
} catch (error) {
2025-09-15 14:13:20 +00:00
console.error('Error saving customer:', error)
alert('Bir hata oluştu. Lütfen tekrar deneyin.')
2025-09-15 09:31:47 +00:00
} finally {
2025-09-15 14:13:20 +00:00
setSaving(false)
2025-09-15 09:31:47 +00:00
}
2025-09-15 14:13:20 +00:00
}
2025-09-15 09:31:47 +00:00
const handleCancel = () => {
2025-09-16 12:33:57 +00:00
navigate(ROUTES_ENUM.protected.crm.customers)
2025-09-15 14:13:20 +00:00
}
2025-09-15 09:31:47 +00:00
if (loading) {
2025-09-15 14:13:20 +00:00
return <LoadingSpinner />
2025-09-15 09:31:47 +00:00
}
return (
2025-09-15 14:13:20 +00:00
<Container>
2025-09-15 19:22:43 +00:00
<div className="space-y-2">
2025-09-15 14:13:20 +00:00
{/* Header */}
<div className="flex items-center justify-between">
<div>
<h2 className="text-lg font-semibold text-gray-900">
{isEdit ? 'Müşteri Düzenle' : 'Yeni Müşteri'}
</h2>
2025-09-15 21:02:48 +00:00
<p className="text-gray-600">
2025-09-15 14:13:20 +00:00
{isEdit ? 'Mevcut müşteri bilgilerini güncelleyin' : 'Yeni müşteri bilgilerini girin'}
</p>
</div>
2025-09-15 09:31:47 +00:00
</div>
2025-09-15 14:13:20 +00:00
{/* Form */}
<form onSubmit={handleSubmit} className="space-y-3">
{/* General Information */}
<div className="bg-white shadow rounded-lg">
<div className="px-4 py-3 border-b border-gray-200">
<h3 className="text-base font-medium text-gray-900 flex items-center">
<FaBuilding className="w-5 h-5 mr-2" />
Genel Bilgiler
</h3>
</div>
2025-09-15 09:31:47 +00:00
2025-09-15 14:13:20 +00:00
<div className="p-4 space-y-3">
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Müşteri Kodu *
</label>
<input
type="text"
value={formData.code}
onChange={(e) => handleInputChange('code', e.target.value)}
className={`block w-full px-3 py-1.5 text-sm border rounded-md shadow-sm focus:outline-none focus:ring-1 ${
errors.code
? 'border-red-300 focus:border-red-500 focus:ring-red-500'
: 'border-gray-300 focus:border-blue-500 focus:ring-blue-500'
}`}
placeholder="Örn: MST001"
/>
{errors.customerCode && (
<p className="mt-1 text-sm text-red-600">{errors.customerCode}</p>
)}
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Müşteri Tipi
</label>
<select
value={formData.customerType}
2025-09-17 09:46:58 +00:00
onChange={(e) => handleInputChange('customerType', e.target.value)}
2025-09-15 14:13:20 +00:00
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
>
2025-09-17 09:46:58 +00:00
{Object.values(CustomerTypeEnum).map((type) => (
<option key={type} value={type}>
{getCustomerTypeText(type)}
</option>
))}
2025-09-15 14:13:20 +00:00
</select>
</div>
2025-09-15 09:31:47 +00:00
</div>
2025-09-15 14:13:20 +00:00
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Şirket Adı *
</label>
<input
type="text"
value={formData.name}
onChange={(e) => handleInputChange('name', e.target.value)}
className={`block w-full px-3 py-1.5 text-sm border rounded-md shadow-sm focus:outline-none focus:ring-1 ${
errors.name
? 'border-red-300 focus:border-red-500 focus:ring-red-500'
: 'border-gray-300 focus:border-blue-500 focus:ring-blue-500'
}`}
placeholder="Şirket adını girin"
/>
{errors.companyName && (
<p className="mt-1 text-sm text-red-600">{errors.companyName}</p>
)}
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Sektör</label>
<select
value={formData.industry}
onChange={(e) => handleInputChange('industry', e.target.value)}
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
>
<option value="">Sektör seçin</option>
2025-09-17 09:46:58 +00:00
{Object.values(BusinessPartyIndustryEnum).map((industry) => (
<option key={industry} value={industry}>
{getBusinessPartyIndustryText(industry)}
</option>
))}
2025-09-15 14:13:20 +00:00
</select>
</div>
2025-09-15 09:31:47 +00:00
</div>
<div>
2025-09-15 14:13:20 +00:00
<label className="block text-xs font-medium text-gray-700 mb-1">Website</label>
2025-09-15 09:31:47 +00:00
<input
2025-09-15 14:13:20 +00:00
type="url"
value={formData.website}
onChange={(e) => handleInputChange('website', e.target.value)}
2025-09-15 09:31:47 +00:00
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
2025-09-15 14:13:20 +00:00
placeholder="https://www.ornek.com"
/>
2025-09-15 09:31:47 +00:00
</div>
</div>
</div>
2025-09-15 14:13:20 +00:00
{/* Contact Information */}
<div className="bg-white shadow rounded-lg">
<div className="px-4 py-3 border-b border-gray-200">
<h3 className="text-base font-medium text-gray-900 flex items-center">
<FaUser className="w-5 h-5 mr-2" />
İletişim Bilgileri
</h3>
</div>
2025-09-15 09:31:47 +00:00
2025-09-15 14:13:20 +00:00
<div className="p-4 space-y-3">
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Email *</label>
<input
type="email"
value={formData.email}
onChange={(e) => handleInputChange('email', e.target.value)}
className={`block w-full px-3 py-1.5 text-sm border rounded-md shadow-sm focus:outline-none focus:ring-1 ${
errors.email
? 'border-red-300 focus:border-red-500 focus:ring-red-500'
: 'border-gray-300 focus:border-blue-500 focus:ring-blue-500'
}`}
placeholder="email@ornek.com"
/>
{errors.email && <p className="mt-1 text-sm text-red-600">{errors.email}</p>}
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">Telefon *</label>
<input
type="tel"
value={formData.phone}
onChange={(e) => handleInputChange('phone', e.target.value)}
className={`block w-full px-3 py-1.5 text-sm border rounded-md shadow-sm focus:outline-none focus:ring-1 ${
errors.phone
? 'border-red-300 focus:border-red-500 focus:ring-red-500'
: 'border-gray-300 focus:border-blue-500 focus:ring-blue-500'
}`}
placeholder="+90 212 555 0123"
/>
{errors.phone && <p className="mt-1 text-sm text-red-600">{errors.phone}</p>}
</div>
2025-09-15 09:31:47 +00:00
</div>
</div>
</div>
2025-09-15 14:13:20 +00:00
{/* Address Information */}
<div className="bg-white shadow rounded-lg">
<div className="px-4 py-3 border-b border-gray-200">
<h3 className="text-base font-medium text-gray-900 flex items-center">
<FaMapMarkerAlt className="w-5 h-5 mr-2" />
Adres Bilgileri
</h3>
</div>
2025-09-15 09:31:47 +00:00
</div>
2025-09-15 14:13:20 +00:00
{/* Business Information */}
<div className="bg-white shadow rounded-lg">
<div className="px-4 py-3 border-b border-gray-200">
<h3 className="text-base font-medium text-gray-900 flex items-center">
<FaCreditCard className="w-5 h-5 mr-2" />
İş Bilgileri
</h3>
2025-09-15 09:31:47 +00:00
</div>
2025-09-15 14:13:20 +00:00
<div className="p-4 space-y-3">
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Vergi Numarası
</label>
<input
type="text"
value={formData.taxNumber}
onChange={(e) => handleInputChange('taxNumber', e.target.value)}
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
placeholder="1234567890"
/>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Ticaret Sicil No
</label>
<input
type="text"
value={formData.registrationNumber}
onChange={(e) => handleInputChange('registrationNumber', e.target.value)}
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
placeholder="98765"
/>
</div>
2025-09-15 09:31:47 +00:00
</div>
2025-09-15 14:13:20 +00:00
<div className="grid grid-cols-1 md:grid-cols-3 gap-3">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Kredi Limiti
</label>
<input
type="number"
min="0"
step="0.01"
value={formData.creditLimit}
onChange={(e) =>
handleInputChange('creditLimit', parseFloat(e.target.value) || 0)
}
className={`block w-full px-3 py-1.5 text-sm border rounded-md shadow-sm focus:outline-none focus:ring-1 ${
errors.creditLimit
? 'border-red-300 focus:border-red-500 focus:ring-red-500'
: 'border-gray-300 focus:border-blue-500 focus:ring-blue-500'
}`}
placeholder="0.00"
/>
{errors.creditLimit && (
<p className="mt-1 text-sm text-red-600">{errors.creditLimit}</p>
)}
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Ödeme Koşulları
</label>
<select
value={formData.paymentTerms}
onChange={(e) => handleInputChange('paymentTerms', e.target.value)}
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
>
2025-09-17 09:46:58 +00:00
{Object.values(PaymentTerms).map((term) => (
<option key={term} value={term}>
{getPaymentTermsText(term)}
</option>
))}
2025-09-15 14:13:20 +00:00
</select>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Para Birimi
</label>
<select
value={formData.currency}
onChange={(e) => handleInputChange('currency', e.target.value)}
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
>
2025-09-17 09:46:58 +00:00
{mockCurrencies.map((currency) => (
<option key={currency.value} value={currency.value}>
{currency.value} - {currency.label}
</option>
))}
2025-09-15 14:13:20 +00:00
</select>
</div>
2025-09-15 09:31:47 +00:00
</div>
</div>
</div>
2025-09-15 14:13:20 +00:00
{/* Relationship Information */}
<div className="bg-white shadow rounded-lg">
<div className="px-4 py-3 border-b border-gray-200">
<h3 className="text-base font-medium text-gray-900 flex items-center">
<FaEnvelope className="w-5 h-5 mr-2" />
İlişki Yönetimi
</h3>
</div>
2025-09-15 09:31:47 +00:00
2025-09-15 14:13:20 +00:00
<div className="p-4 space-y-3">
<div className="grid grid-cols-1 md:grid-cols-3 gap-3">
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Müşteri Durumu
</label>
<select
value={formData.status}
onChange={(e) => handleInputChange('status', e.target.value)}
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
>
2025-09-17 09:46:58 +00:00
{Object.values(BusinessPartyStatusEnum).map((status) => (
<option key={status} value={status}>
{getBusinessPartyStatusText(status)}
</option>
))}
2025-09-15 14:13:20 +00:00
</select>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Müşteri Segmenti
</label>
<select
value={formData.customerSegment}
onChange={(e) => handleInputChange('customerSegment', e.target.value)}
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
>
2025-09-17 09:46:58 +00:00
{Object.values(CustomerSegmentEnum).map((segment) => (
<option key={segment} value={segment}>
{getCustomerSegmentText(segment)}
</option>
))}
2025-09-15 14:13:20 +00:00
</select>
</div>
<div>
<label className="block text-xs font-medium text-gray-700 mb-1">
Sorumlu Satış Temsilcisi
</label>
2025-09-17 09:46:58 +00:00
<input
type="text"
2025-09-15 14:13:20 +00:00
value={formData.assignedSalesRep}
onChange={(e) => handleInputChange('assignedSalesRep', e.target.value)}
className="block w-full px-3 py-1.5 text-sm border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-1 focus:border-blue-500 focus:ring-blue-500"
2025-09-17 09:46:58 +00:00
placeholder="Satış temsilcisi adı"
/>
2025-09-15 14:13:20 +00:00
</div>
2025-09-15 09:31:47 +00:00
</div>
2025-09-15 14:13:20 +00:00
<div className="flex items-center">
<input
type="checkbox"
id="isActive"
checked={formData.isActive}
onChange={(e) => handleInputChange('isActive', e.target.checked)}
className="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded"
/>
<label htmlFor="isActive" className="ml-2 block text-sm text-gray-900">
Aktif
2025-09-15 09:31:47 +00:00
</label>
</div>
</div>
</div>
2025-09-15 14:13:20 +00:00
{/* Form Actions */}
<div className="flex items-center justify-end space-x-2 bg-gray-50 px-4 py-3 rounded-b-lg">
<button
type="button"
onClick={handleCancel}
className="inline-flex items-center px-4 py-1.5 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
>
<FaTimes className="w-4 h-4 mr-2" />
İptal
</button>
<button
type="submit"
disabled={saving}
className="inline-flex items-center px-4 py-1.5 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed"
>
{saving ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2"></div>
Kaydediliyor...
</>
) : (
<>
<FaSave className="w-4 h-4 mr-2" />
{isEdit ? 'Güncelle' : 'Kaydet'}
</>
)}
</button>
</div>
</form>
</div>
</Container>
)
}
2025-09-15 09:31:47 +00:00
2025-09-15 14:13:20 +00:00
export default CustomerForm