index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { useEffect, useState } from 'react'
  2. import { View } from '@tarojs/components'
  3. import Taro, { useDidHide, useDidShow } from '@tarojs/taro'
  4. import { observer, inject } from 'mobx-react'
  5. //==========components组件引用========================
  6. import TopNavBar from '@src/components/TopNavBar/index'
  7. import ScrollViewHoc from '@src/components/ScrollView'
  8. import BookboxRowSmall from '@src/components/PupPetry/BookBox/BookboxRowSmall'
  9. import { Store } from '@src/app'
  10. import useApi from '@src/Hook/useApi'
  11. import SwipeAction from '@src/components/SwipeAction'
  12. interface Props {
  13. store: Store;
  14. }
  15. interface ContentData {
  16. current: number,
  17. total: number,
  18. size: number,
  19. records: any[]
  20. }
  21. const Index: React.FC<Props> = ({ store }) => {
  22. const { indexStore } = store
  23. const { bookrackList } = useApi(store.appInfoStore)
  24. const [readData, setReadData] = useState<ContentData>({
  25. current: 0,
  26. total: 0,
  27. size: 0,
  28. records: []
  29. })
  30. const [pageShow, setPageShow] = useState(false)
  31. const actions = (id) => {
  32. return [
  33. {
  34. text: '删除', backgroundColor: '#f44336', id, onClick: (id) => {
  35. console.log("删除", id)
  36. }
  37. },
  38. ]
  39. };
  40. useEffect(() => {
  41. if (pageShow) {
  42. getList({ pageNum: 1, pageSize: 20 })
  43. }
  44. return () => {
  45. log("componentWillUnmount");
  46. };
  47. }, [pageShow]); // Dependency array, runs effect only once on mount and unmount
  48. // 使用 useDidShow 代替 onShow
  49. useDidShow(() => {
  50. setPageShow(true)
  51. });
  52. // 使用 useDidHide 如果需要在页面隐藏时做一些处理
  53. useDidHide(() => {
  54. setPageShow(false)
  55. });
  56. const getList = (params) => {
  57. bookrackList(params).then((res: any) => {
  58. setReadData(res.data.data)
  59. return res
  60. })
  61. return Promise.resolve(true)
  62. }
  63. const load = () => {
  64. return new Promise((resolve, reject) => {
  65. let { size, total, records } = readData
  66. let pageSize = size + 20
  67. if (records?.length >= total) {
  68. Taro.showToast({
  69. title: '没有更多了~~',
  70. duration: 2000,
  71. icon: 'none'
  72. })
  73. reject()
  74. return
  75. } else {
  76. getList({ pageNum: 1, pageSize }).then(res => {
  77. resolve(true)
  78. })
  79. }
  80. })
  81. }
  82. return <View>
  83. <ScrollViewHoc
  84. load={load}
  85. filterClassName={""}
  86. navHeight={indexStore.navHeight}
  87. >
  88. <View className={"index"}>
  89. {
  90. readData?.records?.map((item: any, index: number) => {
  91. return <View key={item.bookId} style={{ margin: '30rpx' }}>
  92. <SwipeAction actions={actions(item.bookId)}>
  93. <BookboxRowSmall {...item} />
  94. </SwipeAction>
  95. </View>
  96. })
  97. }
  98. </View>
  99. </ScrollViewHoc>
  100. </View>
  101. };
  102. export default TopNavBar((inject('store')(observer(Index))), {
  103. title: '阅读记录'
  104. });