@@ -2,18 +2,30 @@ import axios from 'axios';
22import { URL } from 'src/constants/urls' ;
33import * as actions from './Product.actions' ;
44import { v4 as uuid } from 'uuid' ;
5+ import { setAlert } from 'src/components/Alert/Alert.thunks' ;
6+ import { AlertTypes } from 'src/constants/alerts' ;
7+
8+ const dispatchError = ( dispatch , error ) => {
9+ const payload = {
10+ msg : error . response ?. statusText ,
11+ status : error . response ?. status ,
12+ } ;
13+ dispatch ( actions . productError ( payload ) ) ;
14+ dispatch (
15+ setAlert ( {
16+ msg : error . response ?. statusText ,
17+ type : AlertTypes . ERROR ,
18+ } ) ,
19+ ) ;
20+ } ;
521
622export const getProducts = ( ) => async dispatch => {
723 try {
824 const res = await axios . get ( `${ URL . baseAPIUrl } /api/products` ) ;
925 const products = res . data ;
1026 dispatch ( actions . getProductsSuccess ( products ) ) ;
1127 } catch ( error ) {
12- const payload = {
13- msg : error . response ?. statusText ,
14- status : error . response ?. status ,
15- } ;
16- dispatch ( actions . productError ( payload ) ) ;
28+ dispatchError ( dispatch , error ) ;
1729 }
1830} ;
1931
@@ -23,11 +35,7 @@ export const getProduct = id => async dispatch => {
2335 const product = res . data as Product ;
2436 dispatch ( actions . getProductSuccess ( product ) ) ;
2537 } catch ( error ) {
26- const payload = {
27- msg : error . response ?. statusText ,
28- status : error . response ?. status ,
29- } ;
30- dispatch ( actions . productError ( payload ) ) ;
38+ dispatchError ( dispatch , error ) ;
3139 }
3240} ;
3341export const clearProduct = ( ) => dispatch => {
@@ -44,27 +52,31 @@ export const createProduct = (formData: ProductForm) => async dispatch => {
4452 } ;
4553 await axios . post ( `${ URL . baseAPIUrl } /api/products` , newProduct ) ;
4654 dispatch ( actions . createProductSuccess ( newProduct ) ) ;
55+ dispatch (
56+ setAlert ( {
57+ msg : 'Create product successfully' ,
58+ type : AlertTypes . SUCCESS ,
59+ } ) ,
60+ ) ;
4761 dispatch ( getProducts ( ) ) ;
4862 } catch ( error ) {
49- const payload = {
50- msg : error . response ?. statusText ,
51- status : error . response ?. status ,
52- } ;
53- dispatch ( actions . productError ( payload ) ) ;
63+ dispatchError ( dispatch , error ) ;
5464 }
5565} ;
5666
5767export const deleteProduct = ( id : string ) => async dispatch => {
5868 try {
5969 await axios . delete ( `${ URL . baseAPIUrl } /api/products/${ id } ` ) ;
6070 dispatch ( actions . deleteProductSuccess ( id ) ) ;
71+ dispatch (
72+ setAlert ( {
73+ msg : 'Delete product successfully' ,
74+ type : AlertTypes . SUCCESS ,
75+ } ) ,
76+ ) ;
6177 dispatch ( getProducts ( ) ) ;
6278 } catch ( error ) {
63- const payload = {
64- msg : error . response ?. statusText ,
65- status : error . response ?. status ,
66- } ;
67- dispatch ( actions . productError ( payload ) ) ;
79+ dispatchError ( dispatch , error ) ;
6880 }
6981} ;
7082
@@ -74,24 +86,22 @@ export const editProduct = (id: string) => async dispatch => {
7486 const product = res . data as Product ;
7587 dispatch ( actions . editProductSuccess ( product ) ) ;
7688 } catch ( error ) {
77- const payload = {
78- msg : error . response ?. statusText ,
79- status : error . response ?. status ,
80- } ;
81- dispatch ( actions . productError ( payload ) ) ;
89+ dispatchError ( dispatch , error ) ;
8290 }
8391} ;
8492
8593export const updateProduct = ( product : Product ) => async dispatch => {
8694 try {
8795 await axios . put ( `${ URL . baseAPIUrl } /api/products/${ product . id } ` , product ) ;
8896 dispatch ( actions . updateProductSuccess ( product ) ) ;
97+ dispatch (
98+ setAlert ( {
99+ msg : 'Update product successfully' ,
100+ type : AlertTypes . SUCCESS ,
101+ } ) ,
102+ ) ;
89103 dispatch ( getProducts ( ) ) ;
90104 } catch ( error ) {
91- const payload = {
92- msg : error . response ?. statusText ,
93- status : error . response ?. status ,
94- } ;
95- dispatch ( actions . productError ( payload ) ) ;
105+ dispatchError ( dispatch , error ) ;
96106 }
97107} ;
0 commit comments