Skip to content

Commit 0f3774f

Browse files
committed
Minor update clear product when button create new product clicked
1 parent cfc9633 commit 0f3774f

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

src/components/Products/Product.actions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ export const productError = (payload: ProductError) => ({
2828
type: types.PRODUCT_ERROR,
2929
payload,
3030
});
31+
32+
export const clearProductSuccess = () => ({
33+
type: types.CLEAR_PRODUCT,
34+
});

src/components/Products/Product.constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export const PRODUCT_ERROR = 'views/Product/PRODUCT_ERROR';
44
export const CREATE_PRODUCT = 'views/Product/CREATE_PRODUCT';
55
export const UPDATE_PRODUCT = 'views/Product/UPDATE_PRODUCT';
66
export const DELETE_PRODUCT = 'views/Product/DELETE_PRODUCT';
7+
export const CLEAR_PRODUCT = 'views/Product/CLEAR_PRODUCT';

src/components/Products/Product.reducers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ export const productReducer = (state = initialState, action) => {
4747
...state,
4848
error: payload,
4949
loading: false,
50+
products: [],
51+
product: null,
52+
};
53+
case types.CLEAR_PRODUCT:
54+
return {
55+
...state,
56+
product: null,
57+
loading: false,
5058
};
5159

5260
default:

src/components/Products/Product.thunks.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export const getProduct = id => async dispatch => {
3030
dispatch(actions.productError(payload));
3131
}
3232
};
33+
export const clearProduct = () => dispatch => {
34+
dispatch(actions.clearProductSuccess());
35+
};
3336

3437
export const createProduct = (formData: ProductForm) => async dispatch => {
3538
try {

src/components/Products/ProductList.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React, { useEffect } from 'react';
44
import { connect, ConnectedProps } from 'react-redux';
55
import { Link, useHistory } from 'react-router-dom';
66
import { PATH } from 'src/constants/paths';
7-
import { getProducts } from './Product.thunks';
7+
import { getProducts, clearProduct } from './Product.thunks';
88

99
const mapStateToProps = (state: AppState) => ({
1010
loading: state.products.loading,
@@ -13,6 +13,7 @@ const mapStateToProps = (state: AppState) => ({
1313

1414
const mapDispatchToProps = {
1515
getProducts,
16+
clearProduct,
1617
};
1718

1819
const connector = connect(mapStateToProps, mapDispatchToProps);
@@ -21,7 +22,7 @@ interface Props extends ConnectedProps<typeof connector> {}
2122
export const _ProductList = (props: Props) => {
2223
const history = useHistory();
2324

24-
const { products, getProducts } = props;
25+
const { products, getProducts, clearProduct } = props;
2526

2627
const columns = [
2728
{
@@ -64,6 +65,7 @@ export const _ProductList = (props: Props) => {
6465
};
6566

6667
const addNewProduct = () => {
68+
clearProduct();
6769
history.push(PATH.PRODUCT_NEW);
6870
};
6971

0 commit comments

Comments
 (0)