mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-14 18:44:05 +02:00
# Description of Changes Redesigns `ToolOperationConfig` so that the types of the functions are always known depending on whether the tool runs on single files, multiple files, or uses custom behaviour
45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { useTranslation } from 'react-i18next';
|
|
import { ToolType, useToolOperation } from '../shared/useToolOperation';
|
|
import { createStandardErrorHandler } from '../../../utils/toolErrorHandler';
|
|
import { ChangePermissionsParameters, defaultParameters } from './useChangePermissionsParameters';
|
|
|
|
export const getFormData = ((parameters: ChangePermissionsParameters) =>
|
|
Object.entries(parameters).map(([key, value]) =>
|
|
[key, value.toString()]
|
|
) as string[][]
|
|
);
|
|
|
|
// Static function that can be used by both the hook and automation executor
|
|
export const buildChangePermissionsFormData = (parameters: ChangePermissionsParameters, file: File): FormData => {
|
|
const formData = new FormData();
|
|
formData.append("fileInput", file);
|
|
|
|
// Add all permission parameters
|
|
getFormData(parameters).forEach(([key, value]) => {
|
|
formData.append(key, value);
|
|
});
|
|
|
|
return formData;
|
|
};
|
|
|
|
// Static configuration object
|
|
export const changePermissionsOperationConfig = {
|
|
toolType: ToolType.singleFile,
|
|
buildFormData: buildChangePermissionsFormData,
|
|
operationType: 'change-permissions',
|
|
endpoint: '/api/v1/security/add-password', // Change Permissions is a fake endpoint for the Add Password tool
|
|
filePrefix: 'permissions_',
|
|
defaultParameters,
|
|
} as const;
|
|
|
|
export const useChangePermissionsOperation = () => {
|
|
const { t } = useTranslation();
|
|
|
|
return useToolOperation({
|
|
...changePermissionsOperationConfig,
|
|
getErrorMessage: createStandardErrorHandler(
|
|
t('changePermissions.error.failed', 'An error occurred while changing PDF permissions.')
|
|
),
|
|
});
|
|
};
|