Skip to content
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

create products layout. #3

Merged
merged 8 commits into from
Jun 8, 2020
Merged
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
7 changes: 7 additions & 0 deletions .storybook/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const tailwindcss = require('tailwindcss');
module.exports = {
plugins: [
tailwindcss('./tailwind.config.js'),
require('autoprefixer'),
],
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "webpack-dev-server --open --config ./webpack/webpack.dev.js",
"build": "webpack --config ./webpack/webpack.prod.js",
"server": "http-server ./public",
"storybook": "yarn build:tailwind && start-storybook -p 6006",
"storybook": "yarn build:tailwind && start-storybook -p 6006 --ci",
"build-storybook": "build-storybook",
"build:tailwind": "postcss public/css/base.scss -o public/css/index.scss",
"deploy-storybook": "storybook-to-ghpages"
Expand Down Expand Up @@ -67,6 +67,7 @@
"webpack-dev-server": "^3.10.3"
},
"dependencies": {
"@unbxd-ui/unbxd-search-core": "../search-JS-core",
"babel-polyfill": "^6.26.0",
"babel-runtime": "^6.26.0",
"react": "16.7.0",
Expand All @@ -86,4 +87,4 @@
"url": "https://github.com/unbxd/react-search-JS-SDK/issues"
},
"homepage": "https://github.com/unbxd/react-search-JS-SDK#readme"
}
}
8 changes: 6 additions & 2 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const tailwindcss = require('tailwindcss');
module.exports = {
plugins: [require('tailwindcss'), require('autoprefixer')],
};
plugins: [
tailwindcss('./tailwind.config.js'),
require('autoprefixer'),
],
};
Empty file added public/css/Products.scss
Empty file.
1 change: 1 addition & 0 deletions public/css/modules.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import './Products.scss';
42 changes: 31 additions & 11 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,54 @@
import React, { Component } from 'react';
import UnbxdSearchCore from 'unbxd-search-core';
import UnbxdSearch from 'unbxd-search-core';

import { AppContextProvider } from './common/context';
import searchConfigurations from './config';
import '../public/css/index.scss';

