index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import { Icon } from 'antd';
  4. import { getMap, formatDate, formatPercent } from '@src/services/Tools';
  5. import moment from 'moment';
  6. import { SpecialRadioGroup } from '../Radio';
  7. import Modal from '../Modal';
  8. import Button from '../Button';
  9. import TotalSort from '../TotalSort';
  10. import Date from '../Date';
  11. import Ratio from '../Ratio';
  12. import { My } from '../../stores/my';
  13. import { PrepareStatus, PrepareExaminationTime, PrepareScoreTime } from '../../../Constant';
  14. const PrepareStatusMap = getMap(PrepareStatus, 'value', 'short');
  15. const PrepareExaminationTimeMap = getMap(PrepareExaminationTime, 'value', 'label');
  16. const PrepareScoreTimeMap = getMap(PrepareScoreTime, 'value', 'label');
  17. export default class extends Component {
  18. constructor(props) {
  19. super(props);
  20. this.statusColors = ['#41A6F3', '#3F86EA', '#41A6F3', '#6DC0FF', '#9BD4FF'];
  21. this.examinationTimeColors = ['#6865FD', '#322EF2', '#8F65FF', '#8C8AFF'];
  22. this.goalColors = ['#2754E0', '#3F86EA', '#41A6F3', '#6DC0FF'];
  23. this.scoreTimeColors = ['#6865FD', '#322EF2', '#8F65FF', '#8C8AFF', '#8F65FF', '#C3C1D1'];
  24. this.stepProp = {
  25. 0: {
  26. title: '我的身份',
  27. },
  28. 1: {
  29. title: '下次考试时间',
  30. },
  31. 2: {
  32. title: '目标成绩',
  33. },
  34. 3: {
  35. title: '最晚出分时间(选填)',
  36. },
  37. 4: {
  38. title: '备考信息',
  39. onConfirm: props.onConfirm,
  40. onCancel: () => {
  41. this.setState({ step: 0 });
  42. },
  43. confirmText: '关闭',
  44. cancelText: '修改',
  45. },
  46. };
  47. this.state = { step: 0, data: { prepareGoal: 650 } };
  48. }
  49. componentWillReceiveProps(nextProps) {
  50. if (nextProps.show && !this.init) {
  51. this.init = true;
  52. My.getPrepare()
  53. .then(result => {
  54. const statusTotal = result.stat.status.reduce((p, n) => { return p + n.value; }, 0);
  55. const goalTotal = result.stat.goal.reduce((p, n) => { return p + n.value; }, 0);
  56. const examinationTimeTotal = result.stat.examinationTime.reduce((p, n) => { return p + n.value; }, 0);
  57. const scoreTimeTotal = result.stat.scoreTime.reduce((p, n) => { return p + n.value; }, 0);
  58. const stat = {
  59. status: result.stat.status.map((row, index) => {
  60. row.value = formatPercent(row.value, statusTotal);
  61. row.label = `${PrepareStatusMap[row.key]}; ${row.value}%`;
  62. row.color = this.statusColors[index];
  63. return row;
  64. }),
  65. goal: result.stat.goal.map((row, index) => {
  66. row.value = formatPercent(row.value, goalTotal);
  67. row.label = `${row.key}+; ${row.value}%`;
  68. row.color = this.goalColors[index];
  69. return row;
  70. }),
  71. examinationTime: result.stat.status.map((row, index) => {
  72. row.value = formatPercent(row.value, examinationTimeTotal);
  73. row.label = `${PrepareExaminationTimeMap[row.key]}; ${row.value}%`;
  74. row.color = this.examinationTimeColors[index];
  75. return row;
  76. }),
  77. scoreTime: result.stat.scoreTime.map((row, index) => {
  78. row.value = formatPercent(row.value, scoreTimeTotal);
  79. row.label = `${PrepareScoreTimeMap[row.key]}; ${row.value}%`;
  80. row.color = this.scoreTimeColors[index];
  81. return row;
  82. }),
  83. };
  84. result.prepareGoal = result.prepareGoal || 650;
  85. result.prepareScoreTime = result.prepareScoreTime ? moment(result.prepareScoreTime) : null;
  86. this.setState({ data: result, stat, first: !result.prepareStatus, step: !result.prepareStatus ? 0 : 4, info: result.info });
  87. });
  88. }
  89. }
  90. onChange(type, key) {
  91. const { data } = this.state;
  92. data[type] = key;
  93. this.setState({ data });
  94. }
  95. onPrev() {
  96. const { step } = this.state;
  97. this.setState({ step: step - 1 });
  98. }
  99. onNext() {
  100. const { step } = this.state;
  101. this.setState({ step: step + 1 });
  102. }
  103. submitPrepare() {
  104. const { data } = this.state;
  105. My.editPrepare(data).then(result => {
  106. this.setState({ result });
  107. this.onNext();
  108. });
  109. }
  110. onClose() {
  111. const { onClose } = this.props;
  112. if (onClose) onClose();
  113. this.setState({ step: 0 });
  114. }
  115. render() {
  116. const { step, info } = this.state;
  117. const { show } = this.props;
  118. return (
  119. <Modal
  120. className="examination-modal"
  121. show={show}
  122. width={460}
  123. {...this.stepProp[step]}
  124. onClose={() => this.onClose()}
  125. >
  126. {info && <div className="examination-modal-wrapper">{this[`renderStep${step}`]()}</div>}
  127. </Modal>
  128. );
  129. }
  130. renderStep0() {
  131. const { data } = this.state;
  132. const { prepareStatus } = data;
  133. return (
  134. <div className="step-0-layout">
  135. <SpecialRadioGroup
  136. list={PrepareStatus}
  137. value={prepareStatus}
  138. width={190}
  139. space={10}
  140. onChange={key => this.onChange('prepareStatus', key)}
  141. />
  142. <div className="action-layout">
  143. <div className="next" onClick={() => prepareStatus && this.onNext()}>
  144. 下一题
  145. <Icon type="right-circle" theme="filled" />
  146. </div>
  147. </div>
  148. </div>
  149. );
  150. }
  151. renderStep1() {
  152. const { data } = this.state;
  153. const { prepareExaminationTime } = data;
  154. return (
  155. <div className="step-1-layout">
  156. <SpecialRadioGroup
  157. list={PrepareExaminationTime}
  158. value={prepareExaminationTime}
  159. width={195}
  160. space={10}
  161. onChange={key => this.onChange('prepareExaminationTime', key)}
  162. />
  163. <div className="action-layout">
  164. <div className="prev" onClick={() => this.onPrev()}>
  165. <Icon type="left-circle" theme="filled" />
  166. 上一题
  167. </div>
  168. <div className="next" onClick={() => prepareExaminationTime && this.onNext()}>
  169. 下一题
  170. <Icon type="right-circle" theme="filled" />
  171. </div>
  172. </div>
  173. </div>
  174. );
  175. }
  176. renderStep2() {
  177. const { data } = this.state;
  178. const { prepareGoal } = data;
  179. return (
  180. <div className="step-2-layout">
  181. <TotalSort
  182. value={prepareGoal}
  183. onChange={goal => {
  184. this.onChange('prepareGoal', goal);
  185. }}
  186. />
  187. <div className="action-layout">
  188. <div className="prev" onClick={() => this.onPrev()}>
  189. <Icon type="left-circle" theme="filled" />
  190. 上一题
  191. </div>
  192. <div className="next" onClick={() => prepareGoal && this.onNext()}>
  193. 下一题
  194. <Icon type="right-circle" theme="filled" />
  195. </div>
  196. </div>
  197. </div>
  198. );
  199. }
  200. renderStep3() {
  201. const { data, info, step } = this.state;
  202. const { prepareScoreTime } = data;
  203. return (
  204. <div className="step-3-layout">
  205. {info && <a href={info.link} className="a-l" target='_blank'>{info.title}</a>}
  206. <Date
  207. show={step === 3}
  208. theme="filled"
  209. value={prepareScoreTime}
  210. onChange={date => {
  211. this.onChange('prepareScoreTime', date);
  212. }}
  213. />
  214. <div className="action-layout">
  215. <div className="prev" onClick={() => this.onPrev()}>
  216. <Icon type="left-circle" theme="filled" />
  217. 上一题
  218. </div>
  219. <Button
  220. size="lager"
  221. radius
  222. onClick={() => {
  223. this.submitPrepare();
  224. }}
  225. >
  226. 提交
  227. </Button>
  228. </div>
  229. </div>
  230. );
  231. }
  232. renderStep4() {
  233. const { first, data, stat = {} } = this.state;
  234. return (
  235. <div className="step-4-layout">
  236. {first && (
  237. <div className="tip">
  238. <Icon type="check" />
  239. 7天VIP权限已赠送至您的账户。
  240. </div>
  241. )}
  242. <Ratio text="身份" subtext={PrepareStatusMap[data.prepareStatus]} values={stat.status || []} />
  243. <Ratio
  244. text="考试时间"
  245. subtext={PrepareExaminationTimeMap[data.prepareExaminationTime]}
  246. values={stat.examinationTime || []}
  247. />
  248. <Ratio text="目标成绩" subtext={`${data.prepareGoal}分`} values={stat.goal || []} />
  249. <Ratio
  250. text="出分日期"
  251. subtext={`${formatDate(data.prepareScoreTime, 'YYYY-MM')}`}
  252. values={stat.scoreTime || []}
  253. />
  254. </div>
  255. );
  256. }
  257. }