index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. result.stat = result.stat || {};
  55. if (!result.stat.status) {
  56. result.stat.status = [];
  57. }
  58. if (!result.stat.goal) {
  59. result.stat.goal = [];
  60. }
  61. if (!result.stat.examinationTime) {
  62. result.stat.examinationTime = [];
  63. }
  64. if (!result.stat.scoreTime) {
  65. result.stat.scoreTime = [];
  66. }
  67. const statusTotal = result.stat.status.reduce((p, n) => { return p + n.value; }, 0);
  68. const goalTotal = result.stat.goal.reduce((p, n) => { return p + n.value; }, 0);
  69. const examinationTimeTotal = result.stat.examinationTime.reduce((p, n) => { return p + n.value; }, 0);
  70. const scoreTimeTotal = result.stat.scoreTime.reduce((p, n) => { return p + n.value; }, 0);
  71. const stat = {
  72. status: result.stat.status.map((row, index) => {
  73. row.value = formatPercent(row.value, statusTotal);
  74. row.label = `${PrepareStatusMap[row.key]}; ${row.value}%`;
  75. row.color = this.statusColors[index];
  76. return row;
  77. }),
  78. goal: result.stat.goal.map((row, index) => {
  79. row.value = formatPercent(row.value, goalTotal);
  80. row.label = `${row.key}+; ${row.value}%`;
  81. row.color = this.goalColors[index];
  82. return row;
  83. }),
  84. examinationTime: result.stat.status.map((row, index) => {
  85. row.value = formatPercent(row.value, examinationTimeTotal);
  86. row.label = `${PrepareExaminationTimeMap[row.key]}; ${row.value}%`;
  87. row.color = this.examinationTimeColors[index];
  88. return row;
  89. }),
  90. scoreTime: result.stat.scoreTime.map((row, index) => {
  91. row.value = formatPercent(row.value, scoreTimeTotal);
  92. row.label = `${PrepareScoreTimeMap[row.key]}; ${row.value}%`;
  93. row.color = this.scoreTimeColors[index];
  94. return row;
  95. }),
  96. };
  97. result.prepareGoal = result.prepareGoal || 650;
  98. result.prepareScoreTime = result.prepareScoreTime ? moment(result.prepareScoreTime) : null;
  99. this.setState({ data: result, stat, first: !result.prepareStatus, step: !result.prepareStatus ? 0 : 4, info: result.info });
  100. });
  101. }
  102. }
  103. onChange(type, key) {
  104. const { data } = this.state;
  105. data[type] = key;
  106. this.setState({ data });
  107. }
  108. onPrev() {
  109. const { step } = this.state;
  110. this.setState({ step: step - 1 });
  111. }
  112. onNext() {
  113. const { step } = this.state;
  114. this.setState({ step: step + 1 });
  115. }
  116. submitPrepare() {
  117. const { data } = this.state;
  118. My.editPrepare(data).then(result => {
  119. this.setState({ result });
  120. this.onNext();
  121. });
  122. }
  123. onClose() {
  124. const { onClose } = this.props;
  125. if (onClose) onClose();
  126. this.setState({ step: 0 });
  127. }
  128. render() {
  129. const { step, info } = this.state;
  130. const { show } = this.props;
  131. return (
  132. <Modal
  133. className="examination-modal"
  134. show={show}
  135. width={460}
  136. {...this.stepProp[step]}
  137. onClose={() => this.onClose()}
  138. >
  139. {info && <div className="examination-modal-wrapper">{this[`renderStep${step}`]()}</div>}
  140. </Modal>
  141. );
  142. }
  143. renderStep0() {
  144. const { data } = this.state;
  145. const { prepareStatus } = data;
  146. return (
  147. <div className="step-0-layout">
  148. <SpecialRadioGroup
  149. list={PrepareStatus}
  150. value={prepareStatus}
  151. width={190}
  152. space={10}
  153. onChange={key => this.onChange('prepareStatus', key)}
  154. />
  155. <div className="action-layout">
  156. <div className="next" onClick={() => prepareStatus && this.onNext()}>
  157. 下一题
  158. <Icon type="right-circle" theme="filled" />
  159. </div>
  160. </div>
  161. </div>
  162. );
  163. }
  164. renderStep1() {
  165. const { data } = this.state;
  166. const { prepareExaminationTime } = data;
  167. return (
  168. <div className="step-1-layout">
  169. <SpecialRadioGroup
  170. list={PrepareExaminationTime}
  171. value={prepareExaminationTime}
  172. width={195}
  173. space={10}
  174. onChange={key => this.onChange('prepareExaminationTime', key)}
  175. />
  176. <div className="action-layout">
  177. <div className="prev" onClick={() => this.onPrev()}>
  178. <Icon type="left-circle" theme="filled" />
  179. 上一题
  180. </div>
  181. <div className="next" onClick={() => prepareExaminationTime && this.onNext()}>
  182. 下一题
  183. <Icon type="right-circle" theme="filled" />
  184. </div>
  185. </div>
  186. </div>
  187. );
  188. }
  189. renderStep2() {
  190. const { data } = this.state;
  191. const { prepareGoal } = data;
  192. return (
  193. <div className="step-2-layout">
  194. <TotalSort
  195. value={prepareGoal}
  196. onChange={goal => {
  197. this.onChange('prepareGoal', goal);
  198. }}
  199. />
  200. <div className="action-layout">
  201. <div className="prev" onClick={() => this.onPrev()}>
  202. <Icon type="left-circle" theme="filled" />
  203. 上一题
  204. </div>
  205. <div className="next" onClick={() => prepareGoal && this.onNext()}>
  206. 下一题
  207. <Icon type="right-circle" theme="filled" />
  208. </div>
  209. </div>
  210. </div>
  211. );
  212. }
  213. renderStep3() {
  214. const { data, info, step } = this.state;
  215. const { prepareScoreTime } = data;
  216. return (
  217. <div className="step-3-layout">
  218. {info && <a href={info.link} className="a-l" target='_blank'>{info.title}</a>}
  219. <Date
  220. show={step === 3}
  221. theme="filled"
  222. value={prepareScoreTime}
  223. onChange={date => {
  224. this.onChange('prepareScoreTime', date);
  225. }}
  226. />
  227. <div className="action-layout">
  228. <div className="prev" onClick={() => this.onPrev()}>
  229. <Icon type="left-circle" theme="filled" />
  230. 上一题
  231. </div>
  232. <Button
  233. size="lager"
  234. radius
  235. onClick={() => {
  236. this.submitPrepare();
  237. }}
  238. >
  239. 提交
  240. </Button>
  241. </div>
  242. </div>
  243. );
  244. }
  245. renderStep4() {
  246. const { first, data, stat = {} } = this.state;
  247. return (
  248. <div className="step-4-layout">
  249. {first && (
  250. <div className="tip">
  251. <Icon type="check" />
  252. 7天VIP权限已赠送至您的账户。
  253. </div>
  254. )}
  255. <Ratio text="身份" subtext={PrepareStatusMap[data.prepareStatus]} values={stat.status || []} />
  256. <Ratio
  257. text="考试时间"
  258. subtext={PrepareExaminationTimeMap[data.prepareExaminationTime]}
  259. values={stat.examinationTime || []}
  260. />
  261. <Ratio text="目标成绩" subtext={`${data.prepareGoal}分`} values={stat.goal || []} />
  262. <Ratio
  263. text="出分日期"
  264. subtext={`${formatDate(data.prepareScoreTime, 'YYYY-MM')}`}
  265. values={stat.scoreTime || []}
  266. />
  267. </div>
  268. );
  269. }
  270. }