Skip to content

Commit d22666d

Browse files
Fix the issue of persistent state values in payment page by removing it while component will unmount
1 parent c251644 commit d22666d

27 files changed

+213
-110
lines changed

client/src/actions/index.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import {
33
HANDLE_SIGN_UP,
44
HANDLE_SIGN_UP_ERROR,
55
HANDLE_SIGN_OUT,
6-
HANDLE_TOKEN_ID,
76
HANDLE_SIGN_IN_ERROR,
87
LOAD_FILTER_PRODUCTS,
98
LOAD_FILTER_ATTRIBUTES,
10-
INTERNAL_SERVER_ERROR_CODE,
11-
BAD_REQUEST_ERROR_CODE,
129
SAVE_QUERY_STATUS,
1310
SHIPPING_ADDRESS_CONFIRMED,
14-
PAYMENT_INFO_CONFIRMED, PAYMENT_RESPONSE,
11+
PAYMENT_INFO_CONFIRMED,
12+
PAYMENT_RESPONSE, CART_TOTAL, LOAD_SHOPPING_BAG_PRODUCTS
1513
} from './types';
14+
import {INTERNAL_SERVER_ERROR_CODE, BAD_REQUEST_ERROR_CODE} from '../constants/http_error_codes'
15+
import {SHOPPERS_PRODUCT_INFO_COOKIE, CART_TOTAL_COOKIE, TOKEN_ID_COOKIE} from '../constants/cookies'
1616
import history from "../history";
1717
import {Base64} from 'js-base64';
1818
import Cookies from 'js-cookie';
1919
import log from "loglevel";
20-
import {commonServiceAPI, authServiceAPI, paymentServiceAPI} from "../api/service_api";
20+
import {commonServiceAPI, authServiceAPI} from "../api/service_api";
2121
import axios from 'axios';
2222

