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

Refactor JS Function add_tag() and create add_comment() for Javascript API #6422

Merged
merged 14 commits into from Nov 28, 2019

Conversation

nstjean
Copy link
Contributor

@nstjean nstjean commented Oct 8, 2019

Fixes #6411 (<=== Add issue number here)

Creating a Pull Request early to get feedback! I'm not sure if I'm doing this right.

Make sure these boxes are checked before your pull request (PR) is ready to be reviewed and merged. Thanks!

  • PR is descriptively titled 📑 and links the original issue above 🔗
  • tests pass -- look for a green checkbox ✔️ a few minutes after opening your PR -- or run tests locally with rake test
  • code is in uniquely-named feature branch and has no merge conflicts 📁
  • screenshots/GIFs are attached 📎 in case of UI updation
  • ask @publiclab/reviewers for help, in a comment below

We're happy to help you get this ready -- don't be afraid to ask for help, and don't be discouraged if your tests fail at first!

If tests do fail, click on the red X to learn why by reading the logs.

Please be sure you've reviewed our contribution guidelines at https://publiclab.org/contributing-to-public-lab-software

Thanks!

@nstjean
Copy link
Contributor Author

nstjean commented Oct 11, 2019

@cesswairimu Can you (or any of the mentors!) check my code to make sure I'm doing this correctly? Thank you!!

@cesswairimu
Copy link
Collaborator

Sure, not sure why travis is failing....taking a look

@nstjean
Copy link
Contributor Author

nstjean commented Oct 14, 2019

@jywarren Can you take a look at my code and let me know if this is what you intended? I wasn't sure if you wanted to reference the form at all in the Javascript.

@nstjean
Copy link
Contributor Author

nstjean commented Nov 2, 2019

AddComment is done!

I had to rebase in branch before pushing, I don't know if I did it right. Now it's showing me a bunch of codeclimate errors.

I edited notes/_comment.html.erb because the tag nesting was incorrect. I had to change it so that the comment p was tested inside the .comment-class.
refactor-js-functions code

@nstjean nstjean changed the title [WIP] Refactor JS Function add_tag() and create add_comment() for Javascript API Refactor JS Function add_tag() and create add_comment() for Javascript API Nov 3, 2019
@jywarren
Copy link
Member

Wow, it looks like this may need some rebasing and cleaning up! @nstjean this can be a bit rough, but please save a local copy of your branch with git branch refactor-js-backup so nothing goes wrong.

Would you like to try rebasing again over the latest master branch? I can also help a bit with this!

There's some guidance here, however!

https://publiclab.org/wiki/contributing-to-public-lab-software#Rewinding+the+master+branch

@jywarren
Copy link
Member

Also, i think this needs parentheses to disambiguate the || logic, just a note!

image

The rest of the CodeClimate changes can be ignored for now, as they're mostly stylistic!

@jywarren
Copy link
Member

Thanks for sticking with this one! It's a really awesome one! Just ping me if you get super stuck!

@nstjean
Copy link
Contributor Author

nstjean commented Nov 19, 2019

Yes I'll try rebasing using rewinding the master branch!

@nstjean
Copy link
Contributor Author

nstjean commented Nov 21, 2019

@jywarren I reset the HEAD, pulled from upstream (publiclab), switched to my branch, rebased. But now when trying to run tests on my local everything is failing, even in Master. Do I have to do something else?

Screenshot Test Errors

@nstjean
Copy link
Contributor Author

nstjean commented Nov 21, 2019

When I try to run my test for my javascript function I get this error, which points to a Chrome version error. But I haven't changed anything other than pulling the new version of Master.

