detailActions.js
3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
import ReactNative from 'react-native';
import DetailService from '../../services/DetailService';
import Immutable, {Map} from 'immutable';
const {
SET_PRODUCT_SKN,
GET_DETAIL_REQUEST,
GET_DETAIL_SUCCESS,
GET_DETAIL_FAILURE,
GET_SIMILAR_REQUEST,
GET_SIMILAR_SUCCESS,
GET_SIMILAR_FAILURE,
SET_FAVORITE_STATUS,
} = require('../../constants/actionTypes').default;
export function setProductSKN(product_skn) {
return {
type: SET_PRODUCT_SKN,
payload: product_skn,
};
}
export function getDetail() {
return (dispatch, getState) => {
let {app, detail} = getState();
let {product_skn} = detail;
// if (!product_skn) {
// return ;
// }
dispatch(getDetailRequest());
return new DetailService(app.host).getDetailWithProductSKN(product_skn)
.then(json => {
// console.log(product_skn);
// console.log(json);
dispatch(getDetailSuccess(json));
return new DetailService(app.singleHost).getFavoriteStatusWithProductID(json.product_id)
.then(json => {
dispatch(setFavoriteStatus(json));
})
.catch(error => {
dispatch(setFavoriteStatus(false));
})
})
.catch(error => {
dispatch(getDetailFailure(error));
});
};
}
export function changeFavoriteStatus(favorite) {
return (dispatch, getState) => {
let {app, detail} = getState();
let product_id = detail.get('product').get('data').get('product_id');
let favRequest = (product_id, favorite) => {
if (product_id && favorite) {
new DetailService(app.host).cancelFavorite(product_id);
}
if (product_id && !favorite) {
new DetailService(app.host).addFavorite(product_id);
}
dispatch(setFavoriteStatus(!favorite));
}
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
favRequest(product_id, favorite);
})
.catch(error => {
ReactNative.NativeModules.YH_CommonHelper.login()
.then(uid => {
favRequest(product_id, favorite);
})
.catch(error => {
});
});
}
}
export function setFavoriteStatus(favorite) {
return {
type: SET_FAVORITE_STATUS,
payload: favorite
};
}
export function getDetailRequest() {
return {
type: GET_DETAIL_REQUEST,
};
}
export function getDetailSuccess(json) {
return {
type: GET_DETAIL_SUCCESS,
payload: json
};
}
export function getDetailFailure(error) {
return {
type: GET_DETAIL_FAILURE,
payload: error
};
}
export function getSimilar() {
return (dispatch, getState) => {
let {app, detail} = getState();
let {product_skn} = detail;
// if (!product_skn) {
// return ;
// }
dispatch(getSimilarRequest());
return new DetailService(app.host).getSimilarListWithSKN(product_skn)
.then(json => {
// console.log(product_skn);
// console.log(json);
dispatch(getSimilarSuccess(json.product_list));
})
.catch(error => {
// console.log(product_skn);
// console.log(error);
dispatch(getSimilarFailure(error));
});
};
}
export function getSimilarRequest() {
return {
type: GET_SIMILAR_REQUEST,
};
}
export function getSimilarSuccess(json) {
return {
type: GET_SIMILAR_SUCCESS,
payload: json
};
}
export function getSimilarFailure(error) {
return {
type: GET_SIMILAR_FAILURE,
payload: error
};
}