page.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import Assets from '@src/components/Assets';
  5. import { getMap, formatDate } from '@src/services/Tools';
  6. import { QuestionType } from '../../../../Constant';
  7. import { My } from '../../../stores/my';
  8. const QuestionTypeMap = getMap(QuestionType, 'value', 'label');
  9. export default class extends Page {
  10. initState() {
  11. this.today = formatDate(new Date(), 'YYYY-MM-DD');
  12. return {
  13. list: [],
  14. };
  15. }
  16. initData() {
  17. My.listSearchHistory()
  18. .then(result => {
  19. const map = {};
  20. const dateList = [];
  21. result.forEach((row) => {
  22. const date = formatDate(row.createTime, 'YYYY-MM-DD');
  23. if (!map[date]) {
  24. map[date] = { date, list: [] };
  25. dateList.push(date);
  26. }
  27. const item = map[date];
  28. item.list.push(row);
  29. });
  30. const list = dateList.map(row => {
  31. return map[row];
  32. });
  33. this.setState({ list });
  34. });
  35. }
  36. clearHistory(date) {
  37. My.clearSearchHistory(date)
  38. .then(() => {
  39. this.refresh();
  40. });
  41. }
  42. renderView() {
  43. const { list = [] } = this.state;
  44. return (
  45. <div>
  46. <div className="top content t-8">
  47. 题库 > <span className="t-1">浏览记录</span>
  48. </div>
  49. <div className="center content">
  50. {list.map(item => {
  51. if (item.date === this.today) item.today = true;
  52. return <DayList data={item} onClear={() => this.clearHistory(item.date)} />;
  53. })}
  54. </div>
  55. </div>
  56. );
  57. }
  58. }
  59. class DayList extends Component {
  60. render() {
  61. const { data = {}, onClear } = this.props;
  62. return (
  63. <div className="day-list">
  64. <div className="m-b-1">
  65. <span className="t-1 t-s-18 m-r-1 f-w-b">{data.today ? '今天' : data.date}</span>
  66. <span className="c-p t-2 t-s-12" onClick={() => onClear()}>
  67. <Assets name="ico_24_restart" svg /> 清空记录
  68. </span>
  69. </div>
  70. {data.list.map(item => {
  71. return <LogItem data={item} onClick={() => { }} />;
  72. })}
  73. </div>
  74. );
  75. }
  76. }
  77. class LogItem extends Component {
  78. render() {
  79. const { data, onClick } = this.props;
  80. return (
  81. <div className="log-item">
  82. <span className="t-6 m-r-1 tag">{QuestionTypeMap[data.question.questionType]}</span>
  83. <a className="t-1 m-r-2 f-w-b" href={`/question/detail/${data.questionNo.id}`} target="_blank" onClick={() => onClick()}>{data.questionNo.title}</a>
  84. <span className="p-l-1 t-2">{data.question.description}</span>
  85. </div>
  86. );
  87. }
  88. }