class App extends Component {

state = {
unbxdCore: null
setProductConfiguration = (config) => {
const { per_page, requiredFields, variants,
variantCount, variantRequiredFields, groupBy } = config;
this.state.unbxdCore.setNumberOfProducts(per_page);
this.state.unbxdCore.setFields(requiredFields);
this.state.unbxdCore.setIsVariants(variants);
this.state.unbxdCore.setVariantsCount(variantCount);
this.state.unbxdCore.setVariantFields(variantRequiredFields);
this.state.unbxdCore.setVariantsGroupBy(groupBy);
}

unbxdCallBack = (unbxdSearchObj, eventName) => {
if (eventName === 'afterApiCall') {
helpers = { setProductConfiguration: this.setProductConfiguration }

constructor(props) {
super(props)
const { siteName, siteKey } = this.props;

this.state = {
unbxdCore:
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this object required to be on state? Can we keep it as an instance variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So that the changes are passed on to children.

new UnbxdSearch({ ...searchConfigurations, siteName, siteKey, callBackFn: this.unbxdCallBack }),
helpers: this.helpers
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, is this a dynamically changing value to be kept on state? can we just use instance variable instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is dynamic.

};

}

unbxdCallBack = (unbxdSearchObj, eventName) => {
console.log("hello ", eventName)
if (eventName === 'AFTER_API_CALL') {
this.setState({ unbxdCore: unbxdSearchObj })
}
}

componentDidMount() {
const { siteName, siteKey } = this.props;
const searchObj = new UnbxdSearchCore({ ...searchConfigurations, siteName, siteKey }, this.unbxdCallBack);
searchObj.getResults('shoes');
//this.state.unbxdCore.getResults('boots');
//this.state.unbxdCore.getResults('cooking stoves');
this.state.unbxdCore.getResults('red shirt');
//this.state.unbxdCore.getResults('xxxxxxxxxxxxxxx');
}

render() {

const { unbxdCore } = this.state
return (
<AppContextProvider value={unbxdCore}>
<AppContextProvider value={this.state}>
{this.props.children}
</AppContextProvider>
)
Expand Down
8 changes: 8 additions & 0 deletions src/common/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const conditionalRenderer = (children, state, DefaultComponents) => {
const isChildren = children ? true : false;

return isChildren ?
(typeof children === 'function' ?
children(state) : children) :
(DefaultComponents)
}
37 changes: 37 additions & 0 deletions src/modules/Products/GridView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import PropTypes from 'prop-types';

import { GridProductCard } from './ProductCards';
//We need the fieldMap object to map to values

const GridView = (props) => {

const { products = [],
per_row,
fieldMap,
variantMap,
unbxdProductCardClickHandler } = props;

return (<div className={`Unbx-product-container Unbx-grid-view grid grid-cols-${per_row} gap-4 p-4`}>{
Copy link
Contributor

@Anupama-Unbxd Anupama-Unbxd May 29, 2020

Choose a reason for hiding this comment

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

Lets keep the prefix as UNX-. Will these other classes end up in the final rendered HTML?
These classnames look very generic & hence might clash with the CSS rules already defined by the customer

products.map((product) => {
return (<GridProductCard product={product}
fieldMap={fieldMap}
variantMap={variantMap}
per_row={per_row}
key={product.uniqueId}
unbxdProductCardClickHandler={unbxdProductCardClickHandler} />)
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 move the click to the parent so that we have only 1 click handler instead of one per product (if we show 100 products on page, we will have 100 click handlers)

})

}
</div>)
}

GridView.propTypes = {
products: PropTypes.arrayOf(PropTypes.object).isRequired,
per_row: PropTypes.number.isRequired,
Copy link
Contributor

Choose a reason for hiding this comment

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

lets have a standard of camelCase props everywhere

fieldMap: PropTypes.object.isRequired,
variantMap: PropTypes.object.isRequired,
unbxdProductCardClickHandler: PropTypes.func.isRequired
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets keep naming standard for click handlers as per the React standards in the format on<Event>.
For example in this case, it would be onProductClick.

}

export default GridView;
30 changes: 30 additions & 0 deletions src/modules/Products/ListView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import PropTypes from 'prop-types';

import { ListProductCard } from './ProductCards';

const ListView = ({ products = [], fieldMap, isVariant, variantMap, unbxdProductCardClickHandler }) => {

return (<div className={`Unbx-product-container Unbx-grid-view grid grid-cols-1 gap-4 p-4`}>{
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above, lets add prefix as UNX- and try to remove the non-prefixed class names

products.map((product) => {
return (<ListProductCard product={product}
fieldMap={fieldMap}
isVariant={isVariant}
variantMap={variantMap}
key={product.uniqueId}
unbxdProductCardClickHandler={unbxdProductCardClickHandler} />)
})

}
</div>)
}
ListView.propTypes = {
products: PropTypes.arrayOf(PropTypes.object).isRequired,
per_row: PropTypes.number.isRequired,
fieldMap: PropTypes.object.isRequired,
isVariant: PropTypes.bool.isRequired,
variantMap: PropTypes.object.isRequired,
unbxdProductCardClickHandler: PropTypes.func.isRequired
Copy link
Contributor

Choose a reason for hiding this comment

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

same as the GridView props comments above

}

export default ListView;
7 changes: 7 additions & 0 deletions src/modules/Products/NoProducts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

const NoProducts = () => {
return (<div className='Unbx-no-products-container'>Sorry! No products found!</div>)
Copy link
Contributor

Choose a reason for hiding this comment

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

prefix as UNX-

}

export default NoProducts;
37 changes: 37 additions & 0 deletions src/modules/Products/ProductCards/GridProductCard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets keep small case beginning names for folder names. Here it would be src/modules/products/productCards/GridProductCard.js
Only component class file names should begin with a capital letter.

import PropTypes from 'prop-types';

import { productFieldsMapper } from '../utils';

const GridProductCard = ({ product, fieldMap, variantMap, unbxdProductCardClickHandler }) => {

//Get the datas from the product bases on fieldMap and create the card
const productValues = productFieldsMapper(product, fieldMap, variantMap);
Copy link
Contributor

Choose a reason for hiding this comment

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

name the functions with the "action" type present in the name, like mapProductFields or getProductFields or setProductFields. In general, names would mostly be of the form <action><entity> , where action could be get, set, process, map etc.


const {
productName,
imageUrl,
price,
sellingPrice } = productValues;

//Add support for router as a config
return (<div className='Unbx-product-card-container'>
<a href={product.productUrl} className={`Unbx-product-card Unbx-grid-card`} onClick={unbxdProductCardClickHandler}>
<img className='Unbx-product-card Unbx-image' src={imageUrl[0]} />
<p className='Unbx-product-card Unbx-product-name'>{productName}</p>
<p className='Unbx-product-card Unbx-price'>{price}</p>
<p className='Unbx-product-card Unbx-selling-price'>{sellingPrice}</p>
Copy link
Contributor

Choose a reason for hiding this comment

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

as the parent has the -product-card class already, do we need this in all the children elements?

</a>
{/* swatch content */}
</div>)
}

GridProductCard.propTypes = {
product: PropTypes.object.isRequired,
fieldMap: PropTypes.object.isRequired,
isVariant: PropTypes.bool.isRequired,
variantMap: PropTypes.object.isRequired,
unbxdProductCardClickHandler: PropTypes.func.isRequired
}

export default GridProductCard;
37 changes: 37 additions & 0 deletions src/modules/Products/ProductCards/ListProductCard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import PropTypes from 'prop-types';

import { productFieldsMapper } from '../utils';

const ListProductCard = ({ product, fieldMap, variantMap, unbxdProductCardClickHandler }) => {

//Get the datas from the product bases on fieldMap and create the card
const productValues = productFieldsMapper(product, fieldMap, variantMap);

const {
productName,
imageUrl,
price,
sellingPrice } = productValues;

//Add support for router as a config
return (<div className='Unbx-product-card-container'>
<a href={product.productUrl} className={`Unbx-product-card Unbx-grid-card`} onClick={unbxdProductCardClickHandler}>
Copy link
Contributor

Choose a reason for hiding this comment

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

it should be list-card here right instead of grid-card ?

<img className='Unbx-product-card Unbx-image' src={imageUrl[0]} />
<p className='Unbx-product-card Unbx-product-name'>{productName}</p>
<p className='Unbx-product-card Unbx-price'>{price}</p>
<p className='Unbx-product-card Unbx-selling-price'>{sellingPrice}</p>
Copy link
Contributor

Choose a reason for hiding this comment

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

as the parent has the -product-card class already, do we need this in all the children elements?

</a>
{/* swatch content */}
</div>)
}

ListProductCard.propTypes = {
product: PropTypes.object.isRequired,
fieldMap: PropTypes.object.isRequired,
isVariant: PropTypes.bool.isRequired,
variantMap: PropTypes.object.isRequired,
unbxdProductCardClickHandler: PropTypes.func.isRequired
}

export default ListProductCard;
4 changes: 4 additions & 0 deletions src/modules/Products/ProductCards/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import GridProductCard from './GridProductCard';
import ListProductCard from './ListProductCard';

export { GridProductCard, ListProductCard };
34 changes: 34 additions & 0 deletions src/modules/Products/ProductsView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';

import { ProductContextConsumer } from './Context'

import NoProducts from './NoProducts';
import ProductsWrapper from './ProductsWrapper';



const ProductsView = () => {
return (<ProductContextConsumer>{({ isGrid, getSearchResults, ZeroResultsTemplate, ...props }) => {

const { numberOfProducts = 0, products = [] } = getSearchResults() || {};

//return the prop based Zero results template
if (numberOfProducts === 0 && ZeroResultsTemplate) {
return !ZeroResultsTemplate.prototype.render
? ZeroResultsTemplate() : <ZeroResultsTemplate />;
}

//return the default Zero results template
if (numberOfProducts === 0) {
return <NoProducts />;
}

//return the default product results template
//we can make an object of props and destructure it.
return (<ProductsWrapper isGrid={isGrid} products={products} {...props} />)

}}
</ProductContextConsumer>);
}

export default ProductsView;
Loading