Skip to content
Open
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
6 changes: 3 additions & 3 deletions components/cartlist-card/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const CartCard = (props) => {
.card-item {
width: 100%;
}
.card-bold{
.card-bold {
font-weight: bold;
}
.cartcard-container > img {
Expand Down Expand Up @@ -114,12 +114,12 @@ export const CartCard = (props) => {
.cartcard-container {
flex-direction: column;
}
.cartcard-button{
.cartcard-button {
display: flex;
flex-direction: row;
justify-content: space-evenly;
}
.cartcard-fnbutton{
.cartcard-fnbutton {
display: flex;
flex-direction: row;
justify-content: space-evenly;
Expand Down
3 changes: 3 additions & 0 deletions constants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const SHOP_PRODUCTS_ENDPOINT = 'http://localhost:5000/crypto/products';
export const SOP_PRODUCT_BY_ID_ENDPOINT =
'http://localhost:5000/crypto/products';
76 changes: 38 additions & 38 deletions mock/products.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
{
"Shoe": {
"id": "Shoe",
"image": "shoes.jpeg",
"name": "Shoe",
"usage": [
"Can be used if you are pissed of at someone",
"Can also be used to promote healthy activites"
],
"category": "Footwear",
"manufacturer": "Ankush",
"price": 200
},
"Flower": {
"id": "Flower",
"image": "flowers.jpeg",
"name": "Flower",
"usage": [
"Can be used as token of appreciation",
"Can be a good subsitute for gift"
],
"category": "Gift",
"manufacturer": "Ankush",
"price": 300
},
"Coffee": {
"id": "Coffee",
"image": "coffee.jpeg",
"name": "Coffee",
"usage": [
"Can serve as ice-breaker",
"Can be used as token of appreciation"
],
"category": "Drink",
"manufacturer": "Ankush",
"price": 500
}
}
[
{
"id": "Shoe",
"image": "shoes.jpeg",
"name": "Shoe",
"usage": [
"Can be used if you are pissed of at someone",
"Can also be used to promote healthy activites"
],
"category": "Footwear",
"manufacturer": "Ankush",
"price": 200
},
{
"id": "Flower",
"image": "flowers.jpeg",
"name": "Flower",
"usage": [
"Can be used as token of appreciation",
"Can be a good subsitute for gift"
],
"category": "Gift",
"manufacturer": "Ankush",
"price": 300
},
{
"id": "Coffee",
"image": "coffee.jpeg",
"name": "Coffee",
"usage": [
"Can serve as ice-breaker",
"Can be used as token of appreciation"
],
"category": "Drink",
"manufacturer": "Ankush",
"price": 500
}
]
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"dependencies": {
"@babel/plugin-transform-react-jsx": "^7.12.11",
"axios": "^0.21.1",
"chart.js": "2.9.4",
"eslint-plugin-jest": "^24.1.3",
"framer-motion": "^2.9.4",
Expand Down
2 changes: 1 addition & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function Home() {
<div className={styles.transactionMenu}>
<div className={`${styles.card} ${styles.content}`}>
<div className={`${styles.heading}`}>
<p> Latest Transactions</p>
<p> Latest Transactions</p>
</div>
<TransactionList transactions={transactionData} />
</div>
Expand Down
4 changes: 2 additions & 2 deletions pages/shop/[product].js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from 'prop-types';
import productData from '../../mock/products.json';
import { getProductById } from '../../utils';
import { ProductDetails } from '../../components/product-details';
const CACHE_MAX_AGE = 43200;
const ProductDetail = ({ productJSON }) => {
Expand All @@ -12,7 +12,7 @@ export async function getServerSideProps(context) {
params: { product },
} = context;

const productJSON = productData[product];
const productJSON = await getProductById(product);

return { props: { productJSON } };
}
Expand Down
35 changes: 28 additions & 7 deletions pages/shop/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import Link from 'next/link';
import { ShopCard } from '../../components/shoplist-card';
Expand All @@ -10,10 +11,10 @@ import {
} from '../../redux/action';
// import Header from '../../components/header';
import { Footer } from '../../components/footer';
import productData from '../../mock/products.json';
import { getShopProducts } from '../../utils';

const products = Object.keys(productData);
const Shop = (props) => {
const { products } = props;
const { addCartItem, addShopListItem } = props;
const { delCartItem, delShopListItem } = props;
return (
Expand All @@ -26,15 +27,16 @@ const Shop = (props) => {
</button>
</div>
<div className="shoppinglist-container">
{products.map((itemName) => {
{products.map((product) => {
const { id } = product;
return (
<ShopCard
key={itemName}
product={productData[itemName]}
quantity={props.shopListItemsCount[itemName] || 0}
key={id}
product={product}
quantity={props.shopListItemsCount[id] || 0}
add={{ addCartItem, addShopListItem }}
del={{ delCartItem, delShopListItem }}
link={{ href: '/shop/[product]', as: `/shop/${itemName}` }}
link={{ href: '/shop/[product]', as: `/shop/${id}` }}
/>
);
})}
Expand Down Expand Up @@ -67,11 +69,30 @@ const Shop = (props) => {
);
};

export async function getServerSideProps() {
// context.res.setHeader('Cache-Control', `max-age=${CACHE_MAX_AGE}`);

const products = await getShopProducts();
// console.log(products);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT : Please remove this console statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

return { props: { products } };
}

const mapStateToProps = (state) => {
const shopListItemsCount = getShopListCount(state);
return { shopListItemsCount };
};

Shop.propTypes = {
products: PropTypes.arrayOf(PropTypes.object),
addCartItem: PropTypes.func,
addShopListItem: PropTypes.func,
delCartItem: PropTypes.func,
delShopListItem: PropTypes.func,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If all these props are not required, then we should have these included in the defaultProps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But should we have placeholders of functions ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these functions props are required in this component, make it as .required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But a common track for component testing is to have two exports with and without redux's connect , just for testing props , and since functions are part of pre compiled code do we really need to make it is required?

};
Shop.defaultProps = {
products: [],
};

export default connect(mapStateToProps, {
addCartItem,
delCartItem,
Expand Down
15 changes: 15 additions & 0 deletions utils/get_product_by_id/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import axios from 'axios';
import { SOP_PRODUCT_BY_ID_ENDPOINT } from '../../constants';

export const get_product_by_id = async (id) => {
try {
const {
data: { Products },
} = await axios.get(`${SOP_PRODUCT_BY_ID_ENDPOINT}/${id}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use axiosInstance over here. Since the base url will always remain same.
In utils we can have class that handles network request for the project and expose methods that will return api result. This will help in decoupling network calls from component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good , we can actually go 1 step further and make our own request handler function for this , that would work like a higher level api for multiple request types including handling of options building query string, i actually have worked on a similar kind of thing in my current project which works like a charm, let me know if you want me to go ahead with that , and we can discuss that further.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes so it would be like wrapper function where we can give options as input and make network call.

// console.log(data);
return Products;
} catch (error) {
console.log(error.message);
return {};
}
};
16 changes: 16 additions & 0 deletions utils/get_shop_products/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import axios from 'axios';

import { SHOP_PRODUCTS_ENDPOINT } from '../../constants';

export const get_shop_products = async () => {
try {
const {
data: { Products },
} = await axios.get(SHOP_PRODUCTS_ENDPOINT);
// console.log(Products);
return Products;
} catch (error) {
console.log(error.message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a way where we can let the user's on the screen know that there has been some error, rather than just console logging it. If I click on something which is performing an http request, If there is an error, how will I know about it. What say @Kratika0907

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly what i was thinking , but for that we should better restructure the redux for handling and keeping track of network errors

return [];
}
};
2 changes: 2 additions & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { get_shop_products as getShopProducts } from './get_shop_products';
export { get_product_by_id as getProductById } from './get_product_by_id';