VoteListTeacher.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**
  2. * Created by RaoMeng on 2019/1/13
  3. * Desc: 投票助手教师端
  4. */
  5. import React, {Component} from 'react'
  6. import ReactDOM from 'react-dom'
  7. import Swiper from 'swiper/dist/js/swiper'
  8. import 'swiper/dist/css/swiper.min.css'
  9. import {List, Icon, Skeleton} from 'antd'
  10. import {Modal, Toast} from 'antd-mobile'
  11. import RefreshLayout from '../../components/RefreshLayout'
  12. import {_baseURL, API} from "../../configs/api.config";
  13. import {connect} from 'react-redux'
  14. import {saveListState} from 'action/listState'
  15. import {getArrayValue, getIntValue, getStrValue, isObjEmpty} from "../../utils/common"
  16. import VoteItem from 'components/VoteItem'
  17. import NotifyBoardItem from "../../components/NotifyBoardItem";
  18. import {fetchGet, fetchPost} from "../../utils/fetchRequest";
  19. import NotifyBoBean from "../../model/NotifyBoBean";
  20. const mPageSize = 10
  21. let mReleaseIndex = 0
  22. let mReceiveIndex = 0
  23. class VoteListTeacher extends Component {
  24. constructor() {
  25. super()
  26. this.state = {
  27. selectIndex: 0,
  28. releaseList: [],
  29. receiveList: [],
  30. isReleaseLoading: true,
  31. isReceiveLoading: true,
  32. detailVisible: false,
  33. isReleaseRefreshing: false,
  34. isReceiveRefreshing: false,
  35. height: document.documentElement.clientHeight,
  36. previewVisible: false
  37. }
  38. }
  39. componentDidMount() {
  40. const hei = this.state.height - ReactDOM.findDOMNode(this.contain).offsetTop;
  41. this.setState({
  42. height: hei
  43. })
  44. document.title = '投票助手'
  45. const that = this
  46. console.log(this.props.listState)
  47. this.mySwiper = new Swiper('.swiper-container', {
  48. autoplay: false,
  49. loop: false,
  50. noSwiping: false,
  51. initialSlide: that.state.selectIndex,
  52. on: {
  53. slideChangeTransitionEnd: function () {
  54. that.setState({
  55. selectIndex: this.activeIndex
  56. })
  57. }
  58. }
  59. })
  60. if (this.props.listState.tabIndex >= 0) {
  61. this.setState({
  62. selectIndex: this.props.listState.tabIndex
  63. }, () => {
  64. this.mySwiper.slideTo(this.state.selectIndex, 0, false)
  65. })
  66. }
  67. if (this.props.listState && !isObjEmpty(this.props.listState.listData)) {
  68. this.setState({
  69. releaseList: this.props.listState.listData,
  70. isReleaseLoading: false,
  71. }, () => {
  72. ReactDOM.findDOMNode(this.releaseTab).scrollTop =
  73. this.props.listState.scrollTop
  74. })
  75. mReleaseIndex = this.props.listState.pageIndex
  76. } else {
  77. mReleaseIndex = 0
  78. this.loadReleaseList()
  79. }
  80. if (this.props.listState && !isObjEmpty(this.props.listState.listData2)) {
  81. this.setState({
  82. receiveList: this.props.listState.listData2,
  83. isReceiveLoading: false,
  84. }, () => {
  85. ReactDOM.findDOMNode(this.receiveTab).scrollTop =
  86. this.props.listState.scrollTop2
  87. })
  88. mReceiveIndex = this.props.listState.pageIndex2
  89. } else {
  90. mReceiveIndex = 0
  91. this.loadReceiveList()
  92. }
  93. }
  94. componentWillUnmount() {
  95. Toast.hide()
  96. }
  97. render() {
  98. const {selectIndex} = this.state
  99. const releaseItems = this.getReleaseItems()
  100. const receiveItems = this.getReceiveItems()
  101. return (
  102. <div className='notify-select-root'>
  103. <div className='gray-line'></div>
  104. <div className='identity-select'>
  105. <div className={selectIndex == 0 ?
  106. 'identity-item-select' : 'identity-item-normal'}
  107. onClick={this.onReleaseClick}>我发布的
  108. </div>
  109. <div className={selectIndex == 1 ?
  110. 'identity-item-select' : 'identity-item-normal'}
  111. onClick={this.onReceiveClick}>我接收的
  112. </div>
  113. </div>
  114. <div className="swiper-container"
  115. ref={el => {
  116. this.contain = el
  117. }}>
  118. <div className="swiper-wrapper">
  119. <div className="swiper-slide">
  120. {releaseItems}
  121. </div>
  122. <div className="swiper-slide">
  123. {receiveItems}
  124. </div>
  125. </div>
  126. </div>
  127. <Icon type="plus-circle" theme='filled' className='common-add-icon'
  128. onClick={this.onAddVote}/>
  129. </div>
  130. )
  131. }
  132. loadReleaseList = () => {
  133. mReleaseIndex++
  134. console.log(mReleaseIndex)
  135. try {
  136. this.setState({
  137. isReleaseRefreshing: true
  138. })
  139. } catch (e) {
  140. }
  141. const {releaseList} = this.state
  142. if (mReleaseIndex === 1) {
  143. releaseList.length = 0
  144. }
  145. fetchGet(API.voteListTeacher, {
  146. userId: this.props.userInfo.userId,
  147. voteType: '1',
  148. pageIndex: mReleaseIndex,
  149. pageSize: mPageSize
  150. }).then(response => {
  151. if (response && response.data && response.data.create.length > 0) {
  152. response.data.create.forEach((item, index) => {
  153. let voteBean = {}
  154. voteBean.voteId = getIntValue(item, 'voteId')
  155. voteBean.voteName = getStrValue(item, 'voteName')
  156. voteBean.voteType = getIntValue(item, 'voteType')
  157. voteBean.voteStatusCode = getIntValue(item, 'voteStatus')
  158. if (voteBean.voteStatusCode === 1) {
  159. voteBean.voteStatus = '进行中'
  160. } else {
  161. voteBean.voteStatus = '已投票'
  162. }
  163. voteBean.creatDate = getStrValue(item, 'creatDate')
  164. voteBean.voteEndDate = getStrValue(item, 'voteEndDate')
  165. voteBean.voteRemarks = getStrValue(item, 'voteRemarks')
  166. const topics = getStrValue(item, 'topics')
  167. if (!isObjEmpty(topics)) {
  168. voteBean.options = topics[0].options
  169. }
  170. releaseList.push(voteBean)
  171. })
  172. } else {
  173. if (mReleaseIndex > 1) {
  174. mReleaseIndex--
  175. }
  176. }
  177. this.setState({
  178. releaseList,
  179. isReleaseLoading: false,
  180. isReleaseRefreshing: false,
  181. })
  182. }).catch(error => {
  183. if (mReleaseIndex > 1) {
  184. mReleaseIndex--
  185. }
  186. this.setState({
  187. isReleaseLoading: false,
  188. isReleaseRefreshing: false
  189. })
  190. if (typeof error === 'string') {
  191. Toast.fail(error, 2)
  192. }
  193. })
  194. }
  195. loadReceiveList = () => {
  196. mReceiveIndex++
  197. console.log(mReceiveIndex)
  198. try {
  199. this.setState({
  200. isReceiveRefreshing: true
  201. })
  202. } catch (e) {
  203. }
  204. const {receiveList} = this.state
  205. if (mReceiveIndex === 1) {
  206. receiveList.length = 0
  207. }
  208. fetchGet(API.voteListTeacher, {
  209. userId: this.props.userInfo.userId,
  210. voteType: '1',
  211. pageIndex: mReceiveIndex,
  212. pageSize: mPageSize
  213. }).then(response => {
  214. if (response && response.data && response.data.notify.length > 0) {
  215. response.data.notify.forEach((item, index) => {
  216. let voteBean = {}
  217. voteBean.voteId = getIntValue(item, 'voteId')
  218. voteBean.voteName = getStrValue(item, 'voteName')
  219. voteBean.voteType = getIntValue(item, 'voteType')
  220. voteBean.voteStatusCode = getIntValue(item, 'voteStatus')
  221. if (voteBean.voteStatusCode === 1) {
  222. voteBean.voteStatus = '进行中'
  223. } else {
  224. voteBean.voteStatus = '已投票'
  225. }
  226. voteBean.creatDate = getStrValue(item, 'creatDate')
  227. voteBean.voteEndDate = getStrValue(item, 'voteEndDate')
  228. voteBean.voteRemarks = getStrValue(item, 'voteRemarks')
  229. const topics = getStrValue(item, 'topics')
  230. if (!isObjEmpty(topics)) {
  231. voteBean.options = topics[0].options
  232. }
  233. receiveList.push(voteBean)
  234. })
  235. } else {
  236. if (mReceiveIndex > 1) {
  237. mReceiveIndex--
  238. }
  239. }
  240. this.setState({
  241. receiveList,
  242. isReceiveLoading: false,
  243. isReceiveRefreshing: false,
  244. })
  245. }).catch(error => {
  246. if (mReceiveIndex > 1) {
  247. mReceiveIndex--
  248. }
  249. this.setState({
  250. isReceiveLoading: false,
  251. isReceiveRefreshing: false
  252. })
  253. if (typeof error === 'string') {
  254. Toast.fail(error, 2)
  255. }
  256. })
  257. }
  258. onReleaseClick = () => {
  259. this.setState({
  260. selectIndex: 0
  261. }, () => {
  262. this.mySwiper.slideTo(this.state.selectIndex, 300, false)
  263. })
  264. }
  265. onReceiveClick = () => {
  266. this.setState({
  267. selectIndex: 1
  268. }, () => {
  269. this.mySwiper.slideTo(this.state.selectIndex, 300, false)
  270. })
  271. }
  272. getReleaseItems = () => (
  273. <div className='notify-bg-root'>
  274. <RefreshLayout
  275. ref={el => {
  276. this.releaseTab = el
  277. }}
  278. refreshing={this.state.isReleaseRefreshing}
  279. onRefresh={this.loadReleaseList}
  280. height={this.state.height}>
  281. <Skeleton loading={this.state.isReleaseLoading} active paragraph={{rows: 3}}>
  282. <List split={false} dataSource={this.state.releaseList}
  283. renderItem={(item, index) => (
  284. <VoteItem
  285. voteBean={item}
  286. onItemClick={this.onItemClick.bind(this)}
  287. index={index}
  288. type='release'/>
  289. )}/>
  290. </Skeleton>
  291. </RefreshLayout>
  292. </div>
  293. )
  294. getReceiveItems = () => (
  295. <div className='notify-bg-root'>
  296. <RefreshLayout
  297. ref={el => {
  298. this.receiveTab = el
  299. }}
  300. refreshing={this.state.isReceiveRefreshing}
  301. onRefresh={this.loadReceiveList}
  302. height={this.state.height}>
  303. <Skeleton loading={this.state.isReceiveLoading} active paragraph={{rows: 3}}>
  304. <List split={false} dataSource={this.state.receiveList}
  305. renderItem={(item, index) => (
  306. <VoteItem
  307. voteBean={item}
  308. onItemClick={this.onItemClick.bind(this)}
  309. index={index}
  310. type='receive'/>
  311. )}/>
  312. </Skeleton>
  313. </RefreshLayout>
  314. </div>
  315. )
  316. onItemClick = (index, voteId) => {
  317. this.saveListStatus(false, index)
  318. this.props.history.push('/voteDetail/' + voteId)
  319. }
  320. onAddVote = () => {
  321. console.log("onAddVote")
  322. this.saveListStatus(true, -1)
  323. this.props.history.push('/send-vote')
  324. }
  325. saveListStatus = (pageLimit, itemIndex) => {
  326. const {releaseList, receiveList} = this.state
  327. let releaseScroll = 0, receiveScroll = 0, releaseIndex = 0, receiveIndex = 0
  328. if (releaseList.length <= 10 && pageLimit) {
  329. releaseList.length = 0
  330. releaseIndex = 0
  331. releaseScroll = 0
  332. } else {
  333. releaseIndex = mReleaseIndex
  334. releaseScroll = ReactDOM.findDOMNode(this.releaseTab).scrollTop
  335. }
  336. if (receiveList.length <= 10 && pageLimit) {
  337. receiveList.length = 0
  338. receiveIndex = 0
  339. receiveScroll = 0
  340. } else {
  341. receiveIndex = mReceiveIndex
  342. receiveScroll = ReactDOM.findDOMNode(this.receiveTab).scrollTop
  343. }
  344. saveListState({
  345. scrollTop: releaseScroll,
  346. listData: releaseList,
  347. pageIndex: releaseIndex,
  348. tabIndex: this.state.selectIndex,
  349. scrollTop2: receiveScroll,
  350. listData2: receiveList,
  351. pageIndex2: receiveIndex,
  352. itemIndex: itemIndex
  353. })()
  354. }
  355. }
  356. let mapStateToProps = (state) => ({
  357. userInfo: {...state.redUserInfo},
  358. listState: {...state.redListState}
  359. })
  360. let mapDispatchToProps = (dispatch) => ({})
  361. export default connect(mapStateToProps, mapDispatchToProps)(VoteListTeacher)