Skip to content

Commit

Permalink
Merge pull request #1497 from OpenC3/upload_file
Browse files Browse the repository at this point in the history
Fix fileDialogCallback promise logic
  • Loading branch information
jmthomas authored Aug 26, 2024
2 parents abfaab3 + aa59a9b commit a2c4e49
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export default {
)}?bucket=OPENC3_${this.root.toUpperCase()}_BUCKET`,
)
// This pushes the file into storage by using the fields in the presignedRequest
// See storage_controller.rb get_presigned_request()
// See storage_controller.rb get_upload_presigned_request()
const response = await axios({
...presignedRequest,
data: this.file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,7 @@ export default {
}
this.showMetadata()
break
// This is called continuously by the backend
case 'open_file_dialog':
case 'open_files_dialog':
this.file.title = data.args[0]
Expand All @@ -1953,34 +1954,46 @@ export default {
}
},
async fileDialogCallback(files) {
this.file.show = false // Close the dialog
// Set fileNames to 'Cancel' in case they cancelled
// otherwise we will populate it with the file names they selected
let fileNames = 'Cancel'
// Record all the API request promises so we can ensure they complete
let promises = []
if (files != 'Cancel') {
fileNames = []
await files.forEach(async (file) => {
files.forEach((file) => {
fileNames.push(file.name)
// Reassign data to presignedRequest for readability
const { data: presignedRequest } = await Api.get(
`/openc3-api/storage/upload/${encodeURIComponent(
`${window.openc3Scope}/tmp/${file.name}`,
)}?bucket=OPENC3_CONFIG_BUCKET`,
promises.push(
Api.get(
`/openc3-api/storage/upload/${encodeURIComponent(
`${window.openc3Scope}/tmp/${file.name}`,
)}?bucket=OPENC3_CONFIG_BUCKET`,
).then((response) => {
// This pushes the file into storage by using the fields in the presignedRequest
// See storage_controller.rb get_upload_presigned_request()
promises.push(
axios({
...response.data,
data: file,
}),
)
}),
)
// This pushes the file into storage by using the fields in the presignedRequest
// See storage_controller.rb get_presigned_request()
const response = await axios({
...presignedRequest,
data: file,
})
})
}
await Api.post(`/script-api/running-script/${this.scriptId}/prompt`, {
data: {
method: this.file.multiple ? 'open_files_dialog' : 'open_file_dialog',
answer: fileNames,
prompt_id: this.activePromptId,
},
// We have to wait for all the upload API requests to finish before notifying the prompt
Promise.all(promises).then((responses) => {
Api.post(`/script-api/running-script/${this.scriptId}/prompt`, {
data: {
method: this.file.multiple
? 'open_files_dialog'
: 'open_file_dialog',
answer: fileNames,
prompt_id: this.activePromptId,
},
}).then((response) => {
this.file.show = false // Close the dialog
})
})
},
setError(event) {
Expand Down
9 changes: 6 additions & 3 deletions playwright/tests/data-viewer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,14 @@ test('works with UTC date / times', async ({ page, utils }) => {

await addComponent(page, utils, 'INST', 'ADCS')
await page.locator('[data-test=start-button]').click()
await utils.sleep(500)
localStartTime = addSeconds(localStartTime, 3)
localStartTime = addSeconds(localStartTime, 5)
expect(
await page.inputValue('[data-test=history-component-text-area]'),
).toContain(localStartTime.toISOString().split('.')[0])
).toContain(
// Original string is like '2024-08-26T21:23:41.319Z'
// So we split on ':' to just get the year and hour
localStartTime.toISOString().split(':').slice(0, 1).join(':'),
)

// Switch back to local time
await page.goto('/tools/admin/settings')
Expand Down

0 comments on commit a2c4e49

Please sign in to comment.