Skip to content

Commit

Permalink
Merge pull request #9 from jeremykirc/add-react-router
Browse files Browse the repository at this point in the history
Add react router
  • Loading branch information
Jeremy Kirchhoff authored Jun 25, 2020
2 parents 763e182 + ab06c92 commit c9d1f77
Show file tree
Hide file tree
Showing 84 changed files with 357 additions and 116 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ yarn-debug.log*
*.swo
/log
/tmp
.DS_store
Binary file added app/.DS_Store
Binary file not shown.
44 changes: 44 additions & 0 deletions app/controllers/api/v1/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

module Api
module V1
class ImagesController < ParentController
# GET /images/test_grid_data
# Return test image grid data.
def test_grid_data
render json: test_image_grid_data
end

private

def test_image_grid_data
[
{
title: 'Egg on Waffle',
description: 'Test1',
url: 'EggOnWaffle.jpg'
},
{
title: 'Roasted Carrots with Lentils',
description: 'Test2',
url: 'RoastedCarrotsAndLentils.jpg'
},
{
title: 'Mole Taco',
description: 'Test3',
url: 'MoleTaco.jpg'
},
{
title: 'Burrata and Orange',
description: 'Test4',
url: 'OttelenghiBurrataOrange.jpg'
},
{
title: 'Apricot Lavender Cake',
description: 'Test5',
url: 'ApricotLavenderCake.jpg'
}
]
end
end
end
end
6 changes: 6 additions & 0 deletions app/controllers/api/v1/parent_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Api
module V1
class ParentController < ApplicationController
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class StaticPagesController < ApplicationController
def home; end
def index; end
end
14 changes: 0 additions & 14 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
module ApplicationHelper
def image_grid_data
[
{
title: 'Pacific Ocean',
description: 'A big blue ocean',
url: asset_pack_path('media/images/Ocean.jpg')
},
{
title: 'Santa Barbara Mission',
description: 'The mission on a sunny afternoon',
url: asset_pack_path('media/images/SbMission.jpg')
}
]
end
end
44 changes: 0 additions & 44 deletions app/javascript/components/ImageGrid.jsx

This file was deleted.

11 changes: 0 additions & 11 deletions app/javascript/components/Welcome.jsx

This file was deleted.

Binary file removed app/javascript/images/Ocean.jpg
Binary file not shown.
Binary file removed app/javascript/images/SbMission.jpg
Binary file not shown.
24 changes: 0 additions & 24 deletions app/views/layouts/_header.html.erb

This file was deleted.

8 changes: 1 addition & 7 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
<%= render 'layouts/shim' %>
</head>
<body id='<%= yield(:page_id)%>'>
<%= render 'layouts/header' %>
<div class='wrapper'>
<div class='container'>
<%= render 'layouts/flash_messages' %>
<%= yield %>
</div>
</div>
<%= yield %>
</body>
</html>
3 changes: 0 additions & 3 deletions app/views/static_pages/home.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions app/views/static_pages/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= react_component('AppRouter') %>
Binary file added app/webpack/.DS_Store
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions app/webpack/components/AppRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { Container } from 'react-bootstrap';
import { BrowserRouter, Route, Switch } from 'react-router-dom';

import Header from '../components/Header';
import Home from '../components/Home';
import ImageUpload from '../components/ImageUpload';
import NotFound from '../components/NotFound';

const AppRouter = () => (
<BrowserRouter>
<Header />
<Container>
<Switch>
<Route path='/' component={Home} exact />
<Route path='/images/new' component={ImageUpload} />
<Route component={NotFound} />
</Switch>
</Container>
</BrowserRouter>
);

export default AppRouter;
37 changes: 37 additions & 0 deletions app/webpack/components/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logo from 'images/text-with-whisk-color.png';
import React from 'react';
import { Container } from 'react-bootstrap';
import { NavLink } from 'react-router-dom';

