Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f5ac40c
checkout route created, export withLayout
LuisMM24 Mar 7, 2022
41a18a0
disabled if cartItems are empty
LuisMM24 Mar 7, 2022
25d2840
basic html structure of checkout
LuisMM24 Mar 7, 2022
d9941d3
colors pallete created, step redirect created
LuisMM24 Mar 7, 2022
10b42f2
styling progress bar
LuisMM24 Mar 7, 2022
6306ecc
changed names of context, starting with personal info form
LuisMM24 Mar 8, 2022
cb083f1
form personal info done, checkoutProvider in progress, progressBar co…
LuisMM24 Mar 8, 2022
3c417c9
progress width
LuisMM24 Mar 8, 2022
31643c3
step back btn working
LuisMM24 Mar 8, 2022
dee5802
overview created
LuisMM24 Mar 8, 2022
9330dc4
shipping form created
LuisMM24 Mar 8, 2022
0b500ad
few styles in inputs, country changed to select
LuisMM24 Mar 9, 2022
05c356b
overview not showing in payment and review
LuisMM24 Mar 9, 2022
3bb06e1
payment formulary, creditCard template WIP
LuisMM24 Mar 9, 2022
df94fe7
card positioned in paymentForm
LuisMM24 Mar 10, 2022
d449f94
payment with creditCard animation working
LuisMM24 Mar 10, 2022
50e82f1
half review styling
LuisMM24 Mar 10, 2022
1689a40
review step, refactor of context, localStorage checkout
LuisMM24 Mar 11, 2022
0ae90e8
form folders refact,
LuisMM24 Mar 11, 2022
7a03a09
bug opened in checkout form local storage
LuisMM24 Mar 12, 2022
ebdfc92
localstorage bug done
LuisMM24 Mar 13, 2022
2289199
rework in localStorage
LuisMM24 Mar 14, 2022
17b0213
country bug fix
LuisMM24 Mar 14, 2022
d8f1ebf
temp localStorage
LuisMM24 Mar 14, 2022
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = {
},
],
rules: {
"prettier/prettier": "error",
"prettier/prettier": 0,
"react/jsx-filename-extension": "off",
"import/prefer-default-export": "off",
"prefer-destructuring": "off",
Expand Down
39,114 changes: 36,859 additions & 2,255 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
"prop-types": "^15.7.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-phone-input-2": "^2.15.0",
"react-redux": "^7.2.3",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"sass": "^1.32.11",
"sass": "^1.49.9",
"uuid": "^8.3.2",
"web-vitals": "^1.1.1",
"yup": "^0.32.9"
Expand Down
25 changes: 22 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React, { useState, useEffect } from "react";
import { BrowserRouter, Switch, Route } from "react-router-dom";

// context
import CheckoutContextProvider from "./components/CheckoutContextProvider"
import Home from "./pages/Home";
import NewProduct from "./pages/NewProduct";

import * as api from "./api";

import useLocalStorage from "./hooks/useLocalStorage";
import loadLocalStorageItems from "./utils/loadLocalStorageItems";
import Checkout from "./pages/Checkout/Checkout";
import OverviewContextProvider from "./components/OverviewContextProvider/OverviewContextProvider";

function buildNewCartItem(cartItem) {
if (cartItem.quantity >= cartItem.unitsInStock) {
Expand Down Expand Up @@ -116,7 +120,7 @@ function App() {
if (
product.id === productId &&
product.votes.downVotes.currentValue <
product.votes.downVotes.lowerLimit
product.votes.downVotes.lowerLimit
) {
return {
...product,
Expand Down Expand Up @@ -178,7 +182,6 @@ function App() {
function saveNewProduct(newProduct) {
setProducts((prevState) => [newProduct, ...prevState]);
}

return (
<BrowserRouter>
<Switch>
Expand All @@ -201,8 +204,24 @@ function App() {
handleChange={handleChange}
/>
</Route>

<Route path="/checkout">

<OverviewContextProvider
cartItems={cartItems}
setCartItems={setCartItems}
handleChange={handleChange}
handleRemove={handleRemove}
>
<CheckoutContextProvider>
<Checkout fullWidth />
</CheckoutContextProvider>
</OverviewContextProvider>

</Route>

</Switch>
</BrowserRouter>
</BrowserRouter >
);
}

Expand Down
Binary file added src/assets/img/americanExpress.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/applePay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/countryFlags/andorra.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/countryFlags/england.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/countryFlags/german.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/countryFlags/spain.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/countryFlags/sweden.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/creditChip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/mastercard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/paypalPay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/visa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/img/worldCard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions src/assets/styles/_layout.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.mflex {
display: flex;
}
.mrow {
flex-direction: row;
}
.mcol {
flex-direction: column;
}
.mjustify-center {
justify-content: center;
}
.mjustify-around {
justify-content: space-around;
}
.mjustify-between {
justify-content: space-between;
}
.malign-center {
align-items: center;
}

.mnoButton {
outline: none;
background-color: inherit;
border: none;
}

.bolder {
font-weight: bold;
}
10 changes: 10 additions & 0 deletions src/assets/styles/_variables.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$darkPink: #f72585;
$pink: #b5179e;
$purple: #7209b7;
$darkPurple: #560bad;
$purpledBlue: #480ca8;
$lightPurpledBlue: #3a0ca3;
$darkBlue: #3f37c9;
$blue: #4361ee;
$lightBlue: #4895ef;
$skyBlue: #4cc9f0;
14 changes: 11 additions & 3 deletions src/components/Cart/Cart.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import React from "react";

import { useHistory } from "react-router-dom";
import ShoppingCartItem from "../ShoppingCartItem";
import Button from "../Button";

function getCartTotal(cart) {

export function getCartTotal(cart) {
return cart.reduce((accum, item) => {
return accum + item.price * item.quantity;
}, 0);
}

function Cart({ cartItems, handleRemove, handleChange, ...props }) {
const history = useHistory();

const goToCheckout = () => {
history.push("/checkout/step-1")
}

return (
<aside {...props}>
<div className="row flex-column">
<div className="row flex-column" data-testid="Cart">
<div className="col shopping__cart__header">
<h2 className="h3 mt-2">Shopping Cart</h2>
<hr className="mb-3" />
Expand Down Expand Up @@ -49,7 +57,7 @@ function Cart({ cartItems, handleRemove, handleChange, ...props }) {
<hr />
</div>
<div className="col">
<Button>Checkout</Button>
<Button disabled={cartItems.length === 0} onClick={goToCheckout} >Checkout</Button>
</div>
</div>
</div>
Expand Down
10 changes: 10 additions & 0 deletions src/components/Cart/cart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";
import { render, screen } from '@testing-library/react'
import Cart from "./Cart"

describe("Given a Cart component", () => {
test("Then should render", () => {
render(<Cart />)
expect(screen.getByTestId("Cart")).toBeInTheDocument()
})
})
147 changes: 147 additions & 0 deletions src/components/CheckoutContextProvider/CheckoutContextProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import React, { useReducer, useContext, useEffect } from "react";
import { useHistory } from "react-router-dom";
// uuid
import { v4 as uuid } from "uuid"
// context
import { CheckoutContext } from "../../context/CheckoutContext";
import { OverviewContext } from "../../context/OverviewContext";

const initialState = {
personalInfo: {},
billingAddress: {},
payment: {},
orderID: uuid(),
actualStep: 1,
temporaryStorage: true
}


function checkoutReducer(state, action) {
const { type, nameForm, payload } = action
switch (type) {
case "FINISH_CHECKOUT":
return {
...state,
actualStep: 1,
temporaryStorage: false

}
case "SET_INFO":
// sets the info dynamiclly, and sums 1 to go for next step
return {
...state,
actualStep: state.actualStep + 1,
[nameForm]: payload
}
case "SET_STEP":
return {
...state,
actualStep: payload
}
default:
return {
...state
}
}
}

const checkInitialState = () => {
if (localStorage.getItem("checkoutInfo")) {
return {
...JSON.parse(localStorage.getItem("checkoutInfo")),
actualStep: 1
}
}
// temporal checkout for user refresh
if (localStorage.getItem("tempCheckout")) {
return JSON.parse(localStorage.getItem("tempCheckout"))
}
return initialState
}

export default function CheckoutContextProvider({ children }) {
const [checkoutState, dispatch] = useReducer(
checkoutReducer, checkInitialState()
)
const { setCartItems } = useContext(OverviewContext)
const history = useHistory()
const {
personalInfo,
billingAddress,
payment,
actualStep,
temporaryStorage,
orderID
} = checkoutState

useEffect(() => {
if (temporaryStorage) {
return localStorage.setItem("tempCheckout", JSON.stringify(checkoutState))
}
return localStorage.removeItem("tempCheckout")
}, [checkoutState])

useEffect(() => {
const location = history.location.pathname.split("/")[2]
// const step = parseInt(location.substring(5, 6), 10)
if (location !== `step-${actualStep}`) {
history.push(`/checkout/step-${actualStep}`)
}

}, [])


const setFormInfo = (nameForm, data) => {
dispatch({
type: "SET_INFO",
nameForm: nameForm,
payload: data
})
}

const setStep = (step) => {
dispatch({
type: "SET_STEP",
payload: step
})

return history.push(`/checkout/step-${step}`)
}

const setCheckoutDone = (saveUserInfo) => {
if (saveUserInfo) {
localStorage.setItem("checkoutInfo", JSON.stringify({
personalInfo,
billingAddress,
payment
})
)
} else {
localStorage.removeItem("checkoutInfo")
}
dispatch({
type: "FINISH_CHECKOUT",
payload: saveUserInfo
})
/* this state returns us to home, because condition in overview,
if cart is empty, go back to home
*/
setCartItems([])

}

return (
<CheckoutContext.Provider value={{
setFormInfo: setFormInfo,
setStep: setStep,
setCheckoutDone: setCheckoutDone,
orderID: orderID,
personalInfo: personalInfo,
billingAddress: billingAddress,
payment: payment,
actualStep: checkoutState.actualStep
}}>
{children}
</CheckoutContext.Provider>
)
}
1 change: 1 addition & 0 deletions src/components/CheckoutContextProvider/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./CheckoutContextProvider"
Loading