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

demo react-over-hotwired and proof #1508 fixed #592

Closed
wants to merge 1 commit into from

Conversation

theforestvn88
Copy link

@theforestvn88 theforestvn88 commented May 21, 2024

This is a demo for my pull request react_over_hotwired and also proof that the react_on_rails issue 1508 is fixed.

  • to run this pr:

    • clone theforestvn88:react_over_hotwired repo, build on local
    • yalc push
    • cd react-webpack-rails-tutorial && yalc add react-on-rails
  • demo on new tab Hotwired tab, with a link at the bottom to show the proof of 1508.

  • about the issue 1508: i setup the same as the reproduction, the car model replaced by the comment model. The react-component inside the form will show with the flag force_load = true that proof my solution could solve this issue. (you can turn-off the flag force_load to see that the nested react-component will not show).

  • the main purpose of this pull request is to demo how to work with react-components over rails 7 hotwired, but what i have done so far is only sending new react-components to client side over hotwired. How about the existed react-components ?

    For example, in this demo i setup 2 components: comment-list and comment-form, comment-form will send over hotwired and it looks good, but when a comment is created, the created comment will be sent to client over hotwired and prepend directly to comments list on comment-list component, this is a normal hotwired flow --> but with react, it very smell, in my opinion. We should not inject a view data into dom-elements hierarchy under the react-component control, right ?

    of course with json response or set up ActionCable (which has it's cost), we could do that by dispatching new data (as you guys already did on Stimulus page demo), but what i try to do here is with turbo-stream response.

    the only solution come to my mind (so far) is sending a script that dispatching append/prepend/delete/modify... data through the redux store.

so what do you think ?


This change is Reviewable

Copy link

coderabbitai bot commented May 21, 2024

Walkthrough

The update introduces significant enhancements to the integration of React and Hotwired in the Rails application. Key changes include updating the react_on_rails gem to a local path, adding Turbo Stream templates and actions in controllers, and introducing new React components for handling comments. These modifications aim to improve the dynamic interaction and real-time updates within the application, along with better internationalization support.

Changes

File Path Change Summary
Gemfile Updated react_on_rails gem to use a local path.
app/controllers/comments_controller.rb Added format.turbo_stream in create action and introduced test_1508 action.
app/controllers/pages_controller.rb Added hotwired method to assign @props with comments_json_string.
app/views/comments/_form_1508.html.erb Introduced a form for submitting comments with error handling and embedded React component.
app/views/comments/create.turbo_stream.erb Added Turbo Stream template for dynamically updating comments and comment form.
app/views/comments/new.turbo_stream.erb Added Turbo Stream template for updating the comment form with a new comment component.
app/views/comments/test_1508.turbo_stream.erb Added Turbo Stream template to replace a specific DOM element with a new form.
app/views/pages/hotwired.html.erb Introduced a demo showcasing React integration over Hotwired.
client/app/bundles/comments/components/CommentBox/CommentList/CommentList.jsx Changed TransitionGroup component's className from "commentList" to "comments".
client/app/bundles/comments/components/HotwiredCommentScreen/HotwiredCommentForm.jsx Introduced HotwiredCommentForm and I18nWrapper components.
client/app/bundles/comments/components/HotwiredCommentScreen/HotwiredCommentScreen.jsx Added HotwiredCommentScreen and I18nWrapper components with various methods and JSX updates.
client/app/bundles/comments/components/HotwiredCommentScreen/HotwiredCommentScreen.module.scss Introduced CSS styles for elements with opacity transitions.
client/app/bundles/comments/components/NavigationBar/NavigationBar.jsx Added a new list item for "HotWired" with a corresponding link.
client/app/bundles/comments/constants/paths.js Added HOTWIRED_PATH constant.
client/app/packs/client-bundle.js Imported @hotwired/turbo-rails and registered new React components in ReactOnRails.
config/routes.rb Added routes for hotwired and test_1508 with Turbo Stream format.

🐇✨
In the realm of code, where comments dance,
React and Hotwired take a chance.
With Turbo Streams and forms anew,
Real-time updates come into view.
A rabbit's joy, in lines so bright,
Enhancing apps, day and night.
Hopping through the fields of code,
Making sure the features flowed.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between acfe281 and 6194793.
Files selected for processing (16)
  • Gemfile (1 hunks)
  • app/controllers/comments_controller.rb (2 hunks)
  • app/controllers/pages_controller.rb (1 hunks)
  • app/views/comments/_form_1508.html.erb (1 hunks)
  • app/views/comments/create.turbo_stream.erb (1 hunks)
  • app/views/comments/new.turbo_stream.erb (1 hunks)
  • app/views/comments/test_1508.turbo_stream.erb (1 hunks)
  • app/views/pages/hotwired.html.erb (1 hunks)
  • client/app/bundles/comments/components/CommentBox/CommentList/CommentList.jsx (1 hunks)
  • client/app/bundles/comments/components/HotwiredCommentScreen/HotwiredCommentForm.jsx (1 hunks)
  • client/app/bundles/comments/components/HotwiredCommentScreen/HotwiredCommentScreen.jsx (1 hunks)
  • client/app/bundles/comments/components/HotwiredCommentScreen/HotwiredCommentScreen.module.scss (1 hunks)
  • client/app/bundles/comments/components/NavigationBar/NavigationBar.jsx (1 hunks)
  • client/app/bundles/comments/constants/paths.js (1 hunks)
  • client/app/packs/client-bundle.js (3 hunks)
  • config/routes.rb (2 hunks)
Files skipped from review due to trivial changes (8)
  • app/views/comments/_form_1508.html.erb
  • app/views/comments/create.turbo_stream.erb
  • app/views/comments/new.turbo_stream.erb
  • app/views/comments/test_1508.turbo_stream.erb
  • app/views/pages/hotwired.html.erb
  • client/app/bundles/comments/components/HotwiredCommentScreen/HotwiredCommentScreen.module.scss
  • client/app/bundles/comments/components/NavigationBar/NavigationBar.jsx
  • client/app/bundles/comments/constants/paths.js
Additional comments not posted (14)
config/routes.rb (2)

14-14: The route for "hotwired" is correctly set up to point to the PagesController#hotwired.


28-28: The route for "test_1508" is correctly configured to use turbo_stream, aligning with the PR's objectives to test issue #1508.

client/app/packs/client-bundle.js (4)

2-2: Import of @hotwired/turbo-rails is correctly added to support Turbo Streams integration.


16-17: Imports for HotwiredCommentForm and HotwiredCommentScreen are correctly added for component registration.


32-32: Registration of HotwiredCommentScreen is correctly done, making it available for use in Rails views.


33-33: Registration of HotwiredCommentForm is correctly done, making it available for use in Rails views.

app/controllers/pages_controller.rb (1)

41-43: The hotwired method is correctly implemented to set @props for demonstrating React components over Rails 7 Hotwired.

client/app/bundles/comments/components/CommentBox/CommentList/CommentList.jsx (1)

81-81: The change in the className attribute from "commentList" to "comments" is appropriate and aligns with the enhanced functionality and user experience objectives.

client/app/bundles/comments/components/HotwiredCommentScreen/HotwiredCommentScreen.jsx (2)

14-53: The HotwiredCommentScreen component is correctly implemented with internationalization support and comment display functionality, aligning with the PR's objectives.


56-87: The I18nWrapper component is correctly implemented to provide internationalization context for HotwiredCommentScreen, enhancing the application's internationalization strategy.

app/controllers/comments_controller.rb (2)

36-36: The addition of format.turbo_stream in the create action is correctly implemented to support Turbo Streams functionality.


103-105: The test_1508 action is correctly implemented to initialize a new Comment object for testing the fix for issue #1508.

Gemfile (1)

8-8: Ensure the local path for react_on_rails is intended for development only.

This change should be reverted before merging into production to ensure stability and version control. Please confirm that this is the intended usage.

client/app/bundles/comments/components/HotwiredCommentScreen/HotwiredCommentForm.jsx (1)

89-120: Implementation of I18nWrapper looks good.

This component correctly sets up internationalization for the HotwiredCommentForm. Good use of React's context API with IntlProvider.

submitCommentError: null,
};