ERROR["test_adding_a_tag_via_javascript", #<Minitest::Reporters::Suite:0x00005587bc60d350 @name="CommentTest">, 1.5917023420042824]
 test_adding_a_tag_via_javascript#CommentTest (1.59s)
Selenium::WebDriver::Error::SessionNotCreatedError:         Selenium::WebDriver::Error::SessionNotCreatedError: session not created: This version of ChromeDriver only supports Chrome version 78
            test/system/comment_test.rb:9:in `block in <class:CommentTest>'

@@ -0,0 +1,9 @@
function sendFormSubmissionAjax(dataObj, selector) {
Copy link
Member

Choose a reason for hiding this comment

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

This is looking good! Perhaps this file needs a more descriptive name, however; but is it a generic utility? If so, maybe can add a sentence or two on how to use it?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I should do that!

@jywarren
Copy link
Member

This looks good!

Not sure where this error is coming from though:

ERROR["test_find_by_tagname_and_user_id", #<Minitest::Reporters::Suite:0x00005653ad056bb0 @name="NodeTest">, 2.091748925999923]
 test_find_by_tagname_and_user_id#NodeTest (2.09s)
NoMethodError:         NoMethodError: undefined method `title' for nil:NilClass
            test/unit/node_test.rb:529:in `block in <class:NodeTest>'

That's for this test:

assert_equal 'Question by a moderated user', Node.find_by_tag_and_author('question:spectrometer', 9, 'questions').first.title

... i guess it seems unrelated? I'll try restarting the unit tests to see what happens..

Thanks for the epic rebase!!!!

@@ -0,0 +1,27 @@
require 'test_helper'
Copy link
Member

Choose a reason for hiding this comment

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

Wow, really awesome tests!!!!

page.evaluate_script("addComment('fantastic four')")

# check that the tag showed up on the page
assert_selector('#comments-list .comment-body p', text: 'fantastic four')
Copy link
Member

Choose a reason for hiding this comment

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

🎉

// Allows data to be submitted from anywhere on the page using Javascript without using the form itself
function sendFormSubmissionAjax(dataObj, selector) {
$.ajax({
url: $(selector).attr('action'), // grab the URL from the form
Copy link
Member

Choose a reason for hiding this comment

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

Could we offer a parameter like url which defaults back to this value? That way it could be done without a form!

@nstjean
Copy link
Contributor Author

nstjean commented Nov 26, 2019

@jywarren I've been thinking about this. Will it get confusing if sometimes the function is passed a URL and sometimes it is passed the ID of the form? It only needs one or the other. Maybe it should be two different functions? Say function2 takes in a URL as a parameter and submits the form. Then function2 takes in a selector, gets the URL from the form, and then calls function1?

@jywarren
Copy link
Member

jywarren commented Nov 26, 2019 via email

@nstjean
Copy link
Contributor Author

nstjean commented Nov 27, 2019

Okay! :)

@nstjean
Copy link
Contributor Author

nstjean commented Nov 27, 2019

Ok I allowed either a selector or a url to get passed in and it decides what to do from there. :) I also added a test case for a URL being used in both addComment and addTag. Tests work on local.

@@ -51,3 +51,10 @@ function insertTitleSuggestionTemplate() {
var newText = currentText+template;
element.val(newText);
}

// JS API for submitting comments
function addComment(comment, submitTo) {
Copy link
Member

Choose a reason for hiding this comment

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

Great, and this works appropriately for nested comments (responding to other comments) as well as top-level comments? Just checking!


end

test 'adding a tag via javascript with url only' do
Copy link
Member

Choose a reason for hiding this comment

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

Love this. Just one more question - does adding tags to your profile also still work? I think that might've become broken at some point because we (unfortunately) share code between node tags and user tags, but just curious if this touches that at all? Thanks!

@nstjean
Copy link
Contributor Author

nstjean commented Nov 27, 2019

Let me check on those possibilities!

@jywarren
Copy link
Member

Thanks. What's really nice about your code is that we will be moving away from more brittle implementations like the current one, and backing it up with tests, so the hope is to have fewer "gotchas" like this in the future. Thanks a lot!!!

@jywarren
Copy link
Member

For what it's worth, i just tried adding a profile tag and got some kind of error i think... it submitted via XHR (ajax) to: https://publiclab.org/profile/warren?remote=true&name=providence also which I think isn't right!

@jywarren
Copy link
Member

So, what we can do is merge this, pending the comment-responses clarification above, and then in a new PR (if you're willing!) try to fix the profile tagging -- preferably by extending the methods you've written, to have a more rational system for handling profile tagging, too!

The nice thing is that we are trying to add profile tags at different places around the site, so having a general purpose JS method to do this will be really great.

@nstjean
Copy link
Contributor Author

nstjean commented Nov 28, 2019

Ok! I added the ability to add a parent comment ID number to the function call so it will nest it appropriately... and added a test to check.

I can check on the profile tag implementation in another PR!

@jywarren
Copy link
Member

jywarren commented Nov 28, 2019 via email

@nstjean
Copy link
Contributor Author

nstjean commented Nov 28, 2019

It's throwing an error on my test test_adding_a_tag_via_javascript_with_url_only - but it works when I run it on local. I'll try to figure out what's going on with it.

@nstjean
Copy link
Contributor Author

nstjean commented Nov 28, 2019

It appears I have fixed it! Should be all set!

@jywarren jywarren merged commit 2539bcb into publiclab:master Nov 28, 2019
@jywarren
Copy link
Member

Super! Now go take a break! 😁🎉🙌🏽

You'll be able to test it out at stable.publiclab.org soon once it builds!

@nstjean
Copy link
Contributor Author

nstjean commented Nov 28, 2019

Thanks! 🎉 Enjoy your Thanksgiving break!

vinitshahdeo pushed a commit to vinitshahdeo/plots2 that referenced this pull request Feb 1, 2020
…t API (publiclab#6422)

* change addTag to AJAX

* addTag ajax now works

* add system test for addTag js function

* fix codeclimate error

* add js function AddComment and system test for function

* add parentheses to logic statement

* add JS function sendFormSubmissionAjax to reduce duplicate code

* rename ajax.js to submit_form_ajax.js

* remove parentheses in Node.find_by_tag_and_author

* allow sendFormSubmissionAjax to take in a selector or a url

* allow js addComment and addTest to use URL and add test case for both

* fix codeclimate error

* add to JS addComment ability to reply to parent comment - and add test for it

* attempt to fix tag_test travis error
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.

Refactor JS functions add_tag() and create add_comment() for pure JS API
3 participants