mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2026-03-13 02:18:16 +01:00
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
import { useTranslation } from 'react-i18next';
|
|
import { useToolOperation, ToolOperationConfig, ToolType } from '../shared/useToolOperation';
|
|
import { createStandardErrorHandler } from '../../../utils/toolErrorHandler';
|
|
import { AddAttachmentsParameters } from './useAddAttachmentsParameters';
|
|
|
|
const buildFormData = (parameters: AddAttachmentsParameters, file: File): FormData => {
|
|
const formData = new FormData();
|
|
|
|
// Add the main PDF file (single file per request in singleFile mode)
|
|
if (file) {
|
|
formData.append("fileInput", file);
|
|
}
|
|
|
|
// Add attachment files
|
|
(parameters.attachments || []).forEach((attachment) => {
|
|
if (attachment) formData.append("attachments", attachment);
|
|
});
|
|
|
|
return formData;
|
|
};
|
|
|
|
// Operation configuration for automation
|
|
export const addAttachmentsOperationConfig: ToolOperationConfig<AddAttachmentsParameters> = {
|
|
toolType: ToolType.singleFile,
|
|
buildFormData,
|
|
operationType: 'addAttachments',
|
|
endpoint: '/api/v1/misc/add-attachments',
|
|
};
|
|
|
|
export const useAddAttachmentsOperation = () => {
|
|
const { t } = useTranslation();
|
|
|
|
return useToolOperation<AddAttachmentsParameters>({
|
|
...addAttachmentsOperationConfig,
|
|
getErrorMessage: createStandardErrorHandler(t('addAttachments.error.failed', 'An error occurred while adding attachments to the PDF.'))
|
|
});
|
|
};
|