Authored by 孙凯

add 收藏逻辑 review by 红魔

... ... @@ -16,10 +16,6 @@ export default class ShopTopBanner extends React.Component {
constructor(props) {
super (props);
this.state = {
isCollection: false,
}
}
componentDidMount() {
... ... @@ -34,7 +30,7 @@ export default class ShopTopBanner extends React.Component {
let icon = info.shop_logo.replace('{width}', 100).replace('{height}', 100); // 商品缩略图
let name = info.shop_name;
let buttonIcon = require('../../../../image/btn_gz_p.png');
if (this.state.isCollection) {
if (!this.props.info.is_addFav) {
buttonIcon = require('../../../../image/btn_gz_n.png');
}
return (
... ... @@ -45,7 +41,9 @@ export default class ShopTopBanner extends React.Component {
style={{width: backgroundWidth, height: backgroundHeight }}
resizeMode={'cover'}
>
<TouchableOpacity onPress={this.props.onPressCollection} >
<TouchableOpacity activeOpacity={0.5} onPress={() => {
this.props.onPressCollection && this.props.onPressCollection(!this.props.info.is_addFav);
}}>
<Image
source={buttonIcon}
style={{ width: buttonWidth, height: buttonHeight,position: 'absolute',borderRadius: 5,bottom: -backgroundHeight + 15,right:15}}
... ...
... ... @@ -59,17 +59,20 @@ class BrandStoreContainer extends Component {
this.props.actions.getShopCouponList();
this.props.actions.getShopResources();
this.props.actions.getProductList();
}
componentWillUnmount() {
}
_onPressCollection(){
alert("_onPressCollection");
// this.props.actions.onPressCollection();
_onPressCollection(isCollected){
if (isCollected) {
console.log('addFavorite');
this.props.actions.addFavorite();
}else {
console.log('cancelFavorite');
this.props.actions.cancelFavorite();
}
}
_onPressCoupon() {
... ...
... ... @@ -43,11 +43,33 @@ export function onPressCoupon() {
};
}
export function onPressCollection() {
return {
type: SET_COLLECTION,
payload: true
};
export function addFavoriteSuccess() {
return {
type: SET_COLLECTION,
payload: true
}
}
export function addFavoriteFailure(error) {
if (error.code == 413) {
return {
type: SET_COLLECTION,
payload: true,
}
}
}
export function cancelFavoriteSuccess() {
return {
type: SET_COLLECTION,
payload: false
}
}
export function cancelFavoriteFailure(error) {
return {
type: SET_COLLECTION,
}
}
export function setStoreFilter(filter) {
... ... @@ -386,3 +408,74 @@ function parseProductList(json) {
};
}
export function addFavorite() {
return (dispatch, getState) => {
let {app, brandStore} = getState();
let {shopId} = brandStore;
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
return new BrandStoreService().addFavorite(shopId, shopId, uid)
.then(json => {
console.log('收藏成功');
dispatch(addFavoriteSuccess());
})
.catch(error => {
console.log(error);
dispatch(addFavoriteFailure(error));
});
})
.catch(error => {
ReactNative.NativeModules.YH_CommonHelper.login()
.then(uid => {
return new BrandStoreService().addFavorite(shopId, shopId, uid)
.then(json => {
console.log('收藏成功');
dispatch(addFavoriteSuccess());
})
.catch(error => {
dispatch(addFavoriteFailure(error));
});
})
.catch(error => {
});
});
};
}
export function cancelFavorite() {
return (dispatch, getState) => {
let {app, brandStore} = getState();
let {shopId} = brandStore;
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
return new BrandStoreService().cancelFavorite(shopId, shopId, uid)
.then(json => {
console.log('取消收藏成功');
dispatch(cancelFavoriteSuccess());
})
.catch(error => {
console.log(error);
dispatch(cancelFavoriteFailure(error));
});
})
.catch(error => {
ReactNative.NativeModules.YH_CommonHelper.login()
.then(uid => {
return new BrandStoreService().cancelFavorite(shopId, shopId, uid)
.then(json => {
console.log('取消收藏成功');
dispatch(cancelFavoriteSuccess());
})
.catch(error => {
console.log(error);
dispatch(cancelFavoriteFailure(error));
});
})
.catch(error => {
});
});
};
}
... ...
... ... @@ -16,6 +16,7 @@ let InitialState = Record({
shop_name: '',
shop_template_type: '2',
shops_id: 0,
is_addFav: false,
})),
resource: new (Record({
isFetching: false,
... ...
... ... @@ -41,7 +41,7 @@ const initialState = new InitialState;
export default function brandStoreReducer(state=initialState, action) {
switch(action.type) {
case SET_COLLECTION: {
return state.set('isCollection', action.payload);
return state.setIn(['info', 'is_addFav'], action.payload);
}
case SEL_COUPONITEM: {
... ... @@ -68,7 +68,7 @@ export default function brandStoreReducer(state=initialState, action) {
mult_brand_shop_type,
is_show_shop_name,
shop_domain,
shops_id
shops_id,
} = action.payload;
let info = state.info.set('isFetching', false)
.set('error', null)
... ... @@ -79,7 +79,8 @@ export default function brandStoreReducer(state=initialState, action) {
.set('mult_brand_shop_type', mult_brand_shop_type)
.set('is_show_shop_name', is_show_shop_name)
.set('shop_domain', shop_domain)
.set('shops_id', shops_id);
.set('shops_id', shops_id)
.set('is_addFav',false);
let nextState = state.set('info', info);
return nextState;
}
... ...
... ... @@ -99,4 +99,44 @@ export default class BrandStoreService {
});
}
async addFavorite(id, fav_id,uid, type='brand', gender=1, v=7) {
return await this.api.get({
url: '',
body: {
method: 'app.favorite.add',
id,
fav_id,
uid,
type,
gender,
v,
}
})
.then((json) => {
return json;
})
.catch((error) => {
throw(error);
});
}
async cancelFavorite(id, fav_id,uid, type='brand', gender=1, v=7) {
return await this.api.get({
url: '',
body: {
method: 'app.favorite.cancel',
id,
fav_id,
uid,
type,
gender,
v,
}
})
.then((json) => {
return json;
})
.catch((error) => {
throw(error);
});
}
}
... ...