Skip to content

Commit 7dbbd85

Browse files
committed
Update actions products
1 parent e869aa4 commit 7dbbd85

File tree

14 files changed

+170
-23
lines changed

14 files changed

+170
-23
lines changed

src/@types/alert.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum AlertTypes {
2+
SUCCESS = 'sucess',
3+
ERROR = 'error',
4+
}

src/@types/product.d.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
11
interface Product {
22
id: string;
33
name: string;
4-
quantity: string;
5-
price: number;
4+
image_url: string;
5+
brand: string;
66
}
7-
8-
interface ResGetProductApi extends Res {
9-
data: {
10-
products: Product[];
11-
};
12-
}
13-
14-
interface ResGetProduct extends ActionRedux {
15-
payload: ResGetProductApi;
7+
interface ProductError {
8+
msg: string;
9+
status: number;
1610
}
1711

18-
interface ResGetProductItemApi extends Res {
19-
data: {
20-
product: Product;
21-
};
22-
}
23-
24-
interface ResGetProductItem extends ActionRedux {
25-
payload: ResGetProductItemApi;
12+
interface ProductForm {
13+
name: string;
14+
image_url: string;
15+
brand: string;
2616
}
2717

28-
enum PhoneMark {
18+
enum PhoneBrand {
2919
APPLE,
3020
SAMSUM,
3121
XIAOMI,

src/components/Home/AuthLinks.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { ProductList } from 'src/components/Product/ProductList';
2+
import { ProductList } from 'src/components/Products/ProductList';
33

44
export const AuthLinks = () => {
55
return <ProductList />;

src/components/Product/Product.actions.ts

Whitespace-only changes.

src/components/Product/Product.reducers.ts

Whitespace-only changes.

src/components/Product/Product.thunks.ts

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as types from './Product.constants';
2+
3+
export const getProductsSuccess = (payload: Product[]) => ({
4+
type: types.GET_PRODUCTS,
5+
payload,
6+
});
7+
8+
export const getProductSuccess = (payload: Product) => ({
9+
type: types.GET_PRODUCT,
10+
payload,
11+
});
12+
13+
export const createProductSuccess = (payload: Product) => ({
14+
type: types.CREATE_PRODUCT,
15+
payload,
16+
});
17+
18+
export const updateProductSuccess = (payload: Product) => ({
19+
type: types.UPDATE_PRODUCT,
20+
payload,
21+
});
22+
23+
export const deleteProductSuccess = (payload: string) => ({
24+
type: types.DELETE_PRODUCT,
25+
});
26+
27+
export const productError = (payload: ProductError) => ({
28+
type: types.PRODUCT_ERROR,
29+
payload,
30+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const GET_PRODUCTS = 'views/Product/GET_PRODUCTS';
2+
export const GET_PRODUCT = 'views/Product/GET_PRODUCT';
3+
export const PRODUCT_ERROR = 'views/Product/PRODUCT_ERROR';
4+
export const CREATE_PRODUCT = 'views/Product/CREATE_PRODUCT';
5+
export const UPDATE_PRODUCT = 'views/Product/UPDATE_PRODUCT';
6+
export const DELETE_PRODUCT = 'views/Product/DELETE_PRODUCT';
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as types from './Product.constants';
2+
3+
const products = [] as Product[];
4+
const initialState = {
5+
loading: true,
6+
error: {},
7+
product: null,
8+
products: products,
9+
};
10+
11+
export const productReducer = (state = initialState, action) => {
12+
const { type, payload } = action;
13+
14+
switch (type) {
15+
case types.GET_PRODUCTS:
16+
return {
17+
...state,
18+
products: payload,
19+
loading: false,
20+
};
21+
case types.GET_PRODUCT:
22+
return {
23+
...state,
24+
product: payload,
25+
loading: false,
26+
};
27+
case types.CREATE_PRODUCT:
28+
return {
29+
...state,
30+
products: [payload, ...state.products],
31+
loading: false,
32+
};
33+
case types.UPDATE_PRODUCT:
34+
return {
35+
...state,
36+
product: payload,
37+
loading: false,
38+
};
39+
case types.DELETE_PRODUCT:
40+
return {
41+
...state,
42+
products: state.products.filter(p => p.id !== payload),
43+
loading: false,
44+
};
45+
case types.PRODUCT_ERROR:
46+
return {
47+
...state,
48+
error: payload,
49+
loading: false,
50+
};
51+
52+
default:
53+
return state;
54+
}
55+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import axios from 'axios';
2+
import { URL } from 'src/constants/urls';
3+
import * as actions from './Product.actions';
4+
import { v4 as uuid } from 'uuid';
5+
6+
export const getProducts = () => async dispatch => {
7+
try {
8+
const res = await axios.get(`${URL.baseAPIUrl}/api/products`);
9+
const products = res.data as Product[];
10+
dispatch(actions.getProductsSuccess(products));
11+
} catch (error) {
12+
const payload = {
13+
msg: error.response.statusText,
14+
status: error.response.status,
15+
};
16+
dispatch(actions.productError(payload));
17+
}
18+
};
19+
20+
export const getProduct = id => async dispatch => {
21+
try {
22+
const res = await axios.get(`${URL.baseAPIUrl}/api/products/${id}`);
23+
const product = res.data as Product;
24+
dispatch(actions.getProductSuccess(product));
25+
} catch (error) {
26+
const payload = {
27+
msg: error.response.statusText,
28+
status: error.response.status,
29+
};
30+
dispatch(actions.productError(payload));
31+
}
32+
};
33+
34+
export const createProduct = (formData: ProductForm) => async dispatch => {
35+
try {
36+
const newProduct = {
37+
id: uuid(),
38+
...formData,
39+
};
40+
await axios.post(`${URL.baseAPIUrl}/api/products`, newProduct);
41+
dispatch(actions.createProductSuccess(newProduct));
42+
} catch (error) {
43+
const payload = {
44+
msg: error.response.statusText,
45+
status: error.response.status,
46+
};
47+
dispatch(actions.productError(payload));
48+
}
49+
};
50+
51+
export const deleteProduct = (id: string) => async dispatch => {
52+
try {
53+
await axios.delete(`${URL.baseAPIUrl}/api/products/${id}`);
54+
dispatch(actions.deleteProductSuccess(id));
55+
} catch (error) {
56+
const payload = {
57+
msg: error.response.statusText,
58+
status: error.response.status,
59+
};
60+
dispatch(actions.productError(payload));
61+
}
62+
};

0 commit comments

Comments
 (0)