Skip to content

Commit 7d73b34

Browse files
author
codingphase
committed
Added redux to our components
1 parent 06fcc97 commit 7d73b34

File tree

16 files changed

+506
-246
lines changed

16 files changed

+506
-246
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@babel/plugin-proposal-decorators": "^7.1.6",
3535
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
3636
"@babel/plugin-transform-async-to-generator": "^7.1.0",
37+
"redux": "^4.0.1",
3738
"uglify-es": "^3.3.9",
3839
"uglifyjs-webpack-plugin": "^1.3.0",
3940
"webpack-dev-server": "^3.1.14"
@@ -67,7 +68,7 @@
6768
"prettier-loader": "^2.1.1",
6869
"react": "^16.6.1",
6970
"react-dom": "^16.6.1",
70-
"react-redux": "^5.1.0",
71+
"react-redux": "^5.1.1",
7172
"sass-loader": "^7.1.0",
7273
"style-loader": "^0.23.1",
7374
"webpack": "^4.25.1",

public/js/dist/CartComponents.js

Lines changed: 276 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/js/dist/CartPopup.js

Lines changed: 0 additions & 138 deletions
This file was deleted.

public/js/dist/CartBtn.js renamed to public/js/dist/ProductAddToCart.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/assets/js/CartBtn.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

resources/assets/js/CartComponents.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import { Provider } from 'react-redux';
4+
import { createStore } from 'redux';
5+
import allReducers from './reducers/allReducers';
6+
import CartBtn from './components/CartBtn';
7+
import CartPopup from './components/CartPopup';
8+
9+
const store = createStore(allReducers);
10+
11+
ReactDOM.render(
12+
<Provider store={store}>
13+
<CartBtn />
14+
</Provider>,
15+
document.getElementById('CartBtnRoot')
16+
);
17+
ReactDOM.render(
18+
<Provider store={store}>
19+
<CartPopup />
20+
</Provider>,
21+
document.getElementById('CartPopupRoot')
22+
);

