index.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { Button, message } from 'antd';
  2. import React, { useEffect } from 'react';
  3. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  4. import { useSingleState } from 'nice-hooks';
  5. import ReactQuill from 'react-quill';
  6. import 'react-quill/dist/quill.snow.css';
  7. import { Memory } from './data.d';
  8. import service from './service';
  9. enum Step {
  10. MAIN = 0,
  11. MEMORY = 1,
  12. ADD = 2,
  13. }
  14. interface State {
  15. remindCount: number;
  16. step: Step;
  17. showBack?: boolean;
  18. }
  19. const TableList: React.FC<{}> = () => {
  20. const [state, setState] = useSingleState<State>({
  21. remindCount: 0,
  22. step: Step.MAIN, // 0 主页,1 复习,2 添加
  23. showBack: false,
  24. });
  25. const [memory, setMemory] = useSingleState<Memory>({
  26. id: null,
  27. userId: null,
  28. front: '',
  29. back: '',
  30. tag: '',
  31. });
  32. /**
  33. * 查询用户待提醒数量
  34. * @param fields
  35. */
  36. const countRemind = async () => {
  37. const hide = message.loading('正在查询');
  38. try {
  39. const res = await service.countRemind({});
  40. hide();
  41. if (res.success) {
  42. setState({ remindCount: parseInt(res.data, 10) });
  43. return true;
  44. }
  45. return false;
  46. } catch (error) {
  47. hide();
  48. message.error('查询异常!');
  49. return false;
  50. }
  51. };
  52. useEffect(() => {
  53. countRemind();
  54. }, []);
  55. useEffect(() => {
  56. // console.log(state)
  57. // console.log(memory)
  58. });
  59. /**
  60. * 进入统计页面
  61. */
  62. const changStepMain = () => {
  63. setState({ step: Step.MAIN });
  64. countRemind();
  65. };
  66. const findNextLine = async () => {
  67. const res = await service.findNext({});
  68. if (res.success) {
  69. setMemory({ ...memory, ...res.data });
  70. } else {
  71. changStepMain();
  72. }
  73. };
  74. /**
  75. * 初次进入复习页面
  76. */
  77. const changStepMemory = async () => {
  78. if (state.remindCount <= 0) {
  79. message.info('已经复习完了哟!');
  80. return;
  81. }
  82. setState({ step: Step.MEMORY });
  83. await findNextLine();
  84. };
  85. const memorySuccess = async (factorInt: number) => {
  86. const res = await service.memorySuccess({ id: memory.id, factor: factorInt });
  87. if (res.success) {
  88. setMemory({ ...memory, ...res.data });
  89. } else {
  90. changStepMain();
  91. }
  92. };
  93. const changStepAdd = () => {
  94. setState({ step: Step.ADD });
  95. };
  96. /**
  97. * 清空memory
  98. */
  99. const clearMemory = () => {
  100. setMemory({
  101. id: null,
  102. front: '',
  103. back: '',
  104. });
  105. };
  106. return (
  107. <PageHeaderWrapper title={false}>
  108. {state.step === Step.MAIN ? (
  109. <div>
  110. <div>待复习: {state.remindCount}</div>
  111. <p />
  112. <Button type="primary" size="large" onClick={changStepMemory}>
  113. 复习
  114. </Button>
  115. <p />
  116. <Button type="primary" size="large" onClick={changStepAdd}>
  117. 添加
  118. </Button>
  119. </div>
  120. ) : null}
  121. {state.step === Step.MEMORY ? (
  122. <div>
  123. 问题
  124. <hr />
  125. <div dangerouslySetInnerHTML={{ __html: memory.front }} />
  126. <hr />
  127. {state.showBack ? (
  128. <div>
  129. <div dangerouslySetInnerHTML={{ __html: memory.back }} />
  130. <hr />
  131. <Button
  132. type="primary"
  133. onClick={() => {
  134. memorySuccess(0.5);
  135. }}
  136. >
  137. 生疏
  138. </Button>
  139. <span> </span>
  140. <Button
  141. type="primary"
  142. onClick={() => {
  143. memorySuccess(2);
  144. }}
  145. >
  146. 一般
  147. </Button>
  148. <span> </span>
  149. <Button
  150. type="primary"
  151. onClick={() => {
  152. memorySuccess(3);
  153. }}
  154. >
  155. 简单
  156. </Button>
  157. <p />
  158. <div>
  159. <Button
  160. type="primary"
  161. onClick={async () => {
  162. await service.restart(memory);
  163. findNextLine();
  164. }}
  165. >
  166. 重新开始复习
  167. </Button>
  168. <span> </span>
  169. <Button
  170. type="primary"
  171. onClick={async () => {
  172. await service.noMemory(memory);
  173. findNextLine();
  174. }}
  175. >
  176. 不在复习
  177. </Button>
  178. <span> </span>
  179. <Button
  180. type="primary"
  181. onClick={async () => {
  182. await service.delete(memory);
  183. findNextLine();
  184. }}
  185. >
  186. 删除
  187. </Button>
  188. </div>
  189. <p />
  190. <div>
  191. <Button
  192. type="primary"
  193. onClick={() => {
  194. changStepAdd();
  195. }}
  196. >
  197. 编辑
  198. </Button>
  199. </div>
  200. </div>
  201. ) : (
  202. <div>
  203. <Button
  204. type="primary"
  205. onClick={() => {
  206. setState({ showBack: true });
  207. }}
  208. >
  209. 显示答案
  210. </Button>
  211. </div>
  212. )}
  213. </div>
  214. ) : null}
  215. {state.step === Step.ADD ? (
  216. <div>
  217. <h4>正面</h4>
  218. <ReactQuill
  219. theme="snow"
  220. value={memory.front}
  221. onChange={(value) => {
  222. setMemory({ front: value });
  223. }}
  224. />
  225. <p />
  226. <h4>反面</h4>
  227. <ReactQuill
  228. theme="snow"
  229. value={memory.back}
  230. onChange={(value) => {
  231. setMemory({ back: value });
  232. }}
  233. />
  234. <p />
  235. <Button
  236. type="primary"
  237. onClick={async () => {
  238. let res;
  239. if (memory != null && memory.id != null) {
  240. res = await service.update(memory);
  241. } else {
  242. res = await service.insert(memory);
  243. }
  244. if (res.success) {
  245. message.info('操作成功');
  246. clearMemory();
  247. } else {
  248. message.info(`操作失败: ${res.message}`);
  249. }
  250. }}
  251. >
  252. 确定
  253. </Button>
  254. <span> </span>
  255. <Button
  256. type="primary"
  257. onClick={() => {
  258. clearMemory();
  259. changStepMain();
  260. }}
  261. >
  262. 关闭
  263. </Button>
  264. </div>
  265. ) : null}
  266. </PageHeaderWrapper>
  267. );
  268. };
  269. export default TableList;