const Header = () => (
<nav className='navbar navbar-expand-sm sticky-top navbar-light'>
<Container>
<div id='nav-logo'>
<NavLink to='/' exact>
<img src={logo} />
</NavLink>
</div>
<button className='navbar-toggler' type='button' data-toggle='collapse' data-target='#navbarNavDropdown' aria-controls='navbarNavDropdown' aria-expanded='false' aria-label='Toggle navigation'>
<span className='navbar-toggler-icon'></span>
</button>
<div className='collapse navbar-collapse' id='navbarNavDropdown'>
<ul className='navbar-nav'>
<li className='nav-item dropdown'>
<a className='nav-link dropdown-toggle' href='/#' id='navbarDropdownMenuLink' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>
Dropdown
</a>
<div className='dropdown-menu' aria-labelledby='navbarDropdownMenuLink'>
<NavLink to='/' exact className='dropdown-item'>Home</NavLink>
<NavLink to='/images/new' className='dropdown-item'>Upload Image</NavLink>
</div>
</li>
<li className='nav-item'>
<NavLink to='/sign_in' className='nav-link'>Sign in</NavLink>
</li>
</ul>
</div>
</Container>
</nav>
);

export default Header;
11 changes: 11 additions & 0 deletions app/webpack/components/Home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';

import ImageGrid from './ImageGrid';

const Home = () => {
return (
<ImageGrid />
);
};

export default Home;
File renamed without changes.
50 changes: 50 additions & 0 deletions app/webpack/components/ImageGrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import axios from 'axios';
import React, { useEffect,useState } from 'react';
import { Container, Row } from 'react-bootstrap';

import ImageBox from './ImageBox';
import ImageModal from './ImageModal';

const ImageGrid = () => {
const [isModalShown, setIsModalShown] = useState(false);
const [imageInfo, setImageInfo] = useState({ title: '', description: '', url: '' });
const [imageBoxes, setImageBoxes] = useState();

useEffect(() => {
axios.get('/api/v1/images/test_grid_data')
.then(response => {
setImageBoxes(generateImageBoxes(response.data));
})
.catch(error => console.error(error));
}, []);

const showModal = (imageData) => {
setImageInfo(imageData);
setIsModalShown(true);
};

const hideModal = () => setIsModalShown(false);

const generateImageBoxes = (imageGridData) => {
return imageGridData.map((imageData, i) => (
<ImageBox
key={i}
imageInfo={imageData}
handleImageBoxClick={showModal}
/>
));
};

return (
<Container className='image-grid'>
<Row>{imageBoxes}</Row>
<ImageModal
show={isModalShown}
imageInfo={imageInfo}
handleClose={hideModal}
/>
</Container>
);
};

export default ImageGrid;
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions app/webpack/components/NotFound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { Link } from 'react-router-dom';

const NotFoundPage = () => (
<div>
Page Not Found - <Link to='/'>Return to home</Link>
</div>
);

export default NotFoundPage;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#nav-logo {
margin: 15px 20px;
margin: 15px 8px;
float: left;
img {
height: 40px;
Expand All @@ -8,10 +8,11 @@

.navbar {
background-color: $white;
padding: 8px 0px;
.navbar-nav {
float: none;
margin-left: auto;
margin-right: 20px;
margin-right: 0px;
text-align: center;
font-size: 14px;
font-weight: 600;
Expand All @@ -31,7 +32,7 @@
letter-spacing: 0.5px;
text-transform: uppercase;
color: $white !important;
&:hover, &:focus, &:active, &:active:hover, &:active:focus {
&:hover, &:focus, &:active, &:active:hover, &:active:focus, &.active {
background-color: $primary-dark;
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.image-grid {
margin-left: 3%;
margin-left: 2%;
margin-top: 30px;
.image-box-container {
padding: 0px;
Expand Down Expand Up @@ -33,7 +33,7 @@
}
}
@media (max-width: 767px) {
margin-left: 4%;
margin-left: 3%;
.image-box, .image-info {
height: 42vw !important;
max-width: 42vw !important;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

.container {
max-width: 98% !important;
padding: 0px 15px !important;
}

body {
Expand Down
File renamed without changes.
15 changes: 13 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Rails.application.routes.draw do
# Set static page paths.
root to: 'static_pages#home'
root to: 'static_pages#index'

# Forward all non-Ajax/non-API requests with HTML Mime type to index page.
get '*page', to: 'static_pages#index', constraints: ->(req) do
!req.xhr? && req.format.html? && req.path.exclude?('/api/')
end

# Set paths for signups.
get '/signup', to: 'users#new'
Expand All @@ -11,6 +16,12 @@
delete '/logout', to: 'sessions#destroy'

# Set paths for images.
resources :images, only: %i[new]
post '/upload', to: 'images#upload'

# Set paths for the API.
namespace :api do
namespace :v1 do
get '/images/test_grid_data', to: 'images#test_grid_data'
end
end
end
Loading

0 comments on commit c9d1f77

Please sign in to comment.