RedemptionsTable.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Label, Message, Pagination, Table } from 'semantic-ui-react';
  3. import { Link } from 'react-router-dom';
  4. import { API, copy, showError, showInfo, showSuccess, showWarning, timestamp2string } from '../helpers';
  5. import { ITEMS_PER_PAGE } from '../constants';
  6. import { renderQuota } from '../helpers/render';
  7. function renderTimestamp(timestamp) {
  8. return (
  9. <>
  10. {timestamp2string(timestamp)}
  11. </>
  12. );
  13. }
  14. function renderStatus(status) {
  15. switch (status) {
  16. case 1:
  17. return <Label basic color='green'>未使用</Label>;
  18. case 2:
  19. return <Label basic color='red'> 已禁用 </Label>;
  20. case 3:
  21. return <Label basic color='grey'> 已使用 </Label>;
  22. default:
  23. return <Label basic color='black'> 未知状态 </Label>;
  24. }
  25. }
  26. const RedemptionsTable = () => {
  27. const [redemptions, setRedemptions] = useState([]);
  28. const [loading, setLoading] = useState(true);
  29. const [activePage, setActivePage] = useState(1);
  30. const [searchKeyword, setSearchKeyword] = useState('');
  31. const [searching, setSearching] = useState(false);
  32. const loadRedemptions = async (startIdx) => {
  33. const res = await API.get(`/api/redemption/?p=${startIdx}`);
  34. const { success, message, data } = res.data;
  35. if (success) {
  36. if (startIdx === 0) {
  37. setRedemptions(data);
  38. } else {
  39. let newRedemptions = redemptions;
  40. newRedemptions.push(...data);
  41. setRedemptions(newRedemptions);
  42. }
  43. } else {
  44. showError(message);
  45. }
  46. setLoading(false);
  47. };
  48. const onPaginationChange = (e, { activePage }) => {
  49. (async () => {
  50. if (activePage === Math.ceil(redemptions.length / ITEMS_PER_PAGE) + 1) {
  51. // In this case we have to load more data and then append them.
  52. await loadRedemptions(activePage - 1);
  53. }
  54. setActivePage(activePage);
  55. })();
  56. };
  57. useEffect(() => {
  58. loadRedemptions(0)
  59. .then()
  60. .catch((reason) => {
  61. showError(reason);
  62. });
  63. }, []);
  64. const manageRedemption = async (id, action, idx) => {
  65. let data = { id };
  66. let res;
  67. switch (action) {
  68. case 'delete':
  69. res = await API.delete(`/api/redemption/${id}/`);
  70. break;
  71. case 'enable':
  72. data.status = 1;
  73. res = await API.put('/api/redemption/?status_only=true', data);
  74. break;
  75. case 'disable':
  76. data.status = 2;
  77. res = await API.put('/api/redemption/?status_only=true', data);
  78. break;
  79. }
  80. const { success, message } = res.data;
  81. if (success) {
  82. showSuccess('操作成功完成!');
  83. let redemption = res.data.data;
  84. let newRedemptions = [...redemptions];
  85. let realIdx = (activePage - 1) * ITEMS_PER_PAGE + idx;
  86. if (action === 'delete') {
  87. newRedemptions[realIdx].deleted = true;
  88. } else {
  89. newRedemptions[realIdx].status = redemption.status;
  90. }
  91. setRedemptions(newRedemptions);
  92. } else {
  93. showError(message);
  94. }
  95. };
  96. const searchRedemptions = async () => {
  97. if (searchKeyword === '') {
  98. // if keyword is blank, load files instead.
  99. await loadRedemptions(0);
  100. setActivePage(1);
  101. return;
  102. }
  103. setSearching(true);
  104. const res = await API.get(`/api/redemption/search?keyword=${searchKeyword}`);
  105. const { success, message, data } = res.data;
  106. if (success) {
  107. setRedemptions(data);
  108. setActivePage(1);
  109. } else {
  110. showError(message);
  111. }
  112. setSearching(false);
  113. };
  114. const handleKeywordChange = async (e, { value }) => {
  115. setSearchKeyword(value.trim());
  116. };
  117. const sortRedemption = (key) => {
  118. if (redemptions.length === 0) return;
  119. setLoading(true);
  120. let sortedRedemptions = [...redemptions];
  121. sortedRedemptions.sort((a, b) => {
  122. return ('' + a[key]).localeCompare(b[key]);
  123. });
  124. if (sortedRedemptions[0].id === redemptions[0].id) {
  125. sortedRedemptions.reverse();
  126. }
  127. setRedemptions(sortedRedemptions);
  128. setLoading(false);
  129. };
  130. return (
  131. <>
  132. <Form onSubmit={searchRedemptions}>
  133. <Form.Input
  134. icon='search'
  135. fluid
  136. iconPosition='left'
  137. placeholder='搜索兑换码的 ID 和名称 ...'
  138. value={searchKeyword}
  139. loading={searching}
  140. onChange={handleKeywordChange}
  141. />
  142. </Form>
  143. <Table basic compact size='small'>
  144. <Table.Header>
  145. <Table.Row>
  146. <Table.HeaderCell
  147. style={{ cursor: 'pointer' }}
  148. onClick={() => {
  149. sortRedemption('id');
  150. }}
  151. >
  152. ID
  153. </Table.HeaderCell>
  154. <Table.HeaderCell
  155. style={{ cursor: 'pointer' }}
  156. onClick={() => {
  157. sortRedemption('name');
  158. }}
  159. >
  160. 名称
  161. </Table.HeaderCell>
  162. <Table.HeaderCell
  163. style={{ cursor: 'pointer' }}
  164. onClick={() => {
  165. sortRedemption('status');
  166. }}
  167. >
  168. 状态
  169. </Table.HeaderCell>
  170. <Table.HeaderCell
  171. style={{ cursor: 'pointer' }}
  172. onClick={() => {
  173. sortRedemption('quota');
  174. }}
  175. >
  176. 额度
  177. </Table.HeaderCell>
  178. <Table.HeaderCell
  179. style={{ cursor: 'pointer' }}
  180. onClick={() => {
  181. sortRedemption('created_time');
  182. }}
  183. >
  184. 创建时间
  185. </Table.HeaderCell>
  186. <Table.HeaderCell
  187. style={{ cursor: 'pointer' }}
  188. onClick={() => {
  189. sortRedemption('redeemed_time');
  190. }}
  191. >
  192. 兑换时间
  193. </Table.HeaderCell>
  194. <Table.HeaderCell>操作</Table.HeaderCell>
  195. </Table.Row>
  196. </Table.Header>
  197. <Table.Body>
  198. {redemptions
  199. .slice(
  200. (activePage - 1) * ITEMS_PER_PAGE,
  201. activePage * ITEMS_PER_PAGE
  202. )
  203. .map((redemption, idx) => {
  204. if (redemption.deleted) return <></>;
  205. return (
  206. <Table.Row key={redemption.id}>
  207. <Table.Cell>{redemption.id}</Table.Cell>
  208. <Table.Cell>{redemption.name ? redemption.name : '无'}</Table.Cell>
  209. <Table.Cell>{renderStatus(redemption.status)}</Table.Cell>
  210. <Table.Cell>{renderQuota(redemption.quota)}</Table.Cell>
  211. <Table.Cell>{renderTimestamp(redemption.created_time)}</Table.Cell>
  212. <Table.Cell>{redemption.redeemed_time ? renderTimestamp(redemption.redeemed_time) : "尚未兑换"} </Table.Cell>
  213. <Table.Cell>
  214. <div>
  215. <Button
  216. size={'small'}
  217. positive
  218. onClick={async () => {
  219. if (await copy(redemption.key)) {
  220. showSuccess('已复制到剪贴板!');
  221. } else {
  222. showWarning('无法复制到剪贴板,请手动复制,已将兑换码填入搜索框。')
  223. setSearchKeyword(redemption.key);
  224. }
  225. }}
  226. >
  227. 复制
  228. </Button>
  229. <Button
  230. size={'small'}
  231. negative
  232. onClick={() => {
  233. manageRedemption(redemption.id, 'delete', idx);
  234. }}
  235. >
  236. 删除
  237. </Button>
  238. <Button
  239. size={'small'}
  240. disabled={redemption.status === 3} // used
  241. onClick={() => {
  242. manageRedemption(
  243. redemption.id,
  244. redemption.status === 1 ? 'disable' : 'enable',
  245. idx
  246. );
  247. }}
  248. >
  249. {redemption.status === 1 ? '禁用' : '启用'}
  250. </Button>
  251. <Button
  252. size={'small'}
  253. as={Link}
  254. to={'/redemption/edit/' + redemption.id}
  255. >
  256. 编辑
  257. </Button>
  258. </div>
  259. </Table.Cell>
  260. </Table.Row>
  261. );
  262. })}
  263. </Table.Body>
  264. <Table.Footer>
  265. <Table.Row>
  266. <Table.HeaderCell colSpan='8'>
  267. <Button size='small' as={Link} to='/redemption/add' loading={loading}>
  268. 添加新的兑换码
  269. </Button>
  270. <Pagination
  271. floated='right'
  272. activePage={activePage}
  273. onPageChange={onPaginationChange}
  274. size='small'
  275. siblingRange={1}
  276. totalPages={
  277. Math.ceil(redemptions.length / ITEMS_PER_PAGE) +
  278. (redemptions.length % ITEMS_PER_PAGE === 0 ? 1 : 0)
  279. }
  280. />
  281. </Table.HeaderCell>
  282. </Table.Row>
  283. </Table.Footer>
  284. </Table>
  285. </>
  286. );
  287. };
  288. export default RedemptionsTable;