Authored by 于良

添加店铺收藏 review by days

@@ -28,7 +28,8 @@ export default class ShopRecommend extends Component{ @@ -28,7 +28,8 @@ export default class ShopRecommend extends Component{
28 } 28 }
29 29
30 shouldComponentUpdate(nextProps){ 30 shouldComponentUpdate(nextProps){
31 - if (Immutable.is(nextProps.data, this.props.data)) { 31 + if (Immutable.is(nextProps.data, this.props.data)
  32 + && Immutable.is(nextProps.favoriteState, this.props.favoriteState)) {
32 return false; 33 return false;
33 } else { 34 } else {
34 return true; 35 return true;
@@ -36,4 +36,11 @@ export default keyMirror({ @@ -36,4 +36,11 @@ export default keyMirror({
36 HOME_BOTTOM_BANNER_SUCCESS: null, 36 HOME_BOTTOM_BANNER_SUCCESS: null,
37 HOME_BOTTOM_BANNER_FAILURE: null, 37 HOME_BOTTOM_BANNER_FAILURE: null,
38 38
  39 + SHOP_FAV_ADD_REQUEST: null,
  40 + SHOP_FAV_ADD_SUCCESS: null,
  41 + SHOP_FAV_ADD_FAILURE: null,
  42 +
  43 + SHOP_FAV_CANCEL_REQUEST: null,
  44 + SHOP_FAV_CANCEL_SUCCESS: null,
  45 + SHOP_FAV_CANCEL_FAILURE: null,
39 }); 46 });
@@ -167,7 +167,7 @@ class HomeContainer extends Component { @@ -167,7 +167,7 @@ class HomeContainer extends Component {
167 } 167 }
168 168
169 onPressShopFavorite(shopId, index) { 169 onPressShopFavorite(shopId, index) {
170 - console.log(shopId) 170 + this.props.actions.favoriteOperation(shopId);
171 } 171 }
172 172
173 onPressBrandItem(url, index) { 173 onPressBrandItem(url, index) {
@@ -35,6 +35,14 @@ const { @@ -35,6 +35,14 @@ const {
35 HOME_BOTTOM_BANNER_SUCCESS, 35 HOME_BOTTOM_BANNER_SUCCESS,
36 HOME_BOTTOM_BANNER_FAILURE, 36 HOME_BOTTOM_BANNER_FAILURE,
37 37
  38 + SHOP_FAV_ADD_REQUEST,
  39 + SHOP_FAV_ADD_SUCCESS,
  40 + SHOP_FAV_ADD_FAILURE,
  41 +
  42 + SHOP_FAV_CANCEL_REQUEST,
  43 + SHOP_FAV_CANCEL_SUCCESS,
  44 + SHOP_FAV_CANCEL_FAILURE,
  45 +
38 } = require('../../constants/actionTypes').default; 46 } = require('../../constants/actionTypes').default;
39 47
40 /** 48 /**
@@ -399,6 +407,128 @@ function fetchShopInfo(data, channel) { @@ -399,6 +407,128 @@ function fetchShopInfo(data, channel) {
399 } 407 }
400 } 408 }
401 409
  410 +export function favoriteOperation(shopId) {
  411 + return (dispatch, getState) => {
  412 + let {app, home} = getState();
  413 + let channelStr = channelTransfer.number2String(app.channel);
  414 + let shopState = home[channelStr].shop.get('list');
  415 + let state = shopState.get(shopId, '');
  416 + if (!state) {
  417 + return;
  418 + }
  419 +
  420 + let resultNumber = parseInt(state.get('collectionNum', 0));
  421 + if (state.get('favorite')) {
  422 + resultNumber = resultNumber > 0 ? (resultNumber - 1) : 0;
  423 + dispatch(cancelFavorite(shopId, resultNumber));
  424 + } else {
  425 + resultNumber = resultNumber + 1;
  426 + dispatch(addFavorite(shopId, resultNumber));
  427 + }
  428 + }
  429 +}
  430 +
  431 +function addFavoritRequest(channelStr) {
  432 + return {
  433 + type: SHOP_FAV_ADD_REQUEST,
  434 + payload: channelStr
  435 + };
  436 +}
  437 +
  438 +function addFavoritSuccess(channelStr, resultNumber, shopId, collected) {
  439 + return {
  440 + type: SHOP_FAV_ADD_SUCCESS,
  441 + payload: {channelStr, resultNumber, shopId, collected}
  442 + };
  443 +}
  444 +
  445 +function addFavoritFailure(error, channelStr) {
  446 + return {
  447 + type: SHOP_FAV_ADD_FAILURE,
  448 + payload: {channelStr, error}
  449 + };
  450 +}
  451 +
  452 +export function addFavorite(shopId, resultNumber) {
  453 + return (dispatch, getState) => {
  454 + let {app, home} = getState();
  455 +
  456 + let channelStr = channelTransfer.number2String(app.channel);
  457 +
  458 + let favoriteRequest = (shopId, uid) => {
  459 + dispatch(addFavoritRequest(channelStr));
  460 + dispatch(addFavoritSuccess(channelStr, resultNumber, shopId, true));
  461 + return new HomeService(app.host).addFavorite(shopId, uid)
  462 + .then(json => {
  463 +
  464 + })
  465 + .catch(error => {
  466 + dispatch(addFavoritFailure(error, channelStr));
  467 + });
  468 + }
  469 +
  470 + ReactNative.NativeModules.YH_CommonHelper.uid()
  471 + .then(uid => {
  472 + favoriteRequest(shopId, uid);
  473 + })
  474 + .catch(error => {
  475 + ReactNative.NativeModules.YH_CommonHelper.login()
  476 + .then(uid => {
  477 + favoriteRequest(shopId, uid);
  478 + })
  479 + .catch(error => {
  480 +
  481 + });
  482 + });
  483 + };
  484 +}
  485 +
  486 +function cancelFavoritRequest(channelStr) {
  487 + return {
  488 + type: SHOP_FAV_ADD_REQUEST,
  489 + payload: channelStr
  490 + };
  491 +}
  492 +
  493 +function cancelFavoritSuccess(channelStr, resultNumber, shopId, collected) {
  494 + return {
  495 + type: SHOP_FAV_ADD_SUCCESS,
  496 + payload: {channelStr, resultNumber, shopId, collected}
  497 + };
  498 +}
  499 +
  500 +function cancelFavoritFailure(error, channelStr) {
  501 + return {
  502 + type: SHOP_FAV_ADD_FAILURE,
  503 + payload: {channelStr, error}
  504 + };
  505 +}
  506 +
  507 +export function cancelFavorite(shopId, resultNumber) {
  508 + return (dispatch, getState) => {
  509 + let {app, home} = getState();
  510 +
  511 + let channelStr = channelTransfer.number2String(app.channel);
  512 +
  513 + dispatch(cancelFavoritRequest(channelStr));
  514 + dispatch(cancelFavoritSuccess(channelStr, resultNumber, shopId, false));
  515 +
  516 + ReactNative.NativeModules.YH_CommonHelper.uid()
  517 + .then(uid => {
  518 + return new HomeService(app.host).cancelFavorite(shopId, uid)
  519 + .then(json => {
  520 +
  521 + })
  522 + .catch(error => {
  523 + dispatch(cancelFavoritFailure(error, channelStr));
  524 + });
  525 + })
  526 + .catch(error => {
  527 +
  528 + });
  529 + };
  530 +}
  531 +
402 /******** 首页楼层 *********/ 532 /******** 首页楼层 *********/
403 // 开始请求 533 // 开始请求
404 export function fetchFloorRequest(channelStr) { 534 export function fetchFloorRequest(channelStr) {
@@ -32,6 +32,14 @@ const { @@ -32,6 +32,14 @@ const {
32 HOME_BOTTOM_BANNER_SUCCESS, 32 HOME_BOTTOM_BANNER_SUCCESS,
33 HOME_BOTTOM_BANNER_FAILURE, 33 HOME_BOTTOM_BANNER_FAILURE,
34 34
  35 + SHOP_FAV_ADD_REQUEST,
  36 + SHOP_FAV_ADD_SUCCESS,
  37 + SHOP_FAV_ADD_FAILURE,
  38 +
  39 + SHOP_FAV_CANCEL_REQUEST,
  40 + SHOP_FAV_CANCEL_SUCCESS,
  41 + SHOP_FAV_CANCEL_FAILURE,
  42 +
35 } = require('../../constants/actionTypes').default; 43 } = require('../../constants/actionTypes').default;
36 44
37 const initialState = new InitialState; 45 const initialState = new InitialState;
@@ -142,6 +150,24 @@ export default function homeReducer(state=initialState, action) { @@ -142,6 +150,24 @@ export default function homeReducer(state=initialState, action) {
142 } 150 }
143 151
144 /**************************** 152 /****************************
  153 + *** 更新店铺收藏状态
  154 + *****************************/
  155 + case SHOP_FAV_ADD_REQUEST:
  156 + case SHOP_FAV_CANCEL_REQUEST:
  157 + case SHOP_FAV_ADD_FAILURE:
  158 + case SHOP_FAV_CANCEL_FAILURE:{
  159 + return state;
  160 + }
  161 +
  162 + case SHOP_FAV_ADD_SUCCESS:
  163 + case SHOP_FAV_CANCEL_SUCCESS:{
  164 + let {channelStr, resultNumber, shopId, collected} = action.payload;
  165 + let newState = state.setIn([channelStr, 'shop', 'list', shopId, 'collectionNum'], resultNumber)
  166 + .setIn([channelStr, 'shop', 'list', shopId, 'favorite'], collected);
  167 + return newState;
  168 + }
  169 +
  170 + /****************************
145 *** 获取缓存信息 171 *** 获取缓存信息
146 *****************************/ 172 *****************************/
147 case LOAD_CHANNEL_CACHED_DATA:{ 173 case LOAD_CHANNEL_CACHED_DATA:{
@@ -135,4 +135,42 @@ export default class HomeService { @@ -135,4 +135,42 @@ export default class HomeService {
135 throw(error); 135 throw(error);
136 }); 136 });
137 } 137 }
  138 +
  139 + async addFavorite(id='', uid=0, type='shop', v=7) {
  140 + return await this.api.get({
  141 + url: '',
  142 + body: {
  143 + method: 'app.favorite.add',
  144 + id,
  145 + uid,
  146 + type,
  147 + v,
  148 + }
  149 + })
  150 + .then((json) => {
  151 + return json;
  152 + })
  153 + .catch((error) => {
  154 + throw(error);
  155 + });
  156 + }
  157 +
  158 + async cancelFavorite(fav_id='', uid=0, type='shop', v=7) {
  159 + return await this.api.get({
  160 + url: '',
  161 + body: {
  162 + method: 'app.favorite.cancel',
  163 + fav_id,
  164 + uid,
  165 + type,
  166 + v,
  167 + }
  168 + })
  169 + .then((json) => {
  170 + return json;
  171 + })
  172 + .catch((error) => {
  173 + throw(error);
  174 + });
  175 + }
138 } 176 }