Authored by yoho-js001

Add or cancel favorite for good goods recommend detai. reviewed by YinCheng.

... ... @@ -26,13 +26,7 @@ export default class BLKBrandCell extends React.Component {
});
}
shouldComponentUpdate(nextProps){
if (Immutable.is(nextProps.resource, this.props.resource)) {
return false;
} else {
return true;
}
}
_renderRow(rowData, sectionID, rowID) {
let img = rowData.get('brand_ico');
img = img.replace('{width}', cellWidth*2).replace('{height}', (cellWidth-10)*2).replace('{mode}',2);
... ...
... ... @@ -94,7 +94,7 @@ export default class Detail extends Component {
}
render() {
let {product_skn, product, similarList} = this.props.resource;
let {product_skn, favorite, product, similarList} = this.props.resource;
let isFetching = product.get('isFetching')||similarList.get('isFetching');
let images = product.getIn(['data','goods_list',0,'images_list']);
let similar = similarList&&similarList.get('data')&&similarList.get('data').toArray().length?similarList.get('data').toArray():[];
... ... @@ -106,10 +106,11 @@ export default class Detail extends Component {
'similar':similar,
};
//Test
product_skn = '51148345';
//
// //Test
// product_skn = '51148345';
// //
let fav_image = favorite?require('../../image/love_tab_ic_h.png'):require('../../image/love_tab_ic.png');
let fav_title = favorite?'已收藏':'收藏';
return (
<View style={styles.container}>
{!isFetching?
... ... @@ -122,10 +123,10 @@ export default class Detail extends Component {
renderRow={this.renderRow}
/>
<View style={styles.footer}>
<TouchableOpacity style={styles.favoriteButton}>
<TouchableOpacity style={styles.favoriteButton} onPress={() => {this.props.onPressFavorite&&this.props.onPressFavorite(favorite)}}>
<View style={{flex: 1, flexDirection: 'row', alignItems: 'center'}}>
<Image style={styles.favIcon} source={require('../../image/love_tab_ic.png')}/>
<Text style={styles.favText}>已收藏</Text>
<Image style={styles.favIcon} source={fav_image}/>
<Text style={styles.favText}>{fav_title}</Text>
</View>
</TouchableOpacity>
<View style={styles.detailButton}>
... ...
... ... @@ -14,4 +14,6 @@ export default keyMirror({
GET_SIMILAR_REQUEST: null,
GET_SIMILAR_SUCCESS: null,
GET_SIMILAR_FAILURE: null,
SET_FAVORITE_STATUS: null,
});
... ...
... ... @@ -47,6 +47,7 @@ class DetailContainer extends Component {
constructor(props) {
super(props);
this._onPressProduct = this._onPressProduct.bind(this);
this._onPressFavorite = this._onPressFavorite.bind(this);
}
componentDidMount() {
... ... @@ -68,12 +69,17 @@ class DetailContainer extends Component {
ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
}
_onPressFavorite(favorite) {
this.props.actions.changeFavoriteStatus(favorite);
}
render() {
let {detail} = this.props;
return (
<Detail
resource={detail}
onPressProduct={this._onPressProduct}
onPressFavorite={this._onPressFavorite}
/>
);
}
... ...
... ... @@ -2,8 +2,7 @@
import ReactNative from 'react-native';
import DetailService from '../../services/DetailService';
import helper from '../../../common/utils/helper';
import Immutable, {Map} from 'immutable';
const {
SET_PRODUCT_SKN,
... ... @@ -14,6 +13,7 @@ const {
GET_SIMILAR_REQUEST,
GET_SIMILAR_SUCCESS,
GET_SIMILAR_FAILURE,
SET_FAVORITE_STATUS,
} = require('../../constants/actionTypes').default;
export function setProductSKN(product_skn) {
... ... @@ -33,9 +33,16 @@ export function getDetail() {
dispatch(getDetailRequest());
return new DetailService(app.host).getDetailWithProductSKN(product_skn)
.then(json => {
console.log(product_skn);
console.log(json);
// console.log(product_skn);
// console.log(json);
dispatch(getDetailSuccess(json));
return new DetailService('http://single.yoho.cn/favorite').getFavoriteStatusWithProductID(json.product_id)
.then(json => {
dispatch(setFavoriteStatus(json));
})
.catch(error => {
dispatch(setFavoriteStatus(false));
})
})
.catch(error => {
dispatch(getDetailFailure(error));
... ... @@ -43,6 +50,28 @@ export function getDetail() {
};
}
export function changeFavoriteStatus(favorite) {
return (dispatch, getState) => {
let {app, detail} = getState();
let product_id = detail.get('product').get('data').get('product_id');
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));
}
}
export function setFavoriteStatus(favorite) {
return {
type: SET_FAVORITE_STATUS,
payload: favorite
};
}
export function getDetailRequest() {
return {
type: GET_DETAIL_REQUEST,
... ... @@ -73,8 +102,8 @@ export function getSimilar() {
dispatch(getSimilarRequest());
return new DetailService(app.host).getSimilarListWithSKN(product_skn)
.then(json => {
console.log(product_skn);
console.log(json);
// console.log(product_skn);
// console.log(json);
dispatch(getSimilarSuccess(json.product_list));
})
.catch(error => {
... ...
... ... @@ -4,6 +4,7 @@ import {Record, List, Map} from 'immutable';
let InitialState = Record({
product_skn: '',
favorite: false,
product: new (Record({
isFetching: false,
error: null,
... ...
... ... @@ -12,12 +12,16 @@ const {
GET_SIMILAR_REQUEST,
GET_SIMILAR_SUCCESS,
GET_SIMILAR_FAILURE,
SET_FAVORITE_STATUS,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
export default function detailReducer(state=initialState, action) {
switch(action.type) {
case SET_FAVORITE_STATUS: {
return state.set('favorite', action.payload);
}
case SET_PRODUCT_SKN: {
return state.set('product_skn', action.payload);
}
... ...
... ... @@ -13,11 +13,11 @@ export default class DetailService {
}
async getDetailWithProductSKN(product_skn) {
//test
product_skn = '51148345';
this.api = new Request('http://dev-api.yohops.com:9999');
//
// //test
// product_skn = '51148345';
// this.api = new Request('http://dev-api.yohops.com:9999');
// //
return await this.api.get({
url: '',
body: {
... ... @@ -36,9 +36,9 @@ export default class DetailService {
async getSimilarListWithSKN(product_skn) {
//test
product_skn = '51148345';
//
// //test
// product_skn = '51148345';
// //
return await this.api.get({
url: '',
... ... @@ -56,4 +56,56 @@ export default class DetailService {
});
}
async getFavoriteStatusWithProductID(id) {
return await this.api.get({
url: '',
body: {
method: 'app.favorite.isFavoriteNew',
id,
}
})
.then((json) => {
return json;
})
.catch((error) => {
throw(error);
});
}
async addFavorite(id) {
let type='product';
return await this.api.get({
url: '',
body: {
method: 'app.favorite.add',
id,
type,
}
})
.then((json) => {
return json;
})
.catch((error) => {
throw(error);
});
}
async cancelFavorite(id) {
let type='product';
return await this.api.get({
url: '',
body: {
method: 'app.favorite.cancel',
id,
type,
}
})
.then((json) => {
return json;
})
.catch((error) => {
throw(error);
});
}
}
... ...