-
Notifications
You must be signed in to change notification settings - Fork 389
/
playerCountsActions.js
86 lines (76 loc) · 2.12 KB
/
playerCountsActions.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
/* global API_HOST */
import fetch from 'isomorphic-fetch';
import patch from 'dotaconstants/build/patch.json';
import region from 'dotaconstants/build/region.json';
import {
playerCounts,
} from 'reducers';
import {
getUrl,
} from 'actions/utility';
import {
getPercentWin,
} from 'utility';
import strings from 'lang';
const url = playerId => `/api/players/${playerId}/counts`;
const REQUEST = 'playerCounts/REQUEST';
const OK = 'playerCounts/OK';
const ERROR = 'playerCounts/ERROR';
export const playerCountsActions = {
REQUEST,
OK,
ERROR,
};
export const getPlayerCountsRequest = id => ({
type: REQUEST,
id,
});
export const getPlayerCountsOk = (payload, id) => ({
type: OK,
payload,
id,
});
export const getPlayerCountsError = (payload, id) => ({
type: ERROR,
payload,
id,
});
const patchLookup = {};
patch.forEach((patchElement, index) => {
patchLookup[index] = patchElement.name;
});
const countTypes = {
patch: patchLookup,
region,
is_radiant: {
0: strings.general_dire,
1: strings.general_radiant,
},
};
export const getPlayerCounts = (playerId, options = {}) => (dispatch, getState) => {
if (playerCounts.isLoaded(getState(), playerId)) {
dispatch(getPlayerCountsOk(playerCounts.getCountsList(getState(), playerId), playerId));
} else {
dispatch(getPlayerCountsRequest(playerId));
}
return fetch(`${API_HOST}${getUrl(playerId, options, url)}`)
.then(response => response.json())
.then((json) => {
const data = {};
Object.keys(json).forEach((key) => {
// Translate each ID to a string
data[key] = {
name: key,
list: Object.keys(json[key])
.map(innerKey => ({
category: strings[`${key}_${innerKey}`] || (countTypes[key] && countTypes[key][innerKey]) || innerKey,
matches: json[key][innerKey].games,
winPercent: getPercentWin(json[key][innerKey].win, json[key][innerKey].games),
})),
};
});
return data;
})
.then(json => dispatch(getPlayerCountsOk(json, playerId)))
.catch(error => dispatch(getPlayerCountsError(error, playerId)));
};