-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
1,590 additions
and
603 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,65 @@ | ||
import '../index.css'; | ||
import React, { Component } from 'react'; | ||
import React, { Component, Fragment } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'; | ||
import propTypes from 'prop-types'; | ||
import { checkAuthStatus } from '../actions'; | ||
import PropTypes from 'prop-types'; | ||
import { checkAuthStatus, getCart } from '../actions'; | ||
import HomePage from './HomePage'; | ||
import LoginPage from './LoginPage'; | ||
import Signup from './SignupPage'; | ||
import Menu from './MenuPage'; | ||
import CartPage from './Cart'; | ||
import NotFoundPage from './NotFoundPage'; | ||
import Loader from './Loader'; | ||
import Nav from './Nav'; | ||
|
||
export class App extends Component { | ||
componentDidMount() { | ||
const { checkAuthStatus: checkUserAuthStatus } = this.props; | ||
checkUserAuthStatus(); | ||
async componentDidMount() { | ||
const { checkAuthStatus: checkUserAuthStatus, getCart: getCartItems } = this.props; | ||
await checkUserAuthStatus(); | ||
const { isLoggedIn } = this.props; | ||
if (isLoggedIn) getCartItems(); | ||
} | ||
|
||
render() { | ||
const { loading } = this.props; | ||
return ( | ||
<Router> | ||
<Switch> | ||
<Route path="/" exact component={HomePage} /> | ||
<Route path="/login" component={LoginPage} /> | ||
<Route path="/signup" component={Signup} /> | ||
<Route path="/menu" component={Menu} /> | ||
<Route component={NotFoundPage} /> | ||
</Switch> | ||
<Fragment> | ||
{loading && <Loader />} | ||
<Nav /> | ||
<Switch> | ||
<Route path="/" exact component={HomePage} /> | ||
<Route path="/login" component={LoginPage} /> | ||
<Route path="/signup" component={Signup} /> | ||
<Route path="/menu" component={Menu} /> | ||
<Route path="/cart" component={CartPage} /> | ||
<Route component={NotFoundPage} /> | ||
</Switch> | ||
</Fragment> | ||
</Router> | ||
); | ||
} | ||
} | ||
|
||
App.propTypes = { | ||
checkAuthStatus: propTypes.func.isRequired, | ||
checkAuthStatus: PropTypes.func.isRequired, | ||
getCart: PropTypes.func.isRequired, | ||
isLoggedIn: PropTypes.bool, | ||
loading: PropTypes.bool, | ||
}; | ||
|
||
App.defaultProps = { | ||
isLoggedIn: false, | ||
loading: false, | ||
}; | ||
|
||
const mapStateToProps = state => ({ | ||
isLoggedIn: state.user.isLoggedIn, | ||
loading: state.fetching.fetching, | ||
}); | ||
|
||
export default connect( | ||
null, | ||
{ checkAuthStatus }, | ||
mapStateToProps, | ||
{ checkAuthStatus, getCart }, | ||
)(App); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { Component, Fragment } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import CheckOutCard from './CheckOutCard'; | ||
import { removeFromCart, checkout } from '../actions'; | ||
|
||
export class Cart extends Component { | ||
onCheckoutClick = async () => { | ||
const { cart, checkout: placeOrder, history } = this.props; | ||
const foodItems = Object.keys(cart).map(Number); | ||
await placeOrder(foodItems); | ||
const { | ||
fetching: { fetching, errorMessage }, | ||
} = this.props; | ||
if (!fetching && !errorMessage) return history.push('/'); | ||
return undefined; | ||
}; | ||
|
||
onRemoveClick = (foodId) => { | ||
const { removeFromCart: removeItemFromCart } = this.props; | ||
removeItemFromCart(foodId); | ||
}; | ||
|
||
render() { | ||
const { cart } = this.props; | ||
|
||
const cartItems = Object.keys(cart) | ||
.map(Number) | ||
.map(item => ({ | ||
id: item, | ||
foodName: cart[item].foodName, | ||
foodPrice: cart[item].foodPrice, | ||
})); | ||
|
||
return ( | ||
<div className="wrapper"> | ||
<h2>Items To Be Purchased</h2> | ||
{cartItems.length ? ( | ||
<Fragment> | ||
<section className="container checkout"> | ||
{cartItems.map(item => ( | ||
<CheckOutCard | ||
key={item.id} | ||
id={item.id} | ||
foodName={item.foodName} | ||
foodPrice={item.foodPrice} | ||
buttonCallback={() => this.onRemoveClick(item.id)} | ||
/> | ||
))} | ||
</section> | ||
<div className="order-price"> | ||
<p> | ||
<strong>total price</strong> | ||
: ₦ | ||
<span id="order-price"> | ||
{cartItems | ||
.map(item => item.foodPrice) | ||
.reduce((a, b) => a + b) | ||
.toLocaleString()} | ||
</span> | ||
</p> | ||
</div> | ||
<button | ||
type="button" | ||
className="btn-primary" | ||
id="checkout" | ||
onClick={this.onCheckoutClick} | ||
> | ||
Checkout | ||
</button> | ||
</Fragment> | ||
) : ( | ||
<section className="container checkout"> | ||
<div>No Items in cart</div> | ||
</section> | ||
)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
Cart.propTypes = { | ||
cart: PropTypes.instanceOf(Object).isRequired, | ||
removeFromCart: PropTypes.func.isRequired, | ||
checkout: PropTypes.func.isRequired, | ||
history: PropTypes.oneOfType([PropTypes.object, PropTypes.func]).isRequired, | ||
fetching: PropTypes.instanceOf(Object).isRequired, | ||
}; | ||
|
||
const mapStateToProps = state => ({ | ||
cart: state.cart, | ||
fetching: state.fetching, | ||
}); | ||
|
||
export default connect( | ||
mapStateToProps, | ||
{ removeFromCart, checkout }, | ||
)(Cart); |
Oops, something went wrong.