|
1 | 1 | import { camelizeKeys } from 'humps'; |
2 | | -import moment from 'moment'; |
3 | | -import qs from 'qs'; |
4 | 2 |
|
5 | 3 | import { actions } from '../slices'; |
6 | 4 |
|
@@ -72,74 +70,4 @@ export const sendPremiumRequest = |
72 | 70 | } |
73 | 71 | }; |
74 | 72 |
|
75 | | -const periodToTimeUnit = { |
76 | | - weekly: 'week', |
77 | | - monthly: 'month', |
78 | | -}; |
79 | | - |
80 | | -const getDateByPeriod = (period: string) => { |
81 | | - if (period === 'total') { |
82 | | - return null; |
83 | | - } |
84 | | - return moment() |
85 | | - .startOf(periodToTimeUnit[period as keyof typeof periodToTimeUnit] as moment.unitOfTime.StartOf) |
86 | | - .utc() |
87 | | - .format('YYYY-MM-DD'); |
88 | | -}; |
89 | | - |
90 | | -interface UsersRatingFilter { |
91 | | - name?: string; |
92 | | - period: string; |
93 | | - withBots?: boolean; |
94 | | -} |
95 | | - |
96 | | -interface UsersRatingSort { |
97 | | - attribute: string; |
98 | | - direction: string; |
99 | | -} |
100 | | - |
101 | | -// Guards against stale responses: the rating search fires on every keystroke |
102 | | -// with no debounce, so a slow response for a short prefix (which ILIKE-matches |
103 | | -// many users) could resolve after the final query and clobber it with |
104 | | -// irrelevant results. Only the latest request's response is applied. |
105 | | -let latestRatingRequestId = 0; |
106 | | - |
107 | | -export const getUsersRatingPage = |
108 | | - ( |
109 | | - { name, period, withBots }: UsersRatingFilter, |
110 | | - { attribute, direction }: UsersRatingSort, |
111 | | - page: number, |
112 | | - pageSize: number, |
113 | | - ) => |
114 | | - (dispatch: any) => { |
115 | | - const queryParamsString = qs.stringify({ |
116 | | - page, |
117 | | - page_size: pageSize, |
118 | | - s: `${attribute}+${direction}`, |
119 | | - q: { |
120 | | - name_ilike: name, |
121 | | - }, |
122 | | - date_from: getDateByPeriod(period), |
123 | | - with_bots: withBots, |
124 | | - }); |
125 | | - |
126 | | - latestRatingRequestId += 1; |
127 | | - const requestId = latestRatingRequestId; |
128 | | - |
129 | | - requestJson(`/api/v1/users?${queryParamsString}`) |
130 | | - .then((data: any) => { |
131 | | - if (requestId !== latestRatingRequestId) { |
132 | | - return; |
133 | | - } |
134 | | - dispatch(actions.updateUsersRatingPage(camelizeKeys(data))); |
135 | | - dispatch(actions.finishStoreInit()); |
136 | | - }) |
137 | | - .catch((error: any) => { |
138 | | - if (requestId !== latestRatingRequestId) { |
139 | | - return; |
140 | | - } |
141 | | - dispatch(actions.setError(error)); |
142 | | - }); |
143 | | - }; |
144 | | - |
145 | 73 | export default loadUserStats; |
0 commit comments