shareDetailActions.js
3.09 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
'use strict';
import ReactNative from 'react-native';
import Service from '../../services/ShareDetailService';
const {
SHARE_DETAIL_REQUEST,
SHARE_DETAIL_SUCCESS,
SHARE_DETAIL_FAILURE,
ADD_FAVORITE_REQUEST,
ADD_FAVORITE_SUCCESS,
ADD_FAVORITE_FAILURE,
FAVORITE_INFO_REQUEST,
FAVORITE_INFO_FAILURE,
} = require('../../constants/actionTypes').default;
export function shareDetailRequest() {
return {
type: SHARE_DETAIL_REQUEST
}
}
export function shareDetailSuccess(json) {
return {
type: SHARE_DETAIL_SUCCESS,
payload: json
}
}
export function shareDetailFailue(error) {
return {
type: SHARE_DETAIL_FAILURE,
payload: error
}
}
export function fetchShareDetail(params) {
return async (dispatch, getState) => {
let { app: { host } } = getState();
dispatch(shareDetailRequest());
return new Service(host).fetchShareDetail(params)
.then(json => {
dispatch(shareDetailSuccess(json));
})
.catch(error => {
dispatch(shareDetailFailue(error));
});
}
}
export function addFavoriteRequest() {
return {
type: ADD_FAVORITE_REQUEST,
}
}
export function addFavoriteSuccess(json) {
return {
type: ADD_FAVORITE_SUCCESS,
payload: json
}
}
export function addFavoriteFailure(e) {
return {
type: ADD_FAVORITE_FAILURE,
payload: e
}
}
export function addFavorite(params) {
return async (dispatch, getState) => {
let { app: { host }, shareDetail } = getState();
let uid, data;
let isCollect = shareDetail.get('isCollect');
isCollect === 'Y' && (params.fav_id = params.id)
let service = new Service(host);
dispatch(addFavoriteRequest());
try {
uid = await ReactNative.NativeModules.YH_CommonHelper.uid();
params = {uid, ...params};
if (isCollect === 'Y') {
data = await service.cancelFavorite(params);
} else {
data = await service.addFavorite(params);
}
} catch(e) {
dispatch(addFavoriteFailure(e));
}
dispatch(addFavoriteSuccess(isCollect === 'Y' ? 'N' : 'Y'));
}
}
export function favoriteInfoRequest() {
return {
type: FAVORITE_INFO_REQUEST,
}
}
export function favoriteInfoFailure(e) {
return {
type: FAVORITE_INFO_FAILURE,
payload: e,
}
}
export function fetchFavoriteInfo(params) {
return async (dispatch, getState) => {
let { app: { host } } = getState();
let uid, data;
let service = new Service(host);
dispatch(favoriteInfoRequest());
try {
uid = await ReactNative.NativeModules.YH_CommonHelper.uid();
params = {
method: 'app.favorite.isFavoriteNew',
uid,
...params
};
data = await service.fetchInfo(params);
} catch(e) {
dispatch(favoriteInfoFailure(e));
}
dispatch(addFavoriteSuccess(data ? 'Y' : 'N'));
}
}