From 01142fd3102e94dc057458098c83d5a3d92c572a Mon Sep 17 00:00:00 2001 From: romanskijdev Date: Fri, 25 Sep 2026 17:06:11 +0300 Subject: [PATCH] Update files input --- src/pages/OrdersPage.tsx | 73 +++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/src/pages/OrdersPage.tsx b/src/pages/OrdersPage.tsx index f3c3e15..e716c0f 100644 --- a/src/pages/OrdersPage.tsx +++ b/src/pages/OrdersPage.tsx @@ -156,6 +156,49 @@ export default function OrdersPage({ onError }: { onError: (msg: string) => void })) }, [isReadOnly]); + // Вспомогательная функция для конвертации File в Base64 + const toBase64 = (file: File) => new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = () => resolve(reader.result as string); + reader.onerror = error => reject(error); + }); + +// Обработчик мультизагрузки файлов + const handleFilesChange = async (clientName: string, event: React.ChangeEvent) => { + const files = event.target.files; + if (!files || files.length === 0) return; + + const newFiles = []; + for (let i = 0; i < files.length; i++) { + const file = files[i]; + const base64 = await toBase64(file); + // Отрезаем префикс "data:image/png;base64,", оставляя только чистый Base64 для бэкенда + const base64Data = base64.split(',')[1] || base64; + newFiles.push({ file_name: file.name, file_base64: base64Data }); + } + + setEditableOrders(prev => { + const currentOrder = prev[clientName] || {}; + const existingFiles = currentOrder.files || []; + return { + ...prev, + [clientName]: { + ...currentOrder, + files: [...existingFiles, ...newFiles] // Добавляем новые к уже существующим + } + }; + }); + + notifications.show({ + title: 'Файлы добавлены', + message: `Прикреплено новых скринов: ${newFiles.length}`, + color: 'blue' + }); + + // Очищаем value инпута, чтобы можно было выбрать те же файлы повторно при необходимости + event.target.value = ''; + }; const handleSave = async () => { if (isReadOnly) return; @@ -430,28 +473,42 @@ export default function OrdersPage({ onError }: { onError: (msg: string) => void - {/* Скрытый инпут для прикрепления файла-скрина */} + {/* Скрытый инпут для мультизагрузки файлов */} { - const file = e.target.files?.[0]; - if (file) { - notifications.show({ title: 'Файл выбран', message: `Скрин для ${clientName}: ${file.name}`, color: 'blue' }); - } - }} + disabled={inputsDisabled} + onChange={(e) => handleFilesChange(clientName, e)} /> + 0 ? "teal" : "gray"} size="sm" - title="Прикрепить скрин подтверждения заказа" + title="Прикрепить скрин(ы) подтверждения" + disabled={inputsDisabled} onClick={() => document.getElementById(`file-${clientName}`)?.click()} > + {/* Бейдж с количеством файлов (и возможностью их очистить по клику) */} + {order?.files && order.files.length > 0 && ( + !inputsDisabled && updateClientMeta(clientName, 'files', null)} + > + {order.files.length} + + )} +