index.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import {Button, message, Modal} from 'antd';
  2. import React, {useEffect, useState} 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 UpdateForm from '@/pages/Memory/components/UpdateForm';
  7. import {TableListItem} from '../MemoryList/data.d';
  8. import service from './service';
  9. import styles from './index.less';
  10. import {ExclamationCircleOutlined} from '@ant-design/icons/lib';
  11. import {getTextFromHtml} from "@/utils/utils";
  12. enum Step {
  13. MAIN = 0,
  14. MEMORY = 1,
  15. ADD = 2,
  16. }
  17. interface State {
  18. remindCount: number;
  19. step: Step;
  20. showBack?: boolean;
  21. }
  22. const TableList: React.FC<{}> = () => {
  23. const [btnDisable, setBtnDisable] = useState(false);
  24. let counting = false;
  25. const {confirm} = Modal;
  26. const [state, setState] = useSingleState<State>({
  27. remindCount: 0,
  28. step: Step.MAIN, // 0 主页,1 复习,2 添加
  29. showBack: false,
  30. });
  31. const [memory, setMemory] = useSingleState<TableListItem>({
  32. back: '',
  33. front: '',
  34. id: undefined,
  35. period: 0,
  36. remindTime: new Date(),
  37. tag: '',
  38. updateTime: '',
  39. userId: 0,
  40. });
  41. /**
  42. * 查询用户待提醒数量
  43. * @param fields
  44. */
  45. const countRemind = async () => {
  46. // 如果上一个查询还没有结束,则直接返回
  47. console.log(counting);
  48. if (counting) {
  49. return;
  50. }
  51. counting = true;
  52. const hide = message.loading('正在查询');
  53. try {
  54. const res = await service.countRemind({});
  55. hide();
  56. if (res.success) {
  57. setState({remindCount: parseInt(res.data, 10)});
  58. }
  59. counting = false;
  60. } catch (error) {
  61. hide();
  62. message.error('查询异常!');
  63. counting = false;
  64. }
  65. };
  66. /**
  67. * 清空memory
  68. */
  69. const clearMemory = () => {
  70. setMemory({
  71. id: undefined,
  72. front: '',
  73. back: '',
  74. });
  75. };
  76. /**
  77. * 处理返回值
  78. * @param res
  79. */
  80. const memoryRes = (res: any) => {
  81. // 只要有返回值,就使按钮可见
  82. setBtnDisable(false);
  83. if (res.success) {
  84. setMemory({...memory, ...res.data});
  85. } else {
  86. setState({step: Step.MAIN});
  87. }
  88. };
  89. const memorySuccess = async (factorInt: number) => {
  90. setBtnDisable(true);
  91. memoryRes(await service.memorySuccess({id: memory.id, factor: factorInt}));
  92. };
  93. const findNextLine = async () => {
  94. const res = await service.findNext({});
  95. if (res.success) {
  96. setMemory({...memory, ...res.data});
  97. } else {
  98. setState({step: Step.MAIN});
  99. }
  100. };
  101. // 绑定快捷键
  102. const bindShortKey = () => {
  103. document.onkeydown = (ev) => {
  104. // 空格显示背面和默认选择
  105. if (ev.key === ' ' && state.step === Step.MEMORY) {
  106. ev.preventDefault(); // 关闭浏览器快捷键
  107. if (state.showBack === false) {
  108. setState({showBack: true});
  109. } else {
  110. memorySuccess(2);
  111. }
  112. } else if (ev.key === ' ' && state.step === Step.MAIN) {
  113. // 空格刷新
  114. // 如果待复习大于0,则直接进去
  115. if (state.remindCount > 0) {
  116. setState({step: Step.MEMORY});
  117. } else {
  118. // 刷新一下,再判断是否应该去复习
  119. countRemind();
  120. if (state.remindCount > 0) {
  121. setState({step: Step.MEMORY});
  122. }
  123. }
  124. } else if (ev.key.toLowerCase() === 'e') {
  125. // 快捷 E,进入编辑、新建页面
  126. setState({step: Step.ADD});
  127. } else if (ev.key.toLowerCase() === 'escape') {
  128. // esc键回到主页
  129. setState({step: Step.MAIN});
  130. }
  131. };
  132. }
  133. useEffect(() => {
  134. countRemind();
  135. setTimeout(() => {
  136. countRemind();
  137. }, 1000 * 60 * 60); // 每隔一个小时查询一次
  138. bindShortKey()
  139. }, []);
  140. useEffect(() => {
  141. // 关闭背面,清空 memory
  142. setState({showBack: false});
  143. clearMemory();
  144. if (state.step === Step.MAIN) {
  145. // 进入统计页面
  146. countRemind();
  147. } else if (state.step === Step.MEMORY) {
  148. findNextLine();
  149. }
  150. }, [state.step])
  151. useEffect(() => {
  152. // 判断是否为主页面刷新
  153. if (memory.front.length === 0) {
  154. return;
  155. }
  156. // 进入复习页面后
  157. // 如果没有文字,并且也没有图片
  158. if (getTextFromHtml(memory.back).length === 0 && memory.back.indexOf("img") < 0) {
  159. setState({showBack: true});
  160. } else {
  161. setState({showBack: false});
  162. }
  163. }, [memory.front]);
  164. return (
  165. <PageHeaderWrapper title={false}>
  166. {state.step === Step.MAIN ? (
  167. <div>
  168. <div>待复习: {state.remindCount}</div>
  169. <p/>
  170. <Button type="primary" size="large" onClick={() => {
  171. if (state.remindCount <= 0) {
  172. message.info('已经复习完了哟!');
  173. return;
  174. }
  175. setState({step: Step.MEMORY})
  176. }}>
  177. 复习
  178. </Button>
  179. <p/>
  180. <Button type="primary" size="large" onClick={() => setState({step: Step.ADD})}>
  181. 添加
  182. </Button>
  183. </div>
  184. ) : null}
  185. {state.step === Step.MEMORY ? (
  186. <div>
  187. 问题:
  188. <p/>
  189. <Quill readonly onChange={() => {
  190. }} value={memory.front}/>
  191. <p/>
  192. {state.showBack ? (
  193. <div>
  194. <Quill readonly onChange={() => {
  195. }} value={memory.back}/>
  196. <p/>
  197. <div className={styles.divBottom}>
  198. <Button
  199. type="primary"
  200. disabled={btnDisable}
  201. onClick={() => {
  202. memorySuccess(0.5);
  203. }}
  204. >
  205. 生疏
  206. </Button>
  207. <span> </span>
  208. <Button
  209. type="primary"
  210. disabled={btnDisable}
  211. onClick={() => {
  212. memorySuccess(2);
  213. }}
  214. >
  215. 一般
  216. </Button>
  217. <span> </span>
  218. <Button
  219. type="primary"
  220. disabled={btnDisable}
  221. onClick={() => {
  222. memorySuccess(3);
  223. }}
  224. >
  225. 简单
  226. </Button>
  227. <span> </span>
  228. <Button
  229. type="primary"
  230. disabled={btnDisable}
  231. onClick={async () => {
  232. setBtnDisable(true);
  233. memoryRes(await service.delay(memory));
  234. }}
  235. >
  236. 搁置
  237. </Button>
  238. <p/>
  239. <div>
  240. <Button
  241. type="primary"
  242. disabled={btnDisable}
  243. onClick={async () => {
  244. setBtnDisable(true);
  245. memoryRes(await service.restart(memory));
  246. }}
  247. >
  248. 重新开始复习
  249. </Button>
  250. <span> </span>
  251. <Button
  252. type="primary"
  253. disabled={btnDisable}
  254. onClick={async () => {
  255. setBtnDisable(true);
  256. memoryRes(await service.noMemory(memory));
  257. }}
  258. >
  259. 不在复习
  260. </Button>
  261. <span> </span>
  262. <Button
  263. type="primary"
  264. disabled={btnDisable}
  265. onClick={async () => {
  266. confirm({
  267. title: '确定删除吗?',
  268. icon: <ExclamationCircleOutlined/>,
  269. content: '删除后无法恢复',
  270. okText: '确定',
  271. okType: 'danger',
  272. cancelText: '取消',
  273. async onOk() {
  274. setBtnDisable(true);
  275. memoryRes(await service.delete(memory));
  276. },
  277. onCancel() {
  278. console.log('Cancel');
  279. },
  280. });
  281. }}
  282. >
  283. 删除
  284. </Button>
  285. </div>
  286. <p/>
  287. <div>
  288. <Button
  289. type="primary"
  290. onClick={() => {
  291. setState({step: Step.ADD});
  292. }}
  293. >
  294. 编辑
  295. </Button>
  296. <span> </span>
  297. <Button
  298. type="primary"
  299. onClick={() => {
  300. setState({step: Step.MAIN});
  301. }}
  302. >
  303. 关闭
  304. </Button>
  305. </div>
  306. </div>
  307. </div>
  308. ) : (
  309. <div>
  310. <Button
  311. type="primary"
  312. onClick={() => {
  313. setState({showBack: true});
  314. }}
  315. >
  316. 显示答案
  317. </Button>
  318. </div>
  319. )}
  320. </div>
  321. ) : null}
  322. {state.step === Step.ADD ? (
  323. <UpdateForm
  324. onCancel={() => {
  325. bindShortKey();
  326. setState({step: Step.MAIN});
  327. }}
  328. modalVisible={state.step === Step.ADD}
  329. values={memory}
  330. />
  331. ) : null}
  332. </PageHeaderWrapper>
  333. );
  334. };
  335. export default TableList;