2323
export const setTokenFromCookie = tokenId => {
@@ -59,7 +59,7 @@ export const signIn = formValues => async (dispatch) => {
5959
if (response.data.jwt) {
6060
log.info(`[ACTION]: dispatch HANDLE_SIGN_IN response.data.jwt = ${response.data.jwt}`)
6161
dispatch({type: HANDLE_SIGN_IN, payload: response.data.jwt});
62-
Cookies.set(HANDLE_TOKEN_ID, response.data.jwt, {expires: 7});
62+
Cookies.set(TOKEN_ID_COOKIE, response.data.jwt, {expires: 7});
6363
history.push('/');
6464
} else {
6565
log.info(`[ACTION]: dispatch HANDLE_SIGN_IN_ERROR response.data.error = ${response.data.error}`)
@@ -70,7 +70,7 @@ export const signIn = formValues => async (dispatch) => {
7070

7171
export const signOut = () => {
7272
log.info(`[ACTION]: signOut Cookie is removed...`)
73-
Cookies.remove(HANDLE_TOKEN_ID)
73+
Cookies.remove(TOKEN_ID_COOKIE)
7474
return {
7575
type: HANDLE_SIGN_OUT
7676
}
@@ -111,26 +111,41 @@ export const sendPaymentToken = (token) => async dispatch => {
111111
headers: {
112112
'Content-Type': 'application/json'
113113
},
114-
data : JSON.stringify(token)
114+
data: JSON.stringify(token)
115115
};
116116

117117
axios(config)
118118
.then(function (response) {
119119
console.log(JSON.stringify(response.data));
120-
dispatch({
121-
type: PAYMENT_RESPONSE,
122-
payload: JSON.stringify(response.data)
123-
})
120+
let paymentResponse = {...response.data,
121+
last4: token.card.last4, exp_year: token.card.exp_year,
122+
exp_month: token.card.exp_month, brand: token.card.brand}
124123

125-
if(response.data.paymentFailed) {
124+
if (paymentResponse.payment_failed) {
126125
history.push(`/checkout/cancel-payment/${response.data.charge_id}`)
127126
} else {
128127
history.push(`/checkout/success-payment/${response.data.charge_id}`)
128+
Cookies.remove(CART_TOTAL_COOKIE)
129+
Cookies.remove(SHOPPERS_PRODUCT_INFO_COOKIE)
130+
131+
dispatch({
132+
type: CART_TOTAL,
133+
payload: 0
134+
})
129135
}
130136

137+
dispatch({
138+
type: PAYMENT_RESPONSE,
139+
payload: paymentResponse
140+
})
141+
131142
})
132143
.catch(function (error) {
133144
console.log(error);
145+
dispatch({
146+
type: PAYMENT_RESPONSE,
147+
payload: {error: true}
148+
})
134149
});
135150
}
136151

@@ -160,7 +175,7 @@ export const getDataViaAPI = (type, uri) => async dispatch => {
160175
{isLoading: false, data: JSON.parse(JSON.stringify(response.data))}
161176
});
162177
if (LOAD_FILTER_PRODUCTS.localeCompare(type) === 0) {
163-
if(window.location.search.localeCompare(uri.split("/products")[1]) !== 0) {
178+
if (window.location.search.localeCompare(uri.split("/products")[1]) !== 0) {
164179
history.push(uri)
165180
}
166181
}
@@ -185,8 +200,10 @@ export const loadFilterAttributes = filterQuery => async dispatch => {
185200
dispatch({
186201
type: LOAD_FILTER_ATTRIBUTES,
187202
payload: JSON.parse(JSON.stringify(
188-
{...response.data,
189-
"query": removedSpacesFromFilterQuery.slice(3)}))
203+
{
204+
...response.data,
205+
"query": removedSpacesFromFilterQuery.slice(3)
206+
}))
190207
});
191208

192209
dispatch({

client/src/actions/types.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,3 @@ export const SHIPPING_OPTION_CONFIRMED = "SHIPPING_OPTION_CONFIRMED";
3030
export const PAYMENT_INFO_CONFIRMED = "PAYMENT_INFO_CONFIRMED";
3131
export const PAYMENT_RESPONSE = "PAYMENT_RESPONSE";
3232

33-
34-
// js cookie IDs
35-
export const HANDLE_TOKEN_ID = "HANDLE_TOKEN_ID";
36-
export const SHOPPERS_PRODUCT_ID = 'SHOPPERS_PRODUCT_ID';
37-
38-
// http error codes
39-
export const INTERNAL_SERVER_ERROR_CODE = 500;
40-
export const BAD_REQUEST_ERROR_CODE = 400;
41-
42-
// API URL
43-
export const HOME_PAGE_DATA_API = "/home";
44-
export const TABS_DATA_API = "/tabs";
45-
export const PRODUCT_BY_ID_DATA_API = "/products?product_id=";
46-
export const PRODUCT_BY_CATEGORY_DATA_API = "/products?q=";
47-
48-
// attributes length
49-
export const HOME_PAGE_API_OBJECT_LEN = 3;
50-
export const TABS_API_OBJECT_LEN = 6;

client/src/components/routes/cancelPayment.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react'
22
import log from 'loglevel'
33
import {Grid} from "@material-ui/core";
4+
import {Link} from "react-router-dom";
5+
import {CHECKOUT_ROUTE, HOME_ROUTE} from "../../constants/react_routes";
46

57
export const CancelPayment = () => {
68

@@ -17,9 +19,8 @@ export const CancelPayment = () => {
1719
</Grid>
1820

1921
<Grid item xs={12} style={{marginTop: "2rem", fontWeight: "bold"}}>
20-
Go back to Checkout.
22+
Go back to <Link to={CHECKOUT_ROUTE}>Checkout</Link>.
2123
</Grid>
22-
2324
</Grid>
2425
)
2526
}

client/src/components/routes/checkout/continueButton.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function ContinueButton(props) {
5252
}
5353

5454
function continueButtonPropsAreEqual(prevProps, nextProps) {
55-
return prevProps.action === nextProps.action;
55+
return prevProps.action === nextProps.action && !prevProps.buttonHandler;
5656
}
5757

5858
const continueButtonMemoWrapper = React.memo(ContinueButton, continueButtonPropsAreEqual);

client/src/components/routes/checkout/paymentButton.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class PaymentButton extends Component {
1616
}
1717
}
1818

19+
componentDidMount() {
20+
if(this.props.paymentResponse && this.props.paymentResponse.hasOwnProperty("error")) {
21+
this.setState({paymentBtnClicked: true})
22+
}
23+
}
24+
1925
getGrandTotal = () => {
2026
this._GrandTotal = (this.props.cartTotal + this.props.deliveryCharges) * 100
2127
return this._GrandTotal
@@ -81,7 +87,8 @@ const mapStateToProps = (state) => {
8187
state.form.shippingAddressForm : null,
8288
shippingOption: state.shippingOptionReducer,
8389
addToCart: state.addToCartReducer,
84-
deliveryCharges: state.deliveryChargesReducer
90+
deliveryCharges: state.deliveryChargesReducer,
91+
paymentResponse: state.paymentResponseReducer
8592
})
8693
}
8794

client/src/components/routes/checkout/shippingOptions.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, {useEffect} from 'react';
22
import log from 'loglevel';
33
import {Divider, Grid} from "@material-ui/core";
44
import {makeStyles} from "@material-ui/core/styles";
@@ -9,7 +9,10 @@ import FormControl from '@material-ui/core/FormControl';
99
import ContinueButton from "./continueButton";
1010
import {useDispatch, useSelector} from "react-redux";
1111
import CircularProgress from '@material-ui/core/CircularProgress';
12-
import {DELIVERY_CHARGES, SHIPPING_OPTION_CONFIRMED} from "../../../actions/types";
12+
import {
13+
DELIVERY_CHARGES,
14+
SHIPPING_OPTION_CONFIRMED
15+
} from "../../../actions/types";
1316
import {MONTHS} from "../../../constants/constants";
1417
import Button from "@material-ui/core/Button";
1518

@@ -55,10 +58,11 @@ export function ShippingOptions() {
5558
const classes = useStyles();
5659
const shoppingBagProducts = useSelector(state => state.shoppingBagProductReducer)
5760
const dispatch = useDispatch()
58-
const [shippingOptionState, setShippingOptionState] = React.useState({value: 'free', submitted: false});
61+
const [shippingOptionState, setShippingOptionState] = React.useState( {value: 'free', submitted: false})
5962

6063
const handleRadioBtnChange = (event) => {
61-
setShippingOptionState({value: event.target.value, submitted: false});
64+
log.info(`event.target.value = ${event.target.value}`)
65+
setShippingOptionState({value: event.target.value, submitted: false})
6266

6367
let deliveryPrice = null
6468

@@ -81,18 +85,18 @@ export function ShippingOptions() {
8185
type: DELIVERY_CHARGES,
8286
payload: deliveryPrice
8387
})
84-
8588
};
8689

8790
const submitHandler = () => {
88-
setShippingOptionState({value: shippingOptionState.value, submitted: true});
91+
let selectedOption = shippingOptionState.value
92+
setShippingOptionState({value: selectedOption, submitted: true})
8993

9094
dispatch({
9195
type: SHIPPING_OPTION_CONFIRMED,
9296
payload: {
93-
estimatedDate: getEstimatedDeliveryDate(shippingOptionsData[shippingOptionState.value].estimatedDays),
94-
price: shippingOptionsData[shippingOptionState.value].price,
95-
deliveryType: shippingOptionsData[shippingOptionState.value].label,
97+
estimatedDate: getEstimatedDeliveryDate(shippingOptionsData[selectedOption].estimatedDays),
98+
price: shippingOptionsData[selectedOption].price,
99+
deliveryType: shippingOptionsData[selectedOption].label,
96100
submitted: true
97101
}
98102
})
@@ -212,6 +216,7 @@ export function ShippingOptions() {
212216
}
213217

214218
log.info("[ShippingOptions] Rendering ShippingOptions Component.")
219+
log.info(`shippingOptionState-3 = ${JSON.stringify(shippingOptionState)}`)
215220

216221
return (
217222
<Grid item style={{width: "100%", height: "fit-content"}}>
@@ -228,7 +233,8 @@ export function ShippingOptions() {
228233
<Divider style={{height: 1, width: "inherit"}}/>
229234
</Grid>
230235

231-
{shippingOptionState.submitted ? renderEditButton() : <ContinueButton buttonHandler={submitHandler}/>}
236+
{shippingOptionState.submitted ? renderEditButton() : <ContinueButton
237+
buttonHandler={submitHandler}/>}
232238
</Grid>
233239
)
234240
}

client/src/components/routes/checkout/summaryCard.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import IconButton from '@material-ui/core/IconButton';
88
export const SummaryCard = (props) => {
99

1010
const renderCardContent = () => {
11-
11+
let id = 0
1212
return props.contentList.map((formValue) => {
13+
++id
1314
return (
14-
<Grid item key={formValue} style={{marginBottom: "0.5rem"}}>
15+
<Grid item key={`sc-${id}`} style={{marginBottom: "0.5rem"}}>
1516
{formValue}
1617
</Grid>
1718
)

client/src/components/routes/detail/productDetails.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, {useEffect, useState} from 'react';
2-
import {Button, Grid, Box, Hidden} from "@material-ui/core";
2+
import {Button, Grid, Box} from "@material-ui/core";
33
import log from 'loglevel';
44
import BreadcrumbsSection from "../../ui/breadcrumbs";
5-
import {HOME_ROUTE} from "../../../constants/constants";
65
import history from "../../../history";
76
import {SearchMatchesNotFound} from "../../ui/error/searchMatchesNotFound";
87
import {useDispatch, useSelector} from "react-redux";
@@ -11,14 +10,17 @@ import {getDataViaAPI} from '../../../actions'
1110
import AddShoppingCartIcon from '@material-ui/icons/AddShoppingCart';
1211
import LocalMallIcon from '@material-ui/icons/LocalMall';
1312
import Cookies from 'js-cookie';
14-
import {ADD_TO_CART, PRODUCT_BY_ID_DATA_API, SELECT_PRODUCT_DETAIL, SHOPPERS_PRODUCT_ID} from "../../../actions/types";
13+
import {ADD_TO_CART, SELECT_PRODUCT_DETAIL} from "../../../actions/types";
1514
import RemoveIcon from '@material-ui/icons/Remove';
1615
import AddIcon from '@material-ui/icons/Add';
1716
import {makeStyles} from "@material-ui/core/styles";
1817
import Spinner from "../../ui/spinner";
1918
import {InternalServerError} from "../../ui/error/internalServerError";
2019
import {BadRequest} from "../../ui/error/badRequest";
2120
import _ from "lodash";
21+
import {PRODUCT_BY_ID_DATA_API} from "../../../constants/api_routes";
22+
import {SHOPPERS_PRODUCT_INFO_COOKIE} from "../../../constants/cookies";
23+
import {HOME_ROUTE} from "../../../constants/react_routes";
2224

2325
export const useButtonStyles = makeStyles(() => ({
2426
buttonStartIcon: {
@@ -103,7 +105,7 @@ function ProductDetails(props) {
103105
}
104106

105107
const dispatchAddToCart = newAddToCart => {
106-
Cookies.set(SHOPPERS_PRODUCT_ID, newAddToCart, {expires: 7});
108+
Cookies.set(SHOPPERS_PRODUCT_INFO_COOKIE, newAddToCart, {expires: 7});
107109
log.info(`[Product Detail] dispatchAddToCart productQty = ${JSON.stringify(newAddToCart)}`)
108110
dispatch({
109111
type: ADD_TO_CART,

client/src/components/routes/home/home.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import HomeMenuIcons from "./homeMenuIcons";
1212
import Hidden from "@material-ui/core/Hidden";
1313
import Spinner from "../../ui/spinner";
1414
import {HTTPError} from "../../ui/error/httpError";
15-
import {LOAD_HOME_PAGE, HOME_PAGE_DATA_API, HOME_PAGE_API_OBJECT_LEN} from "../../../actions/types";
15+
import {LOAD_HOME_PAGE} from "../../../actions/types";
1616
import {BadRequest} from "../../ui/error/badRequest";
17+
import {HOME_PAGE_DATA_API} from "../../../constants/api_routes";
18+
import {HOME_PAGE_API_OBJECT_LEN} from "../../../constants/constants"
1719

1820
const Home = props => {
1921
const {hover} = useSelector(state => state.tabHoverEventReducer)

client/src/components/routes/navbar/accordionSection.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import Typography from '@material-ui/core/Typography';
77
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
88
import {Link} from "react-router-dom";
99
import {useSelector} from "react-redux";
10-
import {PRODUCTS_ROUTE, TAB_CONFIG} from "../../../constants/constants";
10+
import {TAB_CONFIG} from "../../../constants/constants";
1111
import log from "loglevel";
12+
import {PRODUCTS_ROUTE} from "../../../constants/react_routes";
1213

1314
const height = 48
1415

0 commit comments

Comments
 (0)