Skip to content

Commit

Permalink
styles are now components
Browse files Browse the repository at this point in the history
  • Loading branch information
sharankarthikyan committed Jun 13, 2021
1 parent 7567f70 commit cd10598
Show file tree
Hide file tree
Showing 85 changed files with 1,604 additions and 1,315 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"redux-logger": "^3.0.6",
"redux-persist": "^6.0.0",
"reselect": "^4.0.0",
"styled-components": "^5.3.0",
"web-vitals": "^1.0.1"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
body {
font-family: "Open Sans Condensed", sans-serif;
padding: 20px 60px;
font-family: 'Open Sans Condensed';
padding: 20px 40px;
}

a {
Expand Down
92 changes: 33 additions & 59 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import React from "react";
import { Switch, Route, Redirect } from "react-router-dom";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import React from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';

import "./App.css";
import './App.css';

import HomePage from "./pages/homepage/homepage.component";
import ShopPage from "./pages/shop/shop.component";
import CheckoutPage from "./pages/checkout/checkout.component";
import HomePage from './pages/homepage/homepage.component';
import ShopPage from './pages/shop/shop.component';
import SignInAndSignUpPage from './pages/sign-in-and-sign-up/sign-in-and-sign-up.component';
import CheckoutPage from './pages/checkout/checkout.component';

import Header from "./components/header/header.component";
import SignInAndSignUpPage from "./pages/sign-in-and-sign-up/sign-in-and-sign-up.component";
import { auth, createUserProfileDocument } from "./firebase/firebase.utils";
import { setCurrentUser } from "./redux/user/user.actions";
import { selectCurrentUser } from "./redux/user/user.selectors";
import Header from './components/header/header.component';

import { auth, createUserProfileDocument } from './firebase/firebase.utils';

import { setCurrentUser } from './redux/user/user.actions';
import { selectCurrentUser } from './redux/user/user.selectors';

class App extends React.Component {
unsubscribeFromAuth = null;

componentDidMount() {
const { setCurrentUser } = this.props;
this.unsubscribeFromAuth = auth.onAuthStateChanged(async (userAuth) => {
// If you signout, userAuth value will be null.

this.unsubscribeFromAuth = auth.onAuthStateChanged(async userAuth => {
if (userAuth) {
const userRef = await createUserProfileDocument(userAuth);

userRef.onSnapshot((snapShot) => {
// onSnapshot mothod gives the actual data.
userRef.onSnapshot(snapShot => {
setCurrentUser({
id: snapShot.id,
...snapShot.data(),
...snapShot.data()
});
});
} else {
setCurrentUser(userAuth);
}

setCurrentUser(userAuth);
});
}

Expand All @@ -47,15 +48,15 @@ class App extends React.Component {
<div>
<Header />
<Switch>
<Route exact path="/" component={HomePage} />
<Route path="/shop" component={ShopPage} />
<Route exact path="/checkout" component={CheckoutPage} />
<Route exact path='/' component={HomePage} />
<Route path='/shop' component={ShopPage} />
<Route exact path='/checkout' component={CheckoutPage} />
<Route
exact
path="/signin"
path='/signin'
render={() =>
this.props.currentUser ? (
<Redirect to="/" />
<Redirect to='/' />
) : (
<SignInAndSignUpPage />
)
Expand All @@ -68,41 +69,14 @@ class App extends React.Component {
}

const mapStateToProps = createStructuredSelector({
currentUser: selectCurrentUser,
currentUser: selectCurrentUser
});

// This is also a another convention of writting selector without createStructuredSelector.
// const mapStateToProps = (state) => ({
// currentUser: selectCurrentUser(state),
// });

// Below are general mapStateToProps in redux.
// const mapStateToProps = ({ user }) => {
// return {
// currentUser: user.currentUser,
// };
// };

// The above and below mapStateToProps code are same.
// const mapStateToProps = (state) => {
// return {
// currentUser: state.user.currentUser,
// };
// };

// The below are mapDispatchToProps
const mapDispatchToProps = (dispatch) => ({
setCurrentUser: (user) => dispatch(setCurrentUser(user)),
const mapDispatchToProps = dispatch => ({
setCurrentUser: user => dispatch(setCurrentUser(user))
});

// The below code is same as above. Syntax only the difference.
// const mapDispatchToProps = (dispatch) => {
// return {
// setCurrentUser: (user) => {
// return dispatch(setCurrentUser(user));
// },
// };
// };

export default connect(mapStateToProps, mapDispatchToProps)(App);
// First argument is null because, there is no mapStateToProps.
export default connect(
mapStateToProps,
mapDispatchToProps
)(App);
11 changes: 6 additions & 5 deletions src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { render, screen } from '@testing-library/react';
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});
73 changes: 33 additions & 40 deletions src/components/cart-dropdown/cart-dropdown.component.jsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
import React from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { withRouter } from "react-router-dom";
import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { withRouter } from 'react-router-dom';

import CustomButton from "../custom-button/custom-button.component";
import CartItem from "../cart-item/cart-item.component";
import { selectCartItems } from "../../redux/cart/cart.selectors";
import { toggleCartHidden } from "../../redux/cart/cart.actions";
import CartItem from '../cart-item/cart-item.component';
import { selectCartItems } from '../../redux/cart/cart.selectors';
import { toggleCartHidden } from '../../redux/cart/cart.actions.js';

import "./cart-dropdown.styles.scss";
import {
CartDropdownContainer,
CartDropdownButton,
EmptyMessageContainer,
CartItemsContainer
} from './cart-dropdown.styles';

const CartDropdown = ({ cartItems, history, dispatch }) => {
return (
<div className="cart-dropdown">
const CartDropdown = ({ cartItems, history, dispatch }) => (
<CartDropdownContainer>
<CartItemsContainer>
{cartItems.length ? (
<div className="cart-items">
{cartItems.map((cartItem) => (
<CartItem key={cartItem.id} item={cartItem} />
))}
</div>
cartItems.map(cartItem => (
<CartItem key={cartItem.id} item={cartItem} />
))
) : (
<span className="empty-message">Your cart is empty</span>
<EmptyMessageContainer>Your cart is empty</EmptyMessageContainer>
)}
<CustomButton
onClick={() => {
history.push("/checkout");
dispatch(toggleCartHidden());
}}
>
GO TO CHECKOUT
</CustomButton>
</div>
);
};
</CartItemsContainer>
<CartDropdownButton
onClick={() => {
history.push('/checkout');
dispatch(toggleCartHidden());
}}
>
GO TO CHECKOUT
</CartDropdownButton>
</CartDropdownContainer>
);

const mapStateToProps = createStructuredSelector({
cartItems: selectCartItems,
cartItems: selectCartItems
});

// This below is also a selector without createStructuredSelector.
// const mapStateToProps = (state) => ({
// cartItems: selectCartItems(state),
// });

// This below is normal mapStateToProps in redux without selectors.
// const mapStateToProps = ({ cart: { cartItems } }) => ({ // We used advanced destructuring to cartItems from cart.
// cartItems,
// });

export default withRouter(connect(mapStateToProps)(CartDropdown));
32 changes: 32 additions & 0 deletions src/components/cart-dropdown/cart-dropdown.styles.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import styled from 'styled-components';
import CustomButton from '../custom-button/custom-button.component';

export const CartDropdownContainer = styled.div`
position: absolute;
width: 240px;
height: 340px;
display: flex;
flex-direction: column;
padding: 20px;
border: 1px solid black;
background-color: white;
top: 90px;
right: 40px;
z-index: 5;
`;

export const CartDropdownButton = styled(CustomButton)`
margin-top: auto;
`;

export const EmptyMessageContainer = styled.span`
font-size: 18px;
margin: 50px auto;
`;

export const CartItemsContainer = styled.div`
height: 240px;
display: flex;
flex-direction: column;
overflow: scroll;
`;
30 changes: 0 additions & 30 deletions src/components/cart-dropdown/cart-dropdown.styles.scss

This file was deleted.

59 changes: 28 additions & 31 deletions src/components/cart-icon/cart-icon.component.jsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import React from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";

import { toggleCartHidden } from "../../redux/cart/cart.actions";
import { selectCartItemsCount } from "../../redux/cart/cart.selectors";

import { ReactComponent as ShoppingIcon } from "../../assets/shopping-bag.svg";

import "./cart-icon.styles.scss";

const CartIcon = ({ toggleCartHidden, itemCount }) => {
// toggleCartHidden coming from mapDispatchToProps
return (
<div className="cart-icon" onClick={toggleCartHidden}>
<ShoppingIcon className="shopping-icon" />
<span className="item-count">{itemCount}</span>
</div>
);
};

const mapStateToProps = createStructuredSelector({
itemCount: selectCartItemsCount,
import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';

import { toggleCartHidden } from '../../redux/cart/cart.actions';
import { selectCartItemsCount } from '../../redux/cart/cart.selectors';

import {
CartContainer,
ShoppingIcon,
ItemCountContainer
} from './cart-icon.styles';

const CartIcon = ({ toggleCartHidden, itemCount }) => (
<CartContainer onClick={toggleCartHidden}>
<ShoppingIcon />
<ItemCountContainer>{itemCount}</ItemCountContainer>
</CartContainer>
);

const mapDispatchToProps = dispatch => ({
toggleCartHidden: () => dispatch(toggleCartHidden())
});

// This below is also a selector without createStructuredSelector.
// const mapStateToProps = (state) => ({
// itemCount: selectCartItemsCount(state),
// });

const mapDispatchToProps = (dispatch) => ({
toggleCartHidden: () => dispatch(toggleCartHidden()),
const mapStateToProps = createStructuredSelector({
itemCount: selectCartItemsCount
});

export default connect(mapStateToProps, mapDispatchToProps)(CartIcon);
export default connect(
mapStateToProps,
mapDispatchToProps
)(CartIcon);
Loading

0 comments on commit cd10598

Please sign in to comment.