Skip to content

Commit 7f48a49

Browse files
committed
Update Edit products actions
1 parent aacb900 commit 7f48a49

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

src/components/Products/Product.actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ export const updateProductSuccess = (payload: Product) => ({
2525
payload,
2626
});
2727

28-
export const deleteProductSuccess = () => ({
28+
export const deleteProductSuccess = (payload: string) => ({
2929
type: types.DELETE_PRODUCT,
30+
payload,
3031
});
3132

3233
export const productError = (payload: ProductError) => ({

src/components/Products/Product.reducers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const productReducer = (state = initialState, action) =>
3737
draft.loading = false;
3838
break;
3939
case types.DELETE_PRODUCT:
40-
draft.products = state.products.filter(p => p.id !== payload);
40+
draft.products = state.products.filter(p => p.id != payload);
4141
draft.loading = false;
4242
break;
4343
case types.PRODUCT_ERROR:

src/components/Products/Product.thunks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const createProduct = (formData: ProductForm) => async dispatch => {
5757
export const deleteProduct = (id: string) => async dispatch => {
5858
try {
5959
await axios.delete(`${URL.baseAPIUrl}/api/products/${id}`);
60-
dispatch(actions.deleteProductSuccess());
60+
dispatch(actions.deleteProductSuccess(id));
6161
dispatch(getProducts());
6262
} catch (error) {
6363
const payload = {
@@ -86,6 +86,7 @@ export const updateProduct = (product: Product) => async dispatch => {
8686
try {
8787
await axios.put(`${URL.baseAPIUrl}/api/products/${product.id}`, product);
8888
dispatch(actions.updateProductSuccess(product));
89+
dispatch(getProducts());
8990
} catch (error) {
9091
const payload = {
9192
msg: error.response?.statusText,

src/components/Products/ProductForm.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export const _ProductForm = (props: Props) => {
2929
const onFinish = values => {
3030
if (!edit) {
3131
createProduct(values);
32+
} else {
33+
const changedProduct = { ...product, ...values };
34+
updateProduct(changedProduct);
3235
}
3336

3437
history.push(PATH.HOME);
@@ -101,10 +104,11 @@ export const _ProductForm = (props: Props) => {
101104
<Input />
102105
</Form.Item>
103106

104-
<Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 10 }}>
105-
<Button type="primary" htmlType="submit">
107+
<Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 8 }}>
108+
<Button type="primary" htmlType="submit" className="mr-1">
106109
Submit
107110
</Button>
111+
<Button onClick={e => history.goBack()}>Back</Button>
108112
</Form.Item>
109113
</Form>
110114
</div>

src/components/Products/ProductItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const _ProductItem = (props: Props) => {
3131
history.goBack();
3232
};
3333
const goEdit = () => {
34-
history.push(PATH.PRODUCT_EDIT);
34+
history.push(`${PATH.PRODUCTS}/${product.id}/edit`);
3535
};
3636
const onDelete = () => {
3737
deleteProduct(product.id);
@@ -43,7 +43,7 @@ export const _ProductItem = (props: Props) => {
4343
return (
4444
<div className="product-item-section mt-2">
4545
<div className="container">
46-
<Row>
46+
<Row gutter={[40, 0]}>
4747
<Col span={12}>
4848
<Image src={item.image_url} />
4949
</Col>

0 commit comments

Comments
 (0)