import React, { useState, useRef } from 'react'; import { backendApi } from '../../services/apiClient'; import { Upload, Download, Loader2, FileSpreadsheet } from 'lucide-react'; type ImportType = 'districts' | 'buildings' | 'employees' | 'accounts'; const IMPORT_LABELS: Record = { districts: 'Участки', buildings: 'Дома', employees: 'Сотрудники', accounts: 'Лицевые счета', }; export const DataImportSection: React.FC = () => { const [importType, setImportType] = useState('districts'); const [file, setFile] = useState(null); const [loading, setLoading] = useState(false); const [result, setResult] = useState<{ created: number; errors: Array<{ row: number; message: string }>; total: number } | null>(null); const [error, setError] = useState(null); const fileInputRef = useRef(null); const handleDownloadTemplate = () => { const url = backendApi.getImportTemplateUrl(importType); window.open(url, '_blank'); }; const handleFileChange = (e: React.ChangeEvent) => { const f = e.target.files?.[0]; setFile(f || null); setResult(null); setError(null); }; const handleImport = async () => { if (!file) { setError('Выберите файл'); return; } setLoading(true); setError(null); setResult(null); try { const data = await backendApi.importData(importType, file); setResult(data); } catch (e: any) { setError(e?.message || 'Ошибка импорта'); } finally { setLoading(false); setFile(null); if (fileInputRef.current) { fileInputRef.current.value = ''; } } }; return (

Загрузка данных

Скачайте шаблон, заполните его и загрузите файл (CSV или XLSX). Поддерживаются: участки, дома, сотрудники, лицевые счета.

{file && ( {file.name} )}
{error && (
{error}
)} {result && (

Импорт завершён: создано/обновлено записей — {result.created}, всего строк — {result.total}.

{result.errors.length > 0 && (

Ошибки по строкам:

    {result.errors.slice(0, 20).map((e, i) => (
  • Строка {e.row}: {e.message}
  • ))} {result.errors.length > 20 && (
  • … и ещё {result.errors.length - 20}
  • )}
)}
)}
); };