Skip to content

Commit 6af29eb

Browse files
committed
Update create product actions
1 parent 131f724 commit 6af29eb

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

server/db/db.json

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"users": [{
2+
"users": [
3+
{
34
"id": "idtest",
45
"username": "tester",
56
"email": "tester@gmail.com",
@@ -24,7 +25,8 @@
2425
"password": "123456"
2526
}
2627
],
27-
"products": [{
28+
"products": [
29+
{
2830
"id": "iphone-8-plus",
2931
"name": "Iphone 8 Plus",
3032
"image_url": "/images/iphone-8-plus.jpg",
@@ -71,6 +73,42 @@
7173
"name": "Iphone XS Max",
7274
"image_url": "/images/iphone-xs-max.jpg",
7375
"brand": "APPLE"
76+
},
77+
{
78+
"name": "Iphone 7s plus",
79+
"brand": "APPLE",
80+
"image_url": "https://images-na.ssl-images-amazon.com/images/I/71Q5RFc7NVL._AC_SL1500_.jpg",
81+
"id": "00528308-fb0f-4d60-85b3-473ebd1aed76"
82+
},
83+
{
84+
"name": "Iphone 12 Pro",
85+
"brand": "APPLE",
86+
"image_url": "https://images.frandroid.com/wp-content/uploads/2020/10/iphone-12-max-frandroid-2020.png",
87+
"id": "5695c3c0-10b3-40d7-b39b-f8f807323dc4"
88+
},
89+
{
90+
"name": "Iphone 12 Pro Max",
91+
"brand": "APPLE",
92+
"image_url": "https://www.apple.com/newsroom/images/product/iphone/standard/Apple_announce-iphone12pro_10132020.jpg.landing-big_2x.jpg",
93+
"id": "4a36b6d8-e8e4-4f13-a19a-ca3c155e479a"
94+
},
95+
{
96+
"name": "Iphone 12 Pro Max",
97+
"brand": "APPLE",
98+
"image_url": "https://www.apple.com/newsroom/images/product/iphone/standard/Apple_announce-iphone12pro_10132020.jpg.landing-big_2x.jpg",
99+
"id": "f7882d67-2207-4dfd-919e-c60a6477cdbd"
100+
},
101+
{
102+
"name": "Iphone 11 Pro max gold",
103+
"brand": "APPLE",
104+
"image_url": "https://leronza.com/wp-content/uploads/2020/10/iPhone-12-Pro-24k-Gold-copy.png",
105+
"id": "4c0ae0b4-9a15-4be7-96cf-6bf47366805c"
106+
},
107+
{
108+
"name": "Iphone 12 Pro Gold",
109+
"brand": "APPLE",
110+
"image_url": "https://store.storeimages.cdn-apple.com/4982/as-images.apple.com/is/iphone-12-pro-gold-hero?wid=470&hei=556&fmt=jpeg&qlt=95&op_usm=0.5,0.5&.v=1604021659000",
111+
"id": "f1d30887-14f9-4238-8ecd-c106d5f529e8"
74112
}
75113
]
76114
}

src/components/Auth/Login.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ const _Login = (props: Props) => {
2121
const { login, isAuthenticated } = props;
2222

2323
const onFinish = async formData => {
24-
const { username, password } = formData;
25-
const payload = { username, password };
2624
try {
27-
await login(payload);
25+
await login(formData);
2826
} catch (error) {
2927
message.error(error.message);
3028
setError(error.payload.message);

src/components/Products/Product.thunks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ export const clearProduct = () => dispatch => {
3636

3737
export const createProduct = (formData: ProductForm) => async dispatch => {
3838
try {
39+
const img_default = '/images/image-default.jpg';
3940
const newProduct = {
40-
id: uuid(),
4141
...formData,
42+
id: uuid(),
43+
image_url: formData.image_url || img_default,
4244
};
4345
await axios.post(`${URL.baseAPIUrl}/api/products`, newProduct);
4446
dispatch(actions.createProductSuccess(newProduct));
47+
dispatch(getProducts());
4548
} catch (error) {
4649
const payload = {
4750
msg: error.response?.statusText,

src/components/Products/ProductForm.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
import React from 'react';
2+
import { connect, ConnectedProps } from 'react-redux';
23
import { Form, Input, Button, Select } from 'antd';
34
import { PhoneBrand } from 'src/constants/products';
4-
import { is } from 'immer/dist/internal';
5+
import { useHistory } from 'react-router-dom';
6+
7+
import { createProduct } from './Product.thunks';
8+
import { PATH } from 'src/constants/paths';
59
const { Option } = Select;
610

7-
export const ProductForm = () => {
8-
const onSelectChange = value => {
9-
//eslint-disable-next-line
10-
console.log(`selected ${value}`);
11-
};
11+
const mapStateToProps = (state: AppState) => ({
12+
products: state.products.products,
13+
});
14+
const mapDispatchToProps = { createProduct };
15+
const connector = connect(mapStateToProps, mapDispatchToProps);
16+
interface Props extends ConnectedProps<typeof connector> {}
17+
18+
export const _ProductForm = (props: Props) => {
19+
const { createProduct } = props;
20+
const history = useHistory();
1221
const layout = {
1322
labelCol: { span: 4 },
1423
wrapperCol: { span: 16 },
1524
};
1625

1726
const onFinish = values => {
18-
console.log(values);
27+
createProduct(values);
28+
history.push(PATH.HOME);
1929
};
2030

2131
const allBrands = Object.values(PhoneBrand).filter(
@@ -63,7 +73,6 @@ export const ProductForm = () => {
6373
style={{ width: 200 }}
6474
placeholder="Select a brand"
6575
optionFilterProp="children"
66-
onChange={onSelectChange}
6776
filterOption={(input, option) =>
6877
option?.children.toLowerCase().indexOf(input.toLowerCase()) >=
6978
0
@@ -89,3 +98,5 @@ export const ProductForm = () => {
8998
</div>
9099
);
91100
};
101+
102+
export const ProductForm = connector(_ProductForm);

src/components/Products/ProductList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const _ProductList = (props: Props) => {
7676
let data = [{}];
7777
useEffect(() => {
7878
getProducts();
79-
}, []);
79+
}, [getProducts]);
8080
products.map((product: Product, index: number) => {
8181
if (index === 0) {
8282
data = [

0 commit comments

Comments
 (0)