USWDS - File Input: Creates unique ID per upload. Fixes #4144#4313
Merged
Conversation
mejiaj
suggested changes
Sep 7, 2021
mejiaj
left a comment
Contributor
There was a problem hiding this comment.
Nice work! Just some minor comments.
| */ | ||
| const makeSafeForID = (name) => name.replace(/[^a-z0-9]/g, replaceName); | ||
|
|
||
| // Creates a unique ID based on upload time. Here we devide by 1000 and use mathfloor to get rid of the milliseconds. |
Contributor
There was a problem hiding this comment.
Suggested change
| // Creates a unique ID based on upload time. Here we devide by 1000 and use mathfloor to get rid of the milliseconds. | |
| // Takes a generated safe ID and creates a unique ID. |
| reader.onloadstart = function createLoadingImage() { | ||
| const imageId = makeSafeForID(fileName); | ||
| const imageId = createUniqueID(makeSafeForID(fileName)); | ||
| console.log(imageId); |
Contributor
There was a problem hiding this comment.
Remove test code console.log().
scottqueen-bixal
approved these changes
Sep 7, 2021
mejiaj
approved these changes
Oct 7, 2021
Contributor
|
Thank you! |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
#4144 pointed out that if a file with the same name is uploaded in two separate file input fields the preview spinner would spin indefinitely. After some research, I found this is because the two documents would share the same ID and the function that would replace the spinner would only select the first ID on the page. By implementing a function that assigns each upload an individual ID, an image preview properly loads.
The function used uses the date and time down to the second to assign an ID.
Steps to reproduce error
(As provided in #4144)
Additional information
Code to create unique ID based on upload time. Here we devide by 1000 and use mathfloor to get rid of the milliseconds.
Partnered with the existing makeSafeForID function:
Example Output:

Screenshot
Before you hit Submit, make sure you’ve done whichever of these applies to you:
npm testand make sure the tests for the files you have changed have passed.