Skip to content

Commit

Permalink
Added Guitar Gallery
Browse files Browse the repository at this point in the history
This Component presents the guitars gotten from the server.
It contains a sub-component: GuitarInfo which is a GalleryItem
In addition this commit removes the unecessary files regarding Users.js
  • Loading branch information
LiorKGOW committed Nov 2, 2022
1 parent 1b371df commit 2aec5fb
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.6.3
2.7.6
24 changes: 24 additions & 0 deletions app/controllers/guitars_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class GuitarsController < ApplicationController
def index
render :json => [
{
'name':'guitar1',
'url':'https://rukminim1.flixcart.com/image/416/416/acoustic-guitar/x/8/w/topaz-blue-signature-original-imaefec7uhypjdr9.jpeg?q=70',
'price':'100',
'description':'blablabla 1'
},
{
'name':'guitar2',
'url':'https://shop.brianmayguitars.co.uk/user/special/content/Antique%20Cherry%20a.jpg',
'price':'200',
'description':'blablabla 2'
},
{
'name':'guitar3',
'url':'https://cdn.mos.cms.futurecdn.net/Yh6r74b8CAj2jbdf2FAhq4-970-80.jpg.webp',
'price':'300',
'description':'blablabla 3'
}
]
end
end
7 changes: 0 additions & 7 deletions app/controllers/user_controller.rb

This file was deleted.

45 changes: 45 additions & 0 deletions app/javascript/components/GuitarGallery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { Title, TitleSizes, Gallery } from '@patternfly/react-core';
import GuitarInfo from './GuitarInfo';

// Loading the data from the server:

export const GuitarGallery = () => {
const getGuitars = async () => {
try {
const response = await axios.get('/guitars');
if (response.data) {
setGuitars(response.data);
}
} catch (error) {
console.error(error);
}
};

const [guitars, setGuitars] = useState([]);

useEffect(() => {
getGuitars();
}, []);

return (
<div>
<Title headingLevel="h1" size={TitleSizes['4xl']}>
Guitars For Sale:
</Title>
<Gallery
hasGutter
minWidths={{
md: '100px',
lg: '150px',
xl: '200px',
'2xl': '300px'
}}>
{guitars.map(({ name, url, price, description }) => (
<GuitarInfo key={name} name={name} url={url} price={price} description={description} />
))}
</Gallery>
</div>
);
};
38 changes: 38 additions & 0 deletions app/javascript/components/GuitarInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import PropTypes from 'prop-types';
import {
GalleryItem,
Grid,
GridItem,
Card,
CardTitle,
CardBody,
CardFooter
} from '@patternfly/react-core';

const GuitarInfo = ({ name, url, price, description }) => {
return (
<GalleryItem>
<Card id={name} isFlat>
<Grid md={6}>
<GridItem>
<img className="Guitar-Image" src={url} alt="Guitar Image" />
</GridItem>
<GridItem>
<CardTitle>{name}</CardTitle>
<CardBody>Price: {price}</CardBody>
<CardFooter>Description: {description}</CardFooter>
</GridItem>
</Grid>
</Card>
</GalleryItem>
);
};
GuitarInfo.propTypes = {
name: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
price: PropTypes.string.isRequired,
description: PropTypes.string.isRequired
};

export default GuitarInfo;
31 changes: 0 additions & 31 deletions app/javascript/components/Users.js

This file was deleted.

4 changes: 2 additions & 2 deletions app/javascript/packs/hello_react.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// of the page.
import React from 'react';
import ReactDOM from 'react-dom';
import { Users } from '../components/Users';
import { GuitarGallery } from '../components/GuitarGallery';
import './fonts.css';

document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(<Users />, document.body.appendChild(document.createElement('div')));
ReactDOM.render(<GuitarGallery />, document.body.appendChild(document.createElement('div')));
});
2 changes: 2 additions & 0 deletions app/views/guitar_gallery/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Guitars#index</h1>
<p>Find me in app/views/guitars/index.html.erb</p>
2 changes: 0 additions & 2 deletions app/views/user/index.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
root 'home#index'
get '/user', to: 'user#index'
get '/guitars', to: 'guitars#index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

0 comments on commit 2aec5fb

Please sign in to comment.