Skip to content

Commit

Permalink
Code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
imse-ty committed Oct 6, 2020
1 parent 397236d commit d36f1f5
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function App() {
<Router>
<Header />

<div className='p-3'>
<main className='p-3'>
<Switch>
<Route exact path='/'>
<Home />
Expand All @@ -28,7 +28,7 @@ function App() {
<Product />
</Route>
</Switch>
</div>
</main>

<Footer />
</Router>
Expand Down
8 changes: 4 additions & 4 deletions src/Components/CounterExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ function CounterExample() {
<h1>
{count}
</h1>
<h1 onClick={() => setCount(count + 1)}>
<h2 onClick={() => setCount(count + 1)}>
Plus
</h1>
<h1 onClick={() => setCount(count - 1)}>
</h2>
<h2 onClick={() => setCount(count - 1)}>
Minus
</h1>
</h2>
</div>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
function Footer() {
return (
<footer className='bg-gray-200 text-center text-xs p-3 absolute bottom-0 w-full'>
AppName
<p>AppName</p>
</footer>
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Header.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { Link } from 'react-router-dom';
import Navigation from './Navigation'

function Header() {
return (
<header className='border-b font-bold p-3 flex justify-between items-center'>
<span>AppName</span>

<Link to="/">AppName</Link>
<Navigation />
</header>
)
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'
function Loader() {
return (
<div className='flex justify-center'>
<div className='loader'></div>
<div className='loader' />
</div>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/NavigationMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Link } from 'react-router-dom';
function NavigationMenu(props) {
return (
<div>
<div className='font-bold py-3'>AppName</div>
<header className='font-bold py-3'>AppName</header>
<ul>
<li>
<Link
Expand Down
13 changes: 6 additions & 7 deletions src/Components/ProductCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ export default function ProductCard(props) {
<div className="font-bold mb-3">
$ {props.product.price}
</div>
<div className="mb-3">
<p className="mb-3">
{props.product.description}
</div>
<Link
to={`/products/${props.product.id}`}
className="bg-blue-500 text-white p-2 flex justify-center"
>
View
</p>
<Link to={`/products/${props.product.id}`}>
<button className="bg-blue-500 text-white p-2 flex justify-center w-full">
View
</button>
</Link>
</div>
</div >
Expand Down
14 changes: 7 additions & 7 deletions src/Hooks/HTTPRequest.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import React, { useState, useEffect } from 'react'
import axios from 'axios'
import { useState, useEffect } from 'react';
import axios from 'axios';

export function useAxiosGet(url) {
const [products, setProducts] = useState({
const [request, setRequest] = useState({
loading: false,
data: null,
error: false
});

useEffect(() => {
setProducts({
setRequest({
loading: true,
data: null,
error: false
});

axios.get(url).then(response => {
setProducts({
setRequest({
loading: false,
data: response.data,
error: false
});
}).catch(() => {
setProducts({
setRequest({
loading: false,
data: null,
error: true
});
});
}, [url]);

return products;
return request;
}

4 changes: 2 additions & 2 deletions src/Views/About.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from 'react'

function About() {
return (
<div>
<article>
<h1 className='font-bold text-2xl mb-3'>About us</h1>
<p>This is the about page content.</p>
</div>
</article>
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Views/Home.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react'
import React from 'react';
import Loader from '../Components/Loader';
import ProductCard from '../Components/ProductCard';
import { useAxiosGet } from '../Hooks/HTTPRequest';

function Home() {
const url = 'https://5f7b89e100bd740016909a8a.mockapi.io/products?page=1&limit=10';
const products = useAxiosGet(url);
let content = null;
let products = useAxiosGet(url);

if (products.loading) {
content = <Loader />
}

if (products.error) {
content = <p>There was an error. Please refresh or try again.</p>
content = <p className="bg-red-300 p-3 rounded-md">There was an error. Please refresh or try again.</p>
}

if (products.data) {
Expand Down
32 changes: 3 additions & 29 deletions src/Views/Product.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,14 @@
import React, { useState, useEffect } from 'react'
import axios from 'axios'
import React from 'react'
import { useParams } from 'react-router-dom';
import Loader from '../Components/Loader';
import { useAxiosGet } from '../Hooks/HTTPRequest';

function Product() {
const { id } = useParams();
const url = `https://5f7b89e100bd740016909a8a.mockapi.io/products/${id}`;
const [product, setProduct] = useState({
loading: false,
data: null,
error: false
});
const product = useAxiosGet(url);
let content = null;

useEffect(() => {
setProduct({
loading: true,
data: null,
error: false
});

axios.get(url).then(response => {
setProduct({
loading: false,
data: response.data,
error: false
});
}).catch(() => {
setProduct({
loading: false,
data: null,
error: true
});
});
}, [url]);

if (product.loading) {
content = <Loader />
}
Expand Down

0 comments on commit d36f1f5

Please sign in to comment.