Authored by 于良

添加店铺收藏 review by days

... ... @@ -28,7 +28,8 @@ export default class ShopRecommend extends Component{
}
shouldComponentUpdate(nextProps){
if (Immutable.is(nextProps.data, this.props.data)) {
if (Immutable.is(nextProps.data, this.props.data)
&& Immutable.is(nextProps.favoriteState, this.props.favoriteState)) {
return false;
} else {
return true;
... ...
... ... @@ -36,4 +36,11 @@ export default keyMirror({
HOME_BOTTOM_BANNER_SUCCESS: null,
HOME_BOTTOM_BANNER_FAILURE: null,
SHOP_FAV_ADD_REQUEST: null,
SHOP_FAV_ADD_SUCCESS: null,
SHOP_FAV_ADD_FAILURE: null,
SHOP_FAV_CANCEL_REQUEST: null,
SHOP_FAV_CANCEL_SUCCESS: null,
SHOP_FAV_CANCEL_FAILURE: null,
});
... ...
... ... @@ -167,7 +167,7 @@ class HomeContainer extends Component {
}
onPressShopFavorite(shopId, index) {
console.log(shopId)
this.props.actions.favoriteOperation(shopId);
}
onPressBrandItem(url, index) {
... ...
... ... @@ -35,6 +35,14 @@ const {
HOME_BOTTOM_BANNER_SUCCESS,
HOME_BOTTOM_BANNER_FAILURE,
SHOP_FAV_ADD_REQUEST,
SHOP_FAV_ADD_SUCCESS,
SHOP_FAV_ADD_FAILURE,
SHOP_FAV_CANCEL_REQUEST,
SHOP_FAV_CANCEL_SUCCESS,
SHOP_FAV_CANCEL_FAILURE,
} = require('../../constants/actionTypes').default;
/**
... ... @@ -399,6 +407,128 @@ function fetchShopInfo(data, channel) {
}
}
export function favoriteOperation(shopId) {
return (dispatch, getState) => {
let {app, home} = getState();
let channelStr = channelTransfer.number2String(app.channel);
let shopState = home[channelStr].shop.get('list');
let state = shopState.get(shopId, '');
if (!state) {
return;
}
let resultNumber = parseInt(state.get('collectionNum', 0));
if (state.get('favorite')) {
resultNumber = resultNumber > 0 ? (resultNumber - 1) : 0;
dispatch(cancelFavorite(shopId, resultNumber));
} else {
resultNumber = resultNumber + 1;
dispatch(addFavorite(shopId, resultNumber));
}
}
}
function addFavoritRequest(channelStr) {
return {
type: SHOP_FAV_ADD_REQUEST,
payload: channelStr
};
}
function addFavoritSuccess(channelStr, resultNumber, shopId, collected) {
return {
type: SHOP_FAV_ADD_SUCCESS,
payload: {channelStr, resultNumber, shopId, collected}
};
}
function addFavoritFailure(error, channelStr) {
return {
type: SHOP_FAV_ADD_FAILURE,
payload: {channelStr, error}
};
}
export function addFavorite(shopId, resultNumber) {
return (dispatch, getState) => {
let {app, home} = getState();
let channelStr = channelTransfer.number2String(app.channel);
let favoriteRequest = (shopId, uid) => {
dispatch(addFavoritRequest(channelStr));
dispatch(addFavoritSuccess(channelStr, resultNumber, shopId, true));
return new HomeService(app.host).addFavorite(shopId, uid)
.then(json => {
})
.catch(error => {
dispatch(addFavoritFailure(error, channelStr));
});
}
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
favoriteRequest(shopId, uid);
})
.catch(error => {
ReactNative.NativeModules.YH_CommonHelper.login()
.then(uid => {
favoriteRequest(shopId, uid);
})
.catch(error => {
});
});
};
}
function cancelFavoritRequest(channelStr) {
return {
type: SHOP_FAV_ADD_REQUEST,
payload: channelStr
};
}
function cancelFavoritSuccess(channelStr, resultNumber, shopId, collected) {
return {
type: SHOP_FAV_ADD_SUCCESS,
payload: {channelStr, resultNumber, shopId, collected}
};
}
function cancelFavoritFailure(error, channelStr) {
return {
type: SHOP_FAV_ADD_FAILURE,
payload: {channelStr, error}
};
}
export function cancelFavorite(shopId, resultNumber) {
return (dispatch, getState) => {
let {app, home} = getState();
let channelStr = channelTransfer.number2String(app.channel);
dispatch(cancelFavoritRequest(channelStr));
dispatch(cancelFavoritSuccess(channelStr, resultNumber, shopId, false));
ReactNative.NativeModules.YH_CommonHelper.uid()
.then(uid => {
return new HomeService(app.host).cancelFavorite(shopId, uid)
.then(json => {
})
.catch(error => {
dispatch(cancelFavoritFailure(error, channelStr));
});
})
.catch(error => {
});
};
}
/******** 首页楼层 *********/
// 开始请求
export function fetchFloorRequest(channelStr) {
... ...
... ... @@ -32,6 +32,14 @@ const {
HOME_BOTTOM_BANNER_SUCCESS,
HOME_BOTTOM_BANNER_FAILURE,
SHOP_FAV_ADD_REQUEST,
SHOP_FAV_ADD_SUCCESS,
SHOP_FAV_ADD_FAILURE,
SHOP_FAV_CANCEL_REQUEST,
SHOP_FAV_CANCEL_SUCCESS,
SHOP_FAV_CANCEL_FAILURE,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
... ... @@ -142,6 +150,24 @@ export default function homeReducer(state=initialState, action) {
}
/****************************
*** 更新店铺收藏状态
*****************************/
case SHOP_FAV_ADD_REQUEST:
case SHOP_FAV_CANCEL_REQUEST:
case SHOP_FAV_ADD_FAILURE:
case SHOP_FAV_CANCEL_FAILURE:{
return state;
}
case SHOP_FAV_ADD_SUCCESS:
case SHOP_FAV_CANCEL_SUCCESS:{
let {channelStr, resultNumber, shopId, collected} = action.payload;
let newState = state.setIn([channelStr, 'shop', 'list', shopId, 'collectionNum'], resultNumber)
.setIn([channelStr, 'shop', 'list', shopId, 'favorite'], collected);
return newState;
}
/****************************
*** 获取缓存信息
*****************************/
case LOAD_CHANNEL_CACHED_DATA:{
... ...
... ... @@ -135,4 +135,42 @@ export default class HomeService {
throw(error);
});
}
async addFavorite(id='', uid=0, type='shop', v=7) {
return await this.api.get({
url: '',
body: {
method: 'app.favorite.add',
id,
uid,
type,
v,
}
})
.then((json) => {
return json;
})
.catch((error) => {
throw(error);
});
}
async cancelFavorite(fav_id='', uid=0, type='shop', v=7) {
return await this.api.get({
url: '',
body: {
method: 'app.favorite.cancel',
fav_id,
uid,
type,
v,
}
})
.then((json) => {
return json;
})
.catch((error) => {
throw(error);
});
}
}
... ...