_.bindAll(this, 'handleCommentSubmit');
Copy link

Choose a reason for hiding this comment

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

Consider using arrow functions for method bindings instead of lodash's bindAll.

Arrow functions can make the code cleaner and reduce the dependency on lodash for this purpose. Here's how you can refactor the handleCommentSubmit method:

handleCommentSubmit = (comment) => {
  // method body remains the same
}

@justin808
Copy link
Member

Hi @theforestvn88 can you update your branch?

@@ -5,7 +5,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.1.2"

gem "react_on_rails", "14.0.0"
gem "react_on_rails", path: '../react_on_rails'
Copy link
Member

Choose a reason for hiding this comment

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

You have this change locally only

Copy link
Author

Choose a reason for hiding this comment

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

it's just a demo to prove 1508, and it depends on the branch [react_on_hotwired](theforestvn88:react_over_hotwired) which has not been merged so it linked to the locally build of that branch.

anyway, i don't think this PR should be merged so we don't need to be serious about this.


<%= turbo_stream.update "comment-form" do %>
<%= link_to "New Comment", new_comment_path, data: {turbo_stream: true} %>
<% end %>
Copy link
Member

Choose a reason for hiding this comment

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

You are missing EOL on new files.

@@ -22,4 +23,7 @@
get "comment-list", to: "comments#comment_list"
resources :comments
mount ActionCable.server => "/cable"


get "test_1508", to: "comments#test_1508", format: :turbo_stream
Copy link
Member

Choose a reason for hiding this comment

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

^^^ ?

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.

2 participants