|
|
@ -1,3 +1,4 @@ |
|
|
|
|
|
|
|
import { AxiosResponse } from 'axios'; |
|
|
|
import { |
|
|
|
import { |
|
|
|
FormEventHandler, |
|
|
|
FormEventHandler, |
|
|
|
MouseEventHandler, |
|
|
|
MouseEventHandler, |
|
|
@ -17,6 +18,16 @@ import Spinner from '../Spinner'; |
|
|
|
import fetchJSON from '../../lib/fetchers/fetchJSON'; |
|
|
|
import fetchJSON from '../../lib/fetchers/fetchJSON'; |
|
|
|
import mainAxiosInstance from '../../lib/singletons/mainAxiosInstance'; |
|
|
|
import mainAxiosInstance from '../../lib/singletons/mainAxiosInstance'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ReducedFileLocation = Partial< |
|
|
|
|
|
|
|
Pick<FileLocation, 'fileLocationUUID' | 'isFileLocationActive'> |
|
|
|
|
|
|
|
>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type EditRequestContent = Partial< |
|
|
|
|
|
|
|
Pick<FileDetailMetadata, 'fileName' | 'fileType' | 'fileUUID'> |
|
|
|
|
|
|
|
> & { |
|
|
|
|
|
|
|
fileLocations: ReducedFileLocation[]; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
type FileEditProps = { |
|
|
|
type FileEditProps = { |
|
|
|
filesOverview: FileOverviewMetadata[]; |
|
|
|
filesOverview: FileOverviewMetadata[]; |
|
|
|
onEditFilesComplete?: () => void; |
|
|
|
onEditFilesComplete?: () => void; |
|
|
@ -40,6 +51,9 @@ const FileEditForm = ( |
|
|
|
onPurgeFilesComplete, |
|
|
|
onPurgeFilesComplete, |
|
|
|
}: FileEditProps = FILE_EDIT_FORM_DEFAULT_PROPS as FileEditProps, |
|
|
|
}: FileEditProps = FILE_EDIT_FORM_DEFAULT_PROPS as FileEditProps, |
|
|
|
): JSX.Element => { |
|
|
|
): JSX.Element => { |
|
|
|
|
|
|
|
const [editRequestContents, setEditRequestContents] = useState< |
|
|
|
|
|
|
|
EditRequestContent[] |
|
|
|
|
|
|
|
>([]); |
|
|
|
const [filesToEdit, setFilesToEdit] = useState<FileToEdit[]>([]); |
|
|
|
const [filesToEdit, setFilesToEdit] = useState<FileToEdit[]>([]); |
|
|
|
const [isLoadingFilesToEdit, setIsLoadingFilesToEdit] = |
|
|
|
const [isLoadingFilesToEdit, setIsLoadingFilesToEdit] = |
|
|
|
useState<boolean>(false); |
|
|
|
useState<boolean>(false); |
|
|
@ -57,14 +71,14 @@ const FileEditForm = ( |
|
|
|
const generateFileInfoChangeHandler = |
|
|
|
const generateFileInfoChangeHandler = |
|
|
|
(fileIndex: number): FileInfoChangeHandler => |
|
|
|
(fileIndex: number): FileInfoChangeHandler => |
|
|
|
(inputValues, { fileLocationIndex } = {}) => { |
|
|
|
(inputValues, { fileLocationIndex } = {}) => { |
|
|
|
if (fileLocationIndex) { |
|
|
|
if (fileLocationIndex !== undefined) { |
|
|
|
filesToEdit[fileIndex].fileLocations[fileLocationIndex] = { |
|
|
|
editRequestContents[fileIndex].fileLocations[fileLocationIndex] = { |
|
|
|
...filesToEdit[fileIndex].fileLocations[fileLocationIndex], |
|
|
|
...editRequestContents[fileIndex].fileLocations[fileLocationIndex], |
|
|
|
...inputValues, |
|
|
|
...inputValues, |
|
|
|
}; |
|
|
|
}; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
filesToEdit[fileIndex] = { |
|
|
|
editRequestContents[fileIndex] = { |
|
|
|
...filesToEdit[fileIndex], |
|
|
|
...editRequestContents[fileIndex], |
|
|
|
...inputValues, |
|
|
|
...inputValues, |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
@ -75,24 +89,61 @@ const FileEditForm = ( |
|
|
|
|
|
|
|
|
|
|
|
setIsLoadingFilesToEdit(true); |
|
|
|
setIsLoadingFilesToEdit(true); |
|
|
|
|
|
|
|
|
|
|
|
const editPromises = filesToEdit.map( |
|
|
|
const editPromises = editRequestContents.reduce<Promise<AxiosResponse>[]>( |
|
|
|
({ fileLocations, fileName, fileType, fileUUID }) => |
|
|
|
( |
|
|
|
mainAxiosInstance.put( |
|
|
|
reducedEditPromises, |
|
|
|
`/files/${fileUUID}`, |
|
|
|
{ fileLocations, fileName, fileType, fileUUID }, |
|
|
|
JSON.stringify({ |
|
|
|
) => { |
|
|
|
fileName, |
|
|
|
const editRequestContent: Partial<EditRequestContent> = {}; |
|
|
|
fileType, |
|
|
|
|
|
|
|
fileLocations: fileLocations.map( |
|
|
|
if (fileName !== undefined) { |
|
|
|
({ fileLocationUUID, isFileLocationActive }) => ({ |
|
|
|
editRequestContent.fileName = fileName; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fileType !== undefined) { |
|
|
|
|
|
|
|
editRequestContent.fileType = fileType; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const changedFileLocations = fileLocations.reduce< |
|
|
|
|
|
|
|
ReducedFileLocation[] |
|
|
|
|
|
|
|
>( |
|
|
|
|
|
|
|
( |
|
|
|
|
|
|
|
reducedFileLocations, |
|
|
|
|
|
|
|
{ fileLocationUUID, isFileLocationActive }, |
|
|
|
|
|
|
|
) => { |
|
|
|
|
|
|
|
if (isFileLocationActive !== undefined) { |
|
|
|
|
|
|
|
reducedFileLocations.push({ |
|
|
|
fileLocationUUID, |
|
|
|
fileLocationUUID, |
|
|
|
isFileLocationActive, |
|
|
|
isFileLocationActive, |
|
|
|
}), |
|
|
|
}); |
|
|
|
), |
|
|
|
} |
|
|
|
}), |
|
|
|
|
|
|
|
{ |
|
|
|
return reducedFileLocations; |
|
|
|
headers: { 'Content-Type': 'application/json' }, |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
), |
|
|
|
[], |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (changedFileLocations.length > 0) { |
|
|
|
|
|
|
|
editRequestContent.fileLocations = changedFileLocations; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const stringEditFileRequestContent = JSON.stringify(editRequestContent); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (stringEditFileRequestContent !== '{}') { |
|
|
|
|
|
|
|
reducedEditPromises.push( |
|
|
|
|
|
|
|
mainAxiosInstance.put( |
|
|
|
|
|
|
|
`/files/${fileUUID}`, |
|
|
|
|
|
|
|
stringEditFileRequestContent, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
headers: { 'Content-Type': 'application/json' }, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reducedEditPromises; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
[], |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
Promise.all(editPromises) |
|
|
|
Promise.all(editPromises) |
|
|
@ -180,6 +231,27 @@ const FileEditForm = ( |
|
|
|
}), |
|
|
|
}), |
|
|
|
).then((fetchedFilesDetail) => { |
|
|
|
).then((fetchedFilesDetail) => { |
|
|
|
setFilesToEdit(fetchedFilesDetail); |
|
|
|
setFilesToEdit(fetchedFilesDetail); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const initialEditRequestContents: EditRequestContent[] = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for ( |
|
|
|
|
|
|
|
let fileIndex = 0; |
|
|
|
|
|
|
|
fileIndex < fetchedFilesDetail.length; |
|
|
|
|
|
|
|
fileIndex += 1 |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
const fetchedFileDetail = fetchedFilesDetail[fileIndex]; |
|
|
|
|
|
|
|
initialEditRequestContents.push({ |
|
|
|
|
|
|
|
fileUUID: fetchedFileDetail.fileUUID, |
|
|
|
|
|
|
|
fileLocations: fetchedFileDetail.fileLocations.map( |
|
|
|
|
|
|
|
({ fileLocationUUID }) => ({ |
|
|
|
|
|
|
|
fileLocationUUID, |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setEditRequestContents(initialEditRequestContents); |
|
|
|
|
|
|
|
|
|
|
|
setIsLoadingFilesToEdit(false); |
|
|
|
setIsLoadingFilesToEdit(false); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, [filesOverview]); |
|
|
|
}, [filesOverview]); |
|
|
|