import React from 'react'; import { Link } from 'react-router-dom'; import './index.less'; import Assets from '@src/components/Assets'; import Page from '@src/containers/Page'; import Footer from '../../../components/Footer'; import { Contact } from '../../../components/Other'; import Select from '../../../components/Select'; import Modal from '../../../components/Modal'; import { Button } from '../../../components/Button'; import { Textbook } from '../../../stores/textbook'; import { My } from '../../../stores/my'; import { Main } from '../../../stores/main'; import { User } from '../../../stores/user'; import { TextbookSubject, TextbookQuality, TextbookType } from '../../../../Constant'; import { getMap, formatDate } from '../../../../../src/services/Tools'; const TextbookSubjectMap = getMap(TextbookSubject, 'value', 'label'); const TextbookQualityMap = getMap(TextbookQuality, 'value', 'label'); const TextbookTypeMap = getMap(TextbookType, 'value', 'label'); export default class extends Page { constructor(props) { super(props); this.keyMap = {}; window.onkeydown = this.onKeydown.bind(this); window.onkeyup = this.onKeyup.bind(this); } initState() { const { info = {} } = this.props.user; return { open: false, showTip: !info.textbookTips, textbookSubject: TextbookSubject.map(row => { return { title: row.label, key: row.value, }; }), }; } init() { Main.getBase() .then(result => { this.setState({ base: result }); }); } initData() { Textbook.getInfo() .then(result => { if (!result.hasService) { linkTo('/textbook'); } this.setState({ data: result }); console.log(this.state); this.refreshItem(this.state.search.no || 1); }); } refreshItem(no) { const { subject } = this.params; const { data } = this.state; Textbook.noTopic(data.latest.id, subject, no) .then(result => { this.setState({ item: result }); const canNext = this.canNext(); const canPrev = this.canPrev(); this.setState({ canNext, canPrev }); }); } onKeydown(e) { let active = false; if (this.keyMap[e.keyCode]) return false; switch (e.keyCode) { case 32: active = true; break; case 37: active = true; break; case 39: active = true; break; default: break; } if (active) { this.keyMap[e.keyCode] = true; return false; } return true; } onKeyup(e) { let active = false; switch (e.keyCode) { case 32: active = true; this.onOpen(); break; case 37: active = true; this.onPrev(); break; case 39: active = true; this.onNext(); break; default: break; } if (active) { this.keyMap[e.keyCode] = false; return false; } return true; } onOpen() { this.setState({ open: !this.state.open }); } onNext() { if (!this.canNext()) return; const { item } = this.state; const no = item.no + 1; this.refreshItem(no); } canNext() { const { subject } = this.params; const { item = {}, data } = this.state; const no = item.no + 1; if (no > data.latest[`${subject}Number`]) return false; return true; } onPrev() { if (!this.canPrev()) return; const { item = {} } = this.state; const no = item.no - 1; this.refreshItem(no); } canPrev() { const { item = {} } = this.state; const no = item.no - 1; if (no <= 0) return false; return true; } closeTips() { this.setState({ showTip: false }); My.textbookTips() .then(() => { const { info } = this.props.user; info.textbookTips = 1; User.infoHandle(info); }); } changeSubject(subject) { linkTo(`/textbook/topic/list/${subject}`); } renderView() { const { subject } = this.params; const { showTip, base = {}, data = {}, textbookSubject, canNext = false, canPrev = false } = this.state; const { latest = {} } = data; return (