index.tsx 7.1 KB

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