-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchannel.action.ts
62 lines (57 loc) · 2.42 KB
/
channel.action.ts
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
import { instance } from "../helpers";
import { AxiosResponse } from 'axios';
import { Dispatch } from 'redux';
import { IState } from "../reducers";
import { CHANNELS, DISCOVERY_CHANNEL, NETWORK_STATUS } from "../constants/action-types.constant";
import { SET, UPDATE_PROPERTY } from "../constants/actions.constant";
import { I5singChannel, I5singResponse } from "../interfaces/i5sing";
import { IChannel } from "../interfaces";
export class ChannelAction {
public static getChannels(page: number = 1, pageSize?: number) {
return async (dispatch: Dispatch, state: () => IState) => {
dispatch({
type: NETWORK_STATUS,
action: UPDATE_PROPERTY,
path: `${CHANNELS}_${SET}`,
data: { loading: true, nodata: false }
});
const url = 'http://mobileapi.5sing.kugou.com/song/GetRecommendSingle';
const query = { channel_id: 1, page, pagesize: pageSize };
const response: AxiosResponse<I5singResponse<I5singChannel[]>> = await instance.get(
url,
{ params: query }
);
let channels = response.data.data.map((item: I5singChannel) => ({
picture: item.pic,
id: item.id,
name: item.name,
songName: item.song_name,
time: item.time,
words: item.words,
playTime: item.play_time,
type: item.type,
click: Number(item.click),
url: item.url,
user: {
id: Number(item.user_id),
nickname: item.nickname,
image: item.user_pic,
}
})) as IChannel[];
if (page === 1) {
dispatch({ type: DISCOVERY_CHANNEL, action: SET, data: channels.slice(0, 6) });
dispatch({ type: CHANNELS, action: SET, data: channels });
} else {
let existChannels: IChannel[] = state().channels;
channels = existChannels.concat(channels);
dispatch({ type: CHANNELS, action: SET, data: channels });
}
dispatch({
type: NETWORK_STATUS,
action: UPDATE_PROPERTY,
path: `${CHANNELS}_${SET}`,
data: { loading: false, nodata: response.data.data.length === 0 }
});
}
}
}