-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from astrocket/react-support
React support
- Loading branch information
Showing
20 changed files
with
264 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Api | ||
class HomeController < Api::ApiController | ||
def index | ||
render json: { | ||
hello: "Hello World from Rails" | ||
} | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class HomeController < ApplicationController | ||
def index | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class ReactController < ApplicationController | ||
def index; render "layouts/react" end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from "react"; | ||
import { useRoutes, A } from "hookrouter"; | ||
import routes from "./routes"; | ||
|
||
function App() { | ||
const routeResult = useRoutes(routes); | ||
return ( | ||
<div> | ||
<A href='/home'>React SPA /home</A> | ||
<br/> | ||
<A href='/about'>React SPA /about</A> | ||
<br/> | ||
<a href="/">Rails SSR /</a> | ||
<hr/> | ||
{routeResult || <h1>404 on React SPA</h1>} | ||
</div> | ||
); | ||
} | ||
|
||
export default App; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import App from './App'; | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
ReactDOM.render(<App/>, document.body.appendChild(document.createElement('div'))) | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import api from 'utils/api'; | ||
|
||
function Index() { | ||
const [ helloWorld, setHelloWorld ] = useState('loading...'); | ||
|
||
useEffect(() => { | ||
console.log(window, 'mount'); | ||
api.get('/api/home/index').then((res) => { | ||
setHelloWorld(res.data.hello); | ||
}); | ||
return () => { | ||
console.log(window, 'unmount') | ||
} | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h2>HomeIndex Page from React.js</h2> | ||
<p>{helloWorld}</p> | ||
</div> | ||
); | ||
} | ||
|
||
export default Index; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from "react"; | ||
import HomeIndex from "./pages/home/Index.jsx"; | ||
|
||
const routes = { | ||
"/home": () => <HomeIndex />, | ||
"/about": () => <h2>About Page from React.js</h2> | ||
}; | ||
|
||
export default routes; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<div><% if use_react %> | ||
<a href="/home">React SPA /home</a> | ||
<br> | ||
<a href="/about">React SPA /about</a> | ||
<br> | ||
<a href="/">Rails SSR /</a> | ||
<% else %> | ||
<a href="/">Home</a> | ||
<% end %></div> | ||
<hr> | ||
<h2> | ||
Application Root | ||
</h2> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title><%= app_name %></title> | ||
<%%= csrf_meta_tags %> | ||
<%%= csp_meta_tag %> | ||
|
||
<%%= stylesheet_link_tag 'application', media: 'all' %> | ||
<%%= javascript_include_tag 'application', media: 'all' %> | ||
</head> | ||
|
||
<body> | ||
<%%= yield %> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title><%= app_name %></title> | ||
<%%= csrf_meta_tags %> | ||
<%%= csp_meta_tag %> | ||
|
||
<%%= stylesheet_link_tag 'application', media: 'all' %> | ||
<%%= javascript_pack_tag 'application' %> | ||
</head> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,53 @@ | ||
insert_into_file 'config/routes.rb', before: /^end/ do | ||
if use_active_admin | ||
<<-'RUBY' | ||
authenticate :admin_user do | ||
require 'sidekiq/web' | ||
mount Sidekiq::Web => '/sidekiq' | ||
end | ||
namespace :api do | ||
end | ||
%w( 404 422 500 ).each do |code| | ||
get code, :to => "errors#show", :code => code | ||
end | ||
RUBY | ||
else | ||
<<-'RUBY' | ||
require 'sidekiq/web' | ||
mount Sidekiq::Web => '/sidekiq' | ||
RUBY | ||
end | ||
end | ||
|
||
insert_into_file 'config/routes.rb', before: /^end/ do | ||
<<-'RUBY' | ||
namespace :api do | ||
get '/home/index' => 'home#index' | ||
end | ||
RUBY | ||
end | ||
|
||
insert_into_file 'config/routes.rb', before: /^end/ do | ||
if use_react | ||
<<-'RUBY' | ||
namespace :app do | ||
get '/' => 'home#index' | ||
end | ||
# To render react packs for any path except app/api | ||
scope '/:path', constraints: { path: /.+/ } do | ||
get '/' => 'react#index', as: :react # react_path | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
insert_into_file 'config/routes.rb', before: /^end/ do | ||
<<-'RUBY' | ||
root 'home#index' | ||
%w( 404 422 500 ).each do |code| | ||
get code, :to => "errors#show", :code => code | ||
end | ||
RUBY | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
copy_file 'lib/tasks/deploy.rake' | ||
copy_file 'lib/tasks/hot.rake' | ||
template 'lib/generators/stimulus/templates/controller.js.erb.tt' | ||
copy_file 'lib/generators/stimulus/stimulus_generator.rb' | ||
copy_file 'lib/generators/stimulus/USAGE' | ||
if use_react | ||
|
||
else | ||
template 'lib/generators/stimulus/templates/controller.js.erb.tt' | ||
copy_file 'lib/generators/stimulus/stimulus_generator.rb' | ||
copy_file 'lib/generators/stimulus/USAGE' | ||
end |
Oops, something went wrong.