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

Excercices #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
43 changes: 37 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import './App.css';
import { connect } from 'react-redux';
import { addProduct } from './actions';
import { addProduct, removeProduct, updateProductsList } from './actions';
import Chance from 'chance';
export const chance = Chance();

Expand All @@ -22,14 +22,45 @@ const mapStateToProps = state => {

const mapDispatchToProps = {
add: addProduct,
remove : removeProduct,
update : updateProductsList
};

const Product = (props) => <div>{props.name}</div>;
const Product = (props) => <div><RemoveButton name={props.name} {...props}/> {props.name}</div>;

const DaBest = ({name}) => <h1>The Best: {name}</h1>;

const AdderButton = ({add}) => <button onClick={ () => add({ name: 'Sofa' }) }>Add Sofa</button>
const AdderButton = ({add}) => <button onClick={ () => add({ name: 'Sofa' }) }>Add Sofa</button>;

// const RemoveButton = ({remove, name}) => <button onClick={ () => remove({ name: 'Sofa' }) }>remove item</button>

const RemoveButton = ({name, remove}) => <button onClick={ () => remove({ name }) }>remove item</button>;

const SearchBar = ({ update }) => <input type="text" placeholder="search" onChange={ evt => update({ searchValue: evt.target.value }) }/>;

const AdderProduct = ({add}) => {
let productName = '';
let productDepartment = '';
let productPrice = '';
let productStock = 0;
return (
<div>
<input className="productNameInput" type="text" placeholder="Enter the product name" onChange={ (evt) => productName = evt.target.value}></input>
<input type="text" placeholder="Enter the product department" onChange={ (evt) => productDepartment= evt.target.value}></input>
<input type="text" placeholder="Enter the product price" onChange={ (evt) => productPrice= evt.target.value}></input>
<input type="text" placeholder="Enter the product stock" onChange={ (evt) => productStock= Number(evt.target.value)}></input>
<button type="submit" onClick={ () => {
if(productName !== "") {
return add({ name: productName, department: productDepartment, price: productPrice, stock: productStock })
}

{/*document.querySelector('.productNameInput').value('');*/}
} }>Add item</button>
</div>
);
};

// const CreateNewItem =
class App extends Component {


Expand All @@ -49,13 +80,13 @@ class App extends Component {

render() {
const { products, add, whoIsTheBest } = this.props;
debugger;
return (
<div>
<SearchBar { ...this.props }/>
<DaBest name={whoIsTheBest} />
{products.map(product => <Product name={product.name} key={product.id} />)}
{products.map(product => <Product name={product.name} key={product.id} { ...this.props }/>)}

<AdderButton { ...this.props } />
<AdderProduct { ...this.props }/>
</div>
);
}
Expand Down
21 changes: 20 additions & 1 deletion src/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
export const ACTION_TYPES = {
addProduct: 'ADD_PRODUCTS',
removeProduct : 'REMOVE_PRODUCTS',
updateProductsList : 'UPDATE_PRODUCTS_LIST'
};

export function addProduct(product) {
debugger;
return {
type: ACTION_TYPES.addProduct,
payload: {
product,
}
}
}

export function removeProduct(product) {
return {
type: ACTION_TYPES.removeProduct,
payload: {
product,
}
}
}

export function updateProductsList(searchValue) {
return {
type: ACTION_TYPES.updateProductsList,
payload: {
searchValue,
}
}
}
7 changes: 5 additions & 2 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ const INITIAL_STATE = {
};

export const products = (state = INITIAL_STATE.products, { type, payload }) => {
debugger;
console.log(payload)
switch (type) {
case ACTION_TYPES.addProduct:
debugger;
return [...state, payload.product];
case ACTION_TYPES.removeProduct:
return [...state.filter( prod => prod.name !== payload.product.name)]
case ACTION_TYPES.updateProductsList:
return [...state.filter( prod => prod.name.includes(payload.searchValue.searchValue))]
}

return state;
Expand Down