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

Ensure file_uploader doesn't trigger needless reruns #7641

Merged
merged 3 commits into from Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -213,8 +213,8 @@ describe("FileUploader widget enzyme tests", () => {
expect(getFiles(wrapper)[0].status.type).toBe("uploading")
expect(instance.status).toBe("updating")

// WidgetStateManager should not have been called yet
expect(props.widgetMgr.setFileUploaderStateValue).not.toHaveBeenCalled()
// WidgetStateManager should have been called on mounting
expect(props.widgetMgr.setFileUploaderStateValue).toHaveBeenCalledTimes(1)

// Wait a tick to simulate the file upload completing.
await process.nextTick
Expand Down Expand Up @@ -402,7 +402,7 @@ describe("FileUploader widget enzyme tests", () => {
expect(instance.status).toBe("ready")

// WidgetStateManager should have been called with our two file IDs
expect(props.widgetMgr.setFileUploaderStateValue).toHaveBeenCalledTimes(1)
expect(props.widgetMgr.setFileUploaderStateValue).toHaveBeenCalledTimes(2)

expect(props.widgetMgr.setFileUploaderStateValue).toHaveBeenLastCalledWith(
props.element,
Expand All @@ -426,9 +426,9 @@ describe("FileUploader widget enzyme tests", () => {
expect(instance.status).toBe("ready")

// WidgetStateManager should have been called with the file ID
// of the remaining file. This should be the second time WidgetStateManager
// of the remaining file. This should be the third time WidgetStateManager
// has been updated.
expect(props.widgetMgr.setFileUploaderStateValue).toHaveBeenCalledTimes(2)
expect(props.widgetMgr.setFileUploaderStateValue).toHaveBeenCalledTimes(3)
const newWidgetValue = [initialFiles[1]]
expect(props.widgetMgr.setFileUploaderStateValue).toHaveBeenLastCalledWith(
props.element,
Expand Down Expand Up @@ -464,14 +464,13 @@ describe("FileUploader widget enzyme tests", () => {
expect(getFiles(wrapper).length).toBe(0)
expect(instance.status).toBe("ready")

// WidgetStateManager will still have been called once, with a single
// value - the id that was last returned from the server.
// WidgetStateManager will still have been called once, during component mounting
expect(props.widgetMgr.setFileUploaderStateValue).toHaveBeenCalledTimes(1)
expect(props.widgetMgr.setFileUploaderStateValue).toHaveBeenCalledWith(
props.element,
buildFileUploaderStateProto([]),
{
fromUi: true,
fromUi: false,
}
)
})
Expand Down
18 changes: 18 additions & 0 deletions frontend/lib/src/components/widgets/FileUploader/FileUploader.tsx
Expand Up @@ -167,6 +167,24 @@ class FileUploader extends React.PureComponent<Props, State> {
}
}

public componentDidMount(): void {
const newWidgetValue = this.createWidgetValue()
if (newWidgetValue === undefined) {
return
}
AnOctopus marked this conversation as resolved.
Show resolved Hide resolved

const { element, widgetMgr } = this.props

// Set the state value on mount, to avoid triggering an extra rerun after
// the first rerun.
const prevWidgetValue = widgetMgr.getFileUploaderStateValue(element)
if (prevWidgetValue === undefined) {
widgetMgr.setFileUploaderStateValue(element, newWidgetValue, {
fromUi: false,
})
}
}

private createWidgetValue(): FileUploaderStateProto | undefined {
const uploadedFileInfo: UploadedFileInfoProto[] = this.state.files
.filter(f => f.status.type === "uploaded")
Expand Down