Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make multi file deployment work again #4708

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,19 @@ export default class DeploymentTool extends PureComponent {

async saveConfiguration(tab, configuration) {

const fileSystem = this.props._getGlobal('fileSystem');
const {
endpoint,
deployment
} = configuration;

await this.saveEndpoint(endpoint);

const deploymentWithAttachments = await withSerializedAttachments(
deployment, file => fileSystem.getFilePath(file)
);
const tabConfiguration = {
deployment: withSerializedAttachments(deployment),
deployment: deploymentWithAttachments,
endpointId: endpoint.id
};

Expand Down Expand Up @@ -663,13 +667,18 @@ function getDeploymentType(tab) {
}
}

function withSerializedAttachments(deployment) {
async function withSerializedAttachments(deployment, getPath) {
const { attachments: fileList = [] } = deployment;
const attachments = fileList.map(file => ({ path: file.path }));
const attachments = await Promise.all(fileList.map(async file => {
const path = file.path || await getPath(file.contents);
return {
path
};
}));

return { ...deployment, attachments };
}

function basename(filePath) {
return filePath.split('\\').pop().split('/').pop();
return filePath ? filePath.split('\\').pop().split('/').pop() : '<unnamed>';
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,11 @@ describe('<DeploymentTool>', function() {

// given
const file = {
path: '/file/path/user.form',
name: 'user.form',
contents: []
};
const attachments = [ {
path: file.path
path: '/non/existing/path/user.form'
} ];
const savedConfiguration = createConfiguration({ attachments });
const config = {
Expand Down Expand Up @@ -310,7 +309,6 @@ describe('<DeploymentTool>', function() {
const { attachments: deployedAttachments } = deploySpy.args[0][1].deployment;

expect(deployedAttachments[0]).have.property('name', file.name);
expect(deployedAttachments[0]).have.property('path', file.path);
expect(deployedAttachments[0]).have.property('contents');
expect(deployedAttachments[0].contents).not.to.be.null;
});
Expand Down Expand Up @@ -1523,6 +1521,9 @@ function createSavedConfiguration(configuration) {

function createFileSystem(overrides = {}) {
return {
getFilePath() {
return '/file/path';
},
readFile() {},
...overrides
};
Expand Down
20 changes: 16 additions & 4 deletions client/src/shared/ui/form/FileInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import ErrorIcon from '../../../../resources/icons/Error.svg';
* @typedef FileDescriptor
* @property {Uint8Array|Blob|null} contents
* @property {string} name
* @property {string} path
* @property {number} lastModified
*/

export default function FileInput(props) {
Expand All @@ -47,8 +47,9 @@ export default function FileInput(props) {
function onChange() {
const { files } = inputRef.current;
const fileDescriptors = toFileDescriptors(files);
const newValue = uniqueBy(file => fileToKey(file), value, fileDescriptors);

form.setFieldValue(name, uniqueBy('path', value, fileDescriptors));
form.setFieldValue(name, newValue);
}

function removeFile(fileToRemove) {
Expand Down Expand Up @@ -82,6 +83,10 @@ export default function FileInput(props) {
);
}

/**
* @param {object} props
* @param {FileDescriptor[]} props.files
*/
function FileList(props) {
const {
errors: formErrors,
Expand All @@ -96,7 +101,7 @@ function FileList(props) {
<ul className={ classNames('file-list', { 'is-invalid': invalid }) }>
{ files.map((file, index) => (
<ListItem
key={ file.path } name={ file.name } onRemove={ () => onRemove(file) }
key={ fileToKey(file) } name={ file.name } onRemove={ () => onRemove(file) }
error={ getIn(formErrors, `${fieldName}[${index}]`) } />
))}
</ul>
Expand Down Expand Up @@ -134,7 +139,6 @@ function toFileDescriptors(fileList) {
.map(file => {
return {
name: file.name,
path: file.path,
lastModified: file.lastModified,
contents: file
};
Expand Down Expand Up @@ -167,3 +171,11 @@ function getIconFromFileType(name, error) {
return <div className="default_file-icon" />;
}
}

/**
* @param {FileDescriptor} file
* @returns {string}
*/
function fileToKey(file) {
return `${file.name}_${file.lastModified}`;
}
Loading