This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pagination + 404 * Link to detail * Get Products hook * Romoved products store * Product detail woth hooks * Fetch products in cart * Padding removed from paggination * Remove @flow * Naive exception handling * Inline exports * Routes params changed to query, changed imports of actions * Reordered imports * Extract routes into constants * Fix for resolve conditions passed into useApi hook
- Loading branch information
1 parent
8781930
commit 320178f
Showing
23 changed files
with
291 additions
and
202 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
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,8 +1,14 @@ | ||
import qs from 'qs' | ||
import { api } from '../api-client' | ||
import { formatProduct } from './utils/format-product' | ||
|
||
export const getProducts = async () => { | ||
const { data, included } = await api('/api/skus?include=prices') | ||
export const getProducts = async urlQuery => { | ||
const { data, meta, included } = await api( | ||
`/api/skus?${qs.stringify({ include: 'prices', ...urlQuery })}` | ||
) | ||
|
||
return data.map(product => formatProduct(product, included)) | ||
return { | ||
data: data.map(product => formatProduct(product, included)), | ||
meta, | ||
} | ||
} |
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,30 @@ | ||
import { useState, useEffect } from 'react' | ||
|
||
const useApi = (fn, resolveCondition = []) => { | ||
const [data, setData] = useState(null) | ||
const [isLoading, setLoading] = useState(false) | ||
|
||
if (!Array.isArray(resolveCondition)) { | ||
// eslint-disable-next-line no-console | ||
console.error('Passed resolve condition for useEffect hook is not an Array') | ||
} | ||
|
||
const request = (...args) => { | ||
setLoading(true) | ||
fn(...args) | ||
.then(returnedData => setData(returnedData)) | ||
// eslint-disable-next-line no-console | ||
.catch(console.error) | ||
.finally(() => setLoading(false)) | ||
} | ||
|
||
useEffect(request, resolveCondition) | ||
|
||
return { | ||
request, | ||
data, | ||
isLoading, | ||
} | ||
} | ||
|
||
export { useApi } |
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,20 @@ | ||
import * as React from 'react' | ||
import range from 'ramda/src/range' | ||
import map from 'ramda/src/map' | ||
import { Link } from 'react-router-dom' | ||
|
||
import * as routes from '../../routes' | ||
|
||
import { List, ListItem } from './styled' | ||
|
||
const renderPaginationItem = number => ( | ||
<ListItem key={number}> | ||
<Link to={`${routes.PRODUCT_LIST}?page=${number}`}>{number}</Link> | ||
</ListItem> | ||
) | ||
|
||
const Pagination = ({ pages }) => ( | ||
<List>{map(renderPaginationItem, range(1, pages + 1))}</List> | ||
) | ||
|
||
export { Pagination } |
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,16 @@ | ||
import styled from 'styled-components' | ||
|
||
export const List = styled.ul` | ||
align-items: center; | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: center; | ||
list-style: none; | ||
padding: 0; | ||
text-align: center; | ||
width: 100%; | ||
` | ||
|
||
export const ListItem = styled.li` | ||
margin: 5px; | ||
` |
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,28 @@ | ||
import * as React from 'react' | ||
|
||
import { getProductById } from '../../api/products/get-product' | ||
import { useApi } from '../../api/use-api' | ||
|
||
import Loader from '../../components/Loader' | ||
import Button from '../../components/Button' | ||
|
||
const CartItem = ({ productId, quantity, removeProduct }) => { | ||
const { data: product, isLoading } = useApi( | ||
() => getProductById(productId), | ||
productId | ||
) | ||
|
||
return ( | ||
<li key={productId}> | ||
{isLoading && <Loader small />} | ||
<p> | ||
{product ? product.name : productId} - {quantity} | ||
</p> | ||
<Button type="button" onClick={() => removeProduct(productId)}> | ||
Remove | ||
</Button> | ||
</li> | ||
) | ||
} | ||
|
||
export { CartItem } |
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,50 +1,43 @@ | ||
import React, { Component } from 'react' | ||
import React from 'react' | ||
import { connect } from 'react-redux' | ||
|
||
import Button from '../../components/Button' | ||
import Layout from '../../components/Layout' | ||
import { H1 } from '../../components/Typography' | ||
import { removeProduct } from '../../store/cart/actions' | ||
import * as cartActions from '../../store/cart/actions' | ||
import { CartItem } from './CartItem' | ||
|
||
class CartView extends Component { | ||
render() { | ||
return ( | ||
<Layout> | ||
<H1>Your cart</H1> | ||
<ul> | ||
{this.props.items.map(item => ( | ||
<li key={item.product.id}> | ||
<p> | ||
{item.product.name} - {item.quantity} | ||
</p> | ||
<Button | ||
type="button" | ||
onClick={() => this.props.removeProduct(item.product.id)} | ||
> | ||
Remove | ||
</Button> | ||
</li> | ||
))} | ||
</ul> | ||
</Layout> | ||
) | ||
} | ||
const CartView = ({ items, removeProduct }) => { | ||
return ( | ||
<Layout> | ||
<H1>Your cart</H1> | ||
<ul> | ||
{items.map(item => ( | ||
<CartItem | ||
key={item.product.id} | ||
productId={item.product.id} | ||
quantity={item.quantity} | ||
removeProduct={removeProduct} | ||
/> | ||
))} | ||
</ul> | ||
</Layout> | ||
) | ||
} | ||
|
||
const mapStateToProps = state => ({ | ||
items: Object.keys(state.cart).map(productId => ({ | ||
quantity: state.cart[productId], | ||
product: state.products.find(p => p.id === productId), | ||
product: { id: productId }, | ||
})), | ||
}) | ||
|
||
const actionCreators = { | ||
removeProduct, | ||
const mapDispatchToProps = { | ||
removeProduct: cartActions.removeProduct, | ||
} | ||
|
||
const Cart = connect( | ||
mapStateToProps, | ||
actionCreators | ||
mapDispatchToProps | ||
)(CartView) | ||
|
||
export { Cart } |
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,5 @@ | ||
import * as React from 'react' | ||
|
||
const NotFound = () => <div>{"Sorry, page doesn't exist"}</div> | ||
|
||
export { NotFound } |
Oops, something went wrong.