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

Added Guitar Gallery #8

Merged
merged 10 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
defaults
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# See https://git-scm.com/docs/gitattributes for more about git attribute files.
LiorKGOW marked this conversation as resolved.
Show resolved Hide resolved

# Mark the database schema as having been generated.
db/schema.rb linguist-generated

# Mark the yarn lockfile as having been generated.
yarn.lock linguist-generated

# Mark any vendored files as having been vendored.
vendor/* linguist-vendored
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

/public/packs
/public/packs-test
/node_modules
/yarn-error.log
yarn-debug.log*
.yarn-integrity
Expand All @@ -43,3 +42,4 @@ yarn-debug.log*
Gemfile.lock
package-lock.json
yarn.lock
node_modules/
6 changes: 6 additions & 0 deletions app/__mocks__/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
get: jest.fn(),
post: jest.fn(),
put: jest.fn(),
delete: jest.fn()
};
30 changes: 30 additions & 0 deletions app/controllers/guitars_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# rubocop:disable Metrics/MethodLength

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

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

// Loading the data from the server:

const GuitarGallery = () => {
const getGuitars = async () => {
try {
const response = await axios.get(guitarsUrl);
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.length === 0 ? (
<Spinner isSVG aria-label="Contents of the Guitar Gallery" aria-valuetext="Loading..." />
) : (
guitars.map(({ name, url, price, description }) => (
<GuitarInfo key={name} name={name} url={url} price={price} description={description} />
))
)}
</Gallery>
</div>
);
};
Ron-Lavi marked this conversation as resolved.
Show resolved Hide resolved

export default GuitarGallery;
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.

1 change: 1 addition & 0 deletions app/javascript/components/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const guitarsUrl = '/guitars';
33 changes: 33 additions & 0 deletions app/javascript/components/tests/GuitarGallery.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { waitFor, render, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import axios from 'axios';
import GuitarGallery from '../GuitarGallery.js';
import { guitarsUrl } from '../constants';
import { guitarData } from './fixtures';

test("show loader when it's fetching data, then render Guitars", async () => {
await axios.get.mockResolvedValueOnce(guitarData);
const { unmount, getByText } = render(<GuitarGallery />);

// check the correct url is called:
expect(axios.get).toHaveBeenCalledWith(guitarsUrl);
expect(axios.get).toHaveBeenCalledTimes(1);

// ensure the title of the page is presented in the page:
expect(screen.getByText('Guitars For Sale:')).toBeInTheDocument();

// Another option to test it using getByText that react-testing-library/render returns:
const title = getByText(/Guitars For Sale:/);
expect(title).toHaveTextContent('Guitars For Sale:');

// ensure some content of the page is presented in the page:

await waitFor(() => screen.getByText('Description: some description in guitar1_test'));

expect(screen.getByText('Description: some description in guitar1_test')).toBeInTheDocument();
expect(screen.getByText('Description: some description in guitar2_test')).toBeInTheDocument();

// unmnount the component from the DOM
unmount();
});
16 changes: 16 additions & 0 deletions app/javascript/components/tests/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const guitarData = {
data: [
{
name: 'guitar1_test',
url: 'https://rukminim1.flixcart.com/image/416/416/acoustic-guitar/x/8/w/topaz-blue-signature-original-imaefec7uhypjdr9.jpeg?q=70',
price: '100',
description: 'some description in guitar1_test'
},
{
name: 'guitar2_test',
url: 'https://shop.brianmayguitars.co.uk/user/special/content/Antique%20Cherry%20a.jpg',
price: '200',
description: 'some description in guitar2_test'
}
]
};
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')));
});
1 change: 0 additions & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

2 changes: 0 additions & 2 deletions app/views/user/index.html.erb

This file was deleted.

5 changes: 2 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

Rails.application.routes.draw do
root
'guitars#index'

root 'home#index'
get '/guitars', to: 'guitars#index'
LiorKGOW marked this conversation as resolved.
Show resolved Hide resolved
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
Loading