-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
164 lines (139 loc) · 4.57 KB
/
index.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pathToRegexp from 'path-to-regexp';
import { routerRedux } from 'dva/router';
import {
fetchTopics,
fetchTopic,
fetchHotTopics,
fetchNoReplyTopics,
fetchAuthorTopics,
addTopic,
updateTopic,
deleteTopic,
fetchTopicComments,
addTopicComment,
updateTopicComment,
deleteTopicComment,
fetchUser,
} from '../../services/cibuci';
export default {
namespace: 'topic',
state: {
type: 'all',
current: null,
comments: [],
list: [],
total: 0,
page: 1,
hot: [],
noreply: [],
author: [],
currentTabId: 'all',
tabs: [
{ id: 'all', name: '全部' },
{ id: 'good', name: '优质话题' },
{ id: 'share', name: '分享' },
{ id: 'ask', name: '提问' },
{ id: 'bb', name: '吐槽' },
],
},
subscriptions: {
setup({ dispatch, history }) { // eslint-disable-line
},
list({ dispatch, history }) {
return history.listen(({ pathname, query }) => {
const tab = query.tab || 'all';
const page = parseInt(query.page, 10) || 1;
const match = pathToRegexp('/topic').exec(pathname);
if (match) {
dispatch({ type: 'fetchList', payload: { tab, page } });
dispatch({ type: 'fetchHot' });
}
});
},
item({ dispatch, history }) {
return history.listen(({ pathname }) => {
const match = pathToRegexp('/topic/:itemId').exec(pathname);
if (match) {
const id = match[1];
if (id !== 'add') {
dispatch({ type: 'fetchItem', payload: { id } });
dispatch({ type: 'fetchComments', payload: { id } });
}
}
});
},
},
effects: {
* fetch({ payload }, { call, put }) { // eslint-disable-line
yield put({ type: 'save' });
},
* fetchList({ payload }, { put, call }) {
const { tab, page } = payload;
const list = yield call(fetchTopics, tab, page);
yield put({ type: 'saveList', payload: { ...list, tab, page } });
},
* fetchHot({ payload }, { put, call }) { // eslint-disable-line
const list = yield call(fetchHotTopics);
yield put({ type: 'save', payload: { hot: list.data } });
},
* fetchNoReply({ payload }, { put, call }) {
const list = yield call(fetchNoReplyTopics);
yield put({ type: 'save', payload: { noreply: list.data } });
},
* fetchAuthor({ payload }, { put, call }) {
const { userId } = payload;
const list = yield call(fetchAuthorTopics, userId);
yield put({ type: 'save', payload: { author: list.data } });
},
* fetchItem({ payload }, { put, call }) {
const { id } = payload;
const item = yield call(fetchTopic, id);
const author = yield call(fetchUser, item.data.authorId);
const topic = { ...item.data, author: author.data };
yield put({ type: 'saveItem', payload: topic });
},
* addItem({ payload }, { put, call }) { // eslint-disable-line
yield call(addTopic, payload);
yield put(routerRedux.push('/topic'));
},
* editItem({ payload }, { put, call }) { // eslint-disable-line
yield call(updateTopic, payload);
yield put(routerRedux.push(`/topic/${payload.id}`));
},
* removeItem({ payload }, { put, call }) { // eslint-disable-line
yield call(deleteTopic, payload);
yield put(routerRedux.push('/topic'));
},
* fetchComments({ payload }, { put, call }) { // eslint-disable-line
const { id } = payload;
const result = yield call(fetchTopicComments, id);
yield put({ type: 'save', payload: { comments: result.data } });
},
* addComment({ payload }, { put, call }) { // eslint-disable-line
yield call(addTopicComment, payload);
yield put({ type: 'fetchComments', payload: { id: payload.current.id } });
},
* editComment({ payload }, { put, call }) {
const { id, data } = payload;
yield call(updateTopicComment, { id, data });
yield put({ type: 'fetchComments', payload: { id: payload.topicId } });
},
* removeComment({ payload }, { put, call }) { // eslint-disable-line
const { id } = payload;
yield call(deleteTopicComment, { id });
yield put({ type: 'fetchComments', payload: { id: payload.topicId } });
},
},
reducers: {
save(state, action) {
return { ...state, ...action.payload };
},
saveList(state, { payload }) {
const { data, total, tab, page } = payload;
return { ...state, list: data, total, currentTabId: tab, page };
},
saveItem(state, { payload: item }) {
return { ...state, current: item };
},
},
};