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

Disable cover upload submit button while uploading #8754

Conversation

rebecca-shoptaw
Copy link
Collaborator

Closes #8749

Feature. Adds a js function to disable Submit button and replace text with Uploading... when form is submitted.

Technical

The addLoadingStyling function is currently defined locally within the on submit function for #addcover-form. Could be moved into the initCoversAddManage function or elsewhere in the case of re-use (i.e. for the Save button in the manage cover window).

Another note: there is no function to clear the styling because the template is re-rendered when the function returns; if re-used elsewhere, a simple cleanup function may be necessary.

With @cdrini's help, I settled on the data-loading-text attribute to store the "Uploading..." message for translation purposes. Any other implementation of the function would require the same attribute in the corresponding HTML.

Testing

  1. Log in (if not logged in)
  2. Go to a book page
  3. Hover over the book and select Manage Covers
  4. Hit the Submit button in the Add tab

Screenshot

Submit Button Uploading

Stakeholders

@cdrini

@rebecca-shoptaw
Copy link
Collaborator Author

rebecca-shoptaw commented Jan 24, 2024

Alas it seems I ran into some problems with the linter because I called my new function above the var declarations of the form values. I opted to do this because I noticed that an invalid image URL would cause the function to exit early at this declaration and thus not run the styling function.

Any advice re: the best way to resolve this would be wonderful!

Note: it seems to be the .trim() within the val function that causes the error (when the selector value is undefined):

function val(selector) {
    return $(selector).val().trim();
}

Preventing this error from happening (i.e. by ensuring the $(selector).val() exists before trimming it should hypothetically allow me to switch the order back.

@cdrini cdrini self-assigned this Jan 24, 2024
Copy link
Collaborator

@cdrini cdrini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This looks great! One change -- should also fix the linting issue.

Comment on lines 60 to 61
const btn = $('#imageUpload');
btn.prop('disabled', true).html(btn.data('loading-text'));
Copy link
Collaborator

@cdrini cdrini Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be in a function; you can move these lines down to just after 83. That will also let the validation checks pass first before disabling the button.

We want those error checks to run first; because if it returns it won't actually upload the image, so we don't want to show the loading text.

So I think you will have to fix the val bug!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me! I'll go ahead and make the changes here, then have a look at the val issue tomorrow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved everything and it all seems to be working great; just wanted to double check whether the val fix should be done here as well or as a separate issue/PR. Thanks so much!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh here is good! Should be a quick fix :) Note you'll have to do a rebase ontop of the lastest master since the other covers PR touches similar code 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Will finish everything up in the morning!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! And fixing the val bug made it clear that the checks to return an error if no file/url/id had been submitted - if (file === '' && url == '' && coverid == '') - had been failing the whole time because the coverid val defaults to undefined and not ''. So I threw in a quick fix for that as well by switching to if (!file && !url && !coverid), so now it looks a bit cleaner and the correct error message shows if nothing is entered.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Much cleaner 🥳

@rebecca-shoptaw rebecca-shoptaw force-pushed the 8749/disable-cover-submit-btn-onclick branch from 23440c1 to 8ea5a81 Compare January 24, 2024 23:58
@codecov-commenter
Copy link

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (6617494) 16.62% compared to head (5caec20) 16.61%.

Files Patch % Lines
openlibrary/plugins/openlibrary/js/covers.js 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8754      +/-   ##
==========================================
- Coverage   16.62%   16.61%   -0.01%     
==========================================
  Files          88       88              
  Lines        4698     4700       +2     
  Branches      837      837              
==========================================
  Hits          781      781              
- Misses       3399     3401       +2     
  Partials      518      518              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rebecca-shoptaw rebecca-shoptaw force-pushed the 8749/disable-cover-submit-btn-onclick branch from 5caec20 to 3e9e522 Compare January 26, 2024 18:42
@cdrini cdrini added the On testing.openlibrary.org This PR has been deployed to testing.openlibrary.org for testing label Jan 27, 2024
Copy link
Collaborator

@cdrini cdrini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm! Thanks @rebecca-shoptaw ! Tested uploading, upload causing an error, upload via upload vs via url.

@cdrini cdrini merged commit d05f2dc into internetarchive:master Jan 27, 2024
4 checks passed
@@ -68,7 +68,9 @@
</div>

<div class="formElement" style="margin: $('15px 0px 0px 15px;' if doc.type.key == '/type/work' else '0px;')">
<button type="submit" name="upload" id="imageUpload" value="$_('Submit')" class="largest">$_("Submit")</button>
<button type="submit" name="upload" id="imageUpload" value="$_('Submit')" class="largest" data-loading-text='$_("Uploading...")'>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick to using data-i18n for localized strings.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's chat about it during our ABC call! We use a few different patterns in the code, +1 it would be good to standardize on one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I find the other patterns?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've seen 3 patterns?

  1. data-i18n
  2. one-offs (like here)
  3. IIRC some parts of the codebase have like i18n in a separate <script> tag? I can't recall

It's a touch hard to search for! I think data-i18n is a reasonable standard should we choose to adopt it :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some examples I found of 2/3

2:

<a href="javascript:;" data-confirm-text="$:_('Yes, I\'m sure')" data-cancel-text="$_('No, cancel')"><span></span>$_('Remove this item?')</a>

3:

<input type="hidden" name="list-i18n-strings" value="$json_encode(show_list_i18n_strings)">

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof... if I ever find the guy that implemented #3...

Converting these to use data-i18n could be a good first issue (or issues).

@jimchamp jimchamp removed the On testing.openlibrary.org This PR has been deployed to testing.openlibrary.org for testing label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disable cover upload submit button after click
4 participants