Skip to content

Redux workshop solution 0 #4

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add code for fetching code by brand
  • Loading branch information
Sumit Gupta committed May 4, 2018
commit a74c65d8716e70effc2020de31e8e90efdd39e8c
13 changes: 11 additions & 2 deletions src/client/components/Products.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ function Product({ product }) {

class Products extends React.Component {
componentDidMount() {
const { productActions } = this.props;
productActions.getProducts();
const { productActions, match } = this.props;
const brand = match.params.brand;
productActions.getProducts(brand);
}

componentWillReceiveProps(nextProps) {
const { match, productActions } = this.props;
if (nextProps.match.params.brand !== match.params.brand) {
productActions.getProducts(nextProps.match.params.brand);
}
}

render() {
const { products } = this.props;
return (
Expand Down
3 changes: 2 additions & 1 deletion src/client/redux/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { transformProductsApi } from "../transformers/transformProductsApi";
import { transformGetCartItemsApi } from "../transformers/transformGetCartItemsApi";

export const getProducts = productId => {
const apiUrl = productId ? `/api/products/${productId}` : "/api/products";
return dispatch => {
return fetch("/api/products/").then(async response => {
return fetch(apiUrl).then(async response => {
const responseData = await response.json();
const data = transformProductsApi(responseData);
dispatch({
Expand Down
2 changes: 1 addition & 1 deletion src/client/redux/reducers/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const initialState = {

export default function productsReducer(state = initialState, action) {
switch (action.type) {
case actionTypes.GET_CART_ITEMS_REQUEST:
case actionTypes.GET_PRODUCTS_REQUEST:
return {
...state,
byId: {},
Expand Down
4 changes: 3 additions & 1 deletion src/server/connectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ let products = [
{
id: 1,
name: "Macbook",
brand: "Apple",
description: "Latest Macbook with 16GB ram and Quad core processor",
price: 65000,
url: "/img/macbook.jpeg"
},
{
id: 2,
name: "Keyboard",
brand: "Apple",
description: "Ergonomic keyboard",
price: 3000,
url: "/img/keyboard.jpeg"
Expand Down Expand Up @@ -37,7 +39,7 @@ export function getProducts() {
return products;
}

export function getProduct(id) {
export function getProductById(id) {
return [products.find(product => product.id === id)];
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ app.get("/api/products/:id(\\d+)/", function(req, res) {
});

app.get("/api/products/:brand(\\w+)/", function(req, res) {
getProductsByBrand(req.params.brand).then(response => res.json(response));
res.json(getProductsByBrand(req.params.brand));
});

app.get("/api/cart-items", function(req, res) {
Expand Down