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 brands to header
  • Loading branch information
Sumit Gupta committed May 4, 2018
commit 4067caab40e47c68f41d60b6198aaf8f4a402718
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"bootstrap": "^4.0.0",
"classnames": "^2.2.5",
"express": "^4.16.2",
"lodash": "4.17.10",
"mongoose": "^4.13.6",
"react": "^16.0.0",
"react-dom": "^16.0.0",
Expand Down
3 changes: 2 additions & 1 deletion src/client/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export default class App extends React.Component {
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/products" component={Products} />
<Route path="/products/:id" component={Product} />
<Route exact path="/products/:id(\d+)" component={Product} />
<Route exact path="/products/:brand(\w+)" component={Products} />
<Route path="/cart" component={Cart} />
</Switch>
</Container>
Expand Down
6 changes: 5 additions & 1 deletion src/client/redux/reducers/products.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as actionTypes from "../actionTypes";
import merge from "lodash/merge";

const initialState = {
byId: {},
Expand All @@ -13,13 +14,16 @@ export default function productsReducer(state = initialState, action) {
case actionTypes.GET_CART_ITEMS_REQUEST:
return {
...state,
byId: {},
ids: [],
isLoading: true
};

case actionTypes.GET_PRODUCTS_SUCCESS:
return {
...state,
...action.payload,
byId: merge({}, state.byId, action.payload.byId),
ids: [...state.ids, action.payload.ids],
isLoading: false
};

Expand Down
14 changes: 14 additions & 0 deletions src/server/connectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ let products = [
description: "Ergonomic keyboard",
price: 3000,
url: "/img/keyboard.jpeg"
},
{
id: 3,
name: "Keyboard",
brand: "Samsung",
description: "Ergonomic keyboard",
price: 3000,
url: "/img/keyboard.jpeg"
}
];

Expand All @@ -33,6 +41,12 @@ export function getProduct(id) {
return [products.find(product => product.id === id)];
}

export function getProductsByBrand(brand) {
return products.filter(
product => product.brand.toLowerCase() === brand.toLowerCase()
);
}

export function getCartItem(id) {
return cartItems.find(c => c.id === id);
}
Expand Down
11 changes: 8 additions & 3 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import morgan from "morgan";
import {
getUser,
getProducts,
getProduct,
getProductById,
getProductsByBrand,
getCartItems,
getCartItem,
addToCart,
Expand Down Expand Up @@ -43,9 +44,13 @@ app.get("/api/products", function(req, res) {
res.json(getProducts());
});

app.get("/api/products/:id", function(req, res) {
app.get("/api/products/:id(\\d+)/", function(req, res) {
const id = parseInt(req.params.id, 10);
res.json(getProduct(id));
res.json(getProductById(id));
});

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

app.get("/api/cart-items", function(req, res) {
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4969,6 +4969,10 @@ lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

lodash@4.17.10:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

"lodash@>=3.5 <5", lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0:
version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"
Expand Down