resources/assets/js/CartProvider.js

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const openingCart = () => {
2+
return {
3+
type: 'OPEN_CART'
4+
};
5+
};
6+
7+
export const closingCart = () => {
8+
return {
9+
type: 'CLOSE_CART'
10+
};
11+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React, { Component } from 'react';
2+
import { connect } from 'react-redux';
3+
import { openingCart } from '../actions/allActions';
4+
5+
class CartBtn extends Component {
6+
constructor() {
7+
super();
8+
this.state = {
9+
name: 'Joe'
10+
};
11+
}
12+
clickedBtn = () => {};
13+
async test() {}
14+
render() {
15+
return (
16+
<a href="#" className="cart-link" onClick={this.props.openingCart}>
17+
Cart<div>4</div>
18+
</a>
19+
);
20+
}
21+
}
22+
23+
const mapStateToProps = state => {
24+
console.log(state);
25+
return state;
26+
};
27+
28+
export default connect(
29+
mapStateToProps,
30+
{
31+
openingCart
32+
}
33+
)(CartBtn);

resources/assets/js/CartPopup.js renamed to resources/assets/js/components/CartPopup.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from 'react';
2-
import ReactDOM from 'react-dom';
2+
import { connect } from 'react-redux';
3+
import { closingCart } from '../actions/allActions';
34

45
class CartPopup extends Component {
56
constructor() {
@@ -8,11 +9,25 @@ class CartPopup extends Component {
89
name: 'Joe'
910
};
1011
}
11-
clickedBtn = () => {};
12-
async test() {}
12+
componentDidUpdate() {
13+
if (this.props.globalState.popupCartOpen == true) {
14+
console.log(this.props.globalState.popupCartOpen);
15+
const cartPopupElement = document.getElementById('cart-popup');
16+
document.addEventListener('click', event => {
17+
var clickedInside = cartPopupElement.contains(event.target);
18+
if (clickedInside) {
19+
} else {
20+
this.props.closingCart();
21+
}
22+
});
23+
}
24+
}
1325
render() {
1426
return (
15-
<section id="cart-popup" className="active">
27+
<section
28+
id="cart-popup"
29+
className={this.props.globalState.popupCartOpen == true ? 'active' : ''}
30+
>
1631
<div className="cart-title">
1732
<div className="title">My Cart</div>
1833
</div>
@@ -103,6 +118,13 @@ class CartPopup extends Component {
103118
}
104119
}
105120

106-
const CartPopupRoot = document.getElementById('CartPopupRoot');
121+
const mapStateToProps = state => {
122+
return state;
123+
};
107124

108-
ReactDOM.render(<CartPopup />, CartPopupRoot);
125+
export default connect(
126+
mapStateToProps,
127+
{
128+
closingCart
129+
}
130+
)(CartPopup);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import React, { Component } from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
class ProductAddToCart extends Component {
5+
constructor() {
6+
super();
7+
this.state = {
8+
selectedSize: '',
9+
sizes: [
10+
4,
11+
4.5,
12+
5,
13+
5.5,
14+
6,
15+
6.5,
16+
7,
17+
7.5,
18+
8,
19+
8.5,
20+
9,
21+
9.5,
22+
10,
23+
10.5,
24+
11,
25+
11.5,
26+
12,
27+
12.5
28+
]
29+
};
30+
}
31+
showAllSizes = () => {
32+
return this.state.sizes.map(size => {
33+
return (
34+
<div
35+
key={size}
36+
className={`option ${
37+
this.state.selectedSize == size ? 'selected' : ''
38+
}`}
39+
onClick={this.clickedSize.bind(null, size)}
40+
>
41+
{size}
42+
</div>
43+
);
44+
});
45+
};
46+
clickedSize = selectedSize => {
47+
this.setState(
48+
{
49+
selectedSize
50+
},
51+
() => {
52+
console.log(this.state);
53+
}
54+
);
55+
};
56+
clickedAddToCartBtn = () => {
57+
if (this.state.selectedSize !== '') {
58+
console.log(`Added Product Size ${this.state.selectedSize}`);
59+
} else {
60+
console.log(`sorry need to add a size`);
61+
}
62+
};
63+
render() {
64+
return (
65+
<div className="add-to-cart">
66+
<div className="detail-section">
67+
<div className="detail">
68+
<input type="checkbox" id="size-dropdown" className="toggle" />
69+
<label className="title size-title" htmlFor="size-dropdown">
70+
Size
71+
</label>
72+
<div className="content">
73+
<div className="sizes">{this.showAllSizes()}</div>
74+
</div>
75+
</div>
76+
</div>
77+
<div className="button-area">
78+
<div className="button" onClick={this.clickedAddToCartBtn}>
79+
Add to cart
80+
</div>
81+
</div>
82+
</div>
83+
);
84+
}
85+
}
86+
87+
ReactDOM.render(
88+
<ProductAddToCart />,
89+
document.getElementById('ProductAddToCartRoot')
90+
);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { combineReducers } from 'redux';
2+
import { appStateReducer } from './appStateReducer';
3+
4+
export default combineReducers({ globalState: appStateReducer });
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const initialState = {
2+
popupCartOpen: false
3+
};
4+
5+
let newState;
6+
7+
export const appStateReducer = (state = initialState, action) => {
8+
switch (action.type) {
9+
case 'OPEN_CART':
10+
newState = Object.assign({}, state, {
11+
popupCartOpen: true
12+
});
13+
return newState;
14+
break;
15+
case 'CLOSE_CART':
16+
newState = Object.assign({}, state, {
17+
popupCartOpen: false
18+
});
19+
return newState;
20+
break;
21+
22+
default:
23+
return state;
24+
break;
25+
}
26+
};

resources/views/layouts/main.edge

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929

3030

3131

32-
33-
@!section('javascript')
3432
<script src="/js/dist/vendors.js"></script>
35-
<script src="/js/dist/CartBtn.js"></script>
36-
<script src="/js/dist/CartPopup.js"></script>
33+
@!section('javascript')
34+
35+
<script src="/js/dist/CartComponents.js"></script>
3736
</body>
3837
</html>

0 commit comments

Comments
 (0)