FoodJournal.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { useTranslation } from "react-i18next"
  2. import TitleView from "../trackTimeDuration/components/TitleView"
  3. import { View } from "@tarojs/components"
  4. import FoodConsole from "./FoodConsole"
  5. import FoodTimeline from "./FoodTimeline"
  6. import Layout from "@/components/layout/layout"
  7. import { NaviBarTitleShowType, TemplateType } from "@/utils/types"
  8. import { useEffect, useState } from "react"
  9. import { getFoodJournals } from "@/services/foodJournal"
  10. import { usePullDownRefresh, useReachBottom } from "@tarojs/taro"
  11. import Taro from "@tarojs/taro"
  12. import { useSelector } from "react-redux"
  13. export default function Component() {
  14. const { t } = useTranslation()
  15. const user = useSelector((state: any) => state.user);
  16. const [pageIndex, setPageIndex] = useState(1)
  17. const [list, setList] = useState<any[]>([])
  18. const [count, setCount] = useState(0)
  19. const [total, setTotal] = useState(0)
  20. const [isLoading, setIsLoading] = useState(false)
  21. const [loaded, setLoaded] = useState(false)
  22. useEffect(() => {
  23. if (user.isLogin) {
  24. getList(1)
  25. }
  26. }, [user.isLogin])
  27. usePullDownRefresh(() => {
  28. getList(1)
  29. })
  30. useReachBottom(() => {
  31. console.log('bottom')
  32. more()
  33. // setPageIndex(pageIndex+1)
  34. // getHistory()
  35. })
  36. function more() {
  37. if (total <= list.length || isLoading) {
  38. return;
  39. }
  40. setIsLoading(true)
  41. var page = pageIndex + 1
  42. setPageIndex(page)
  43. }
  44. function getList(index) {
  45. getFoodJournals({
  46. page: index,
  47. limit: 10
  48. }).then(res => {
  49. Taro.stopPullDownRefresh()
  50. setLoaded(true)
  51. setTotal((res as any).total)
  52. if (index == 1) {
  53. setList((res as any).data)
  54. } else {
  55. setList(list.concat((res as any).data))
  56. }
  57. setIsLoading(false)
  58. }).catch(e => {
  59. Taro.stopPullDownRefresh()
  60. })
  61. }
  62. function addItem(item) {
  63. setCount(count + 1)
  64. var temps: any = list
  65. if (!temps) {
  66. temps = []
  67. }
  68. temps.unshift(item)
  69. setList(temps)
  70. }
  71. function headerView() {
  72. return <TitleView title={t('page.food.title')} subTitle="hello world" showAddBtn={false}>
  73. </TitleView>
  74. }
  75. function detail() {
  76. return <View>
  77. {
  78. <FoodConsole addItem={addItem} />
  79. }
  80. {
  81. user.isLogin && list && <FoodTimeline array={list} />
  82. }
  83. <View style={{height:60}}/>
  84. </View>
  85. }
  86. return <View>
  87. <Layout children={detail()}
  88. title={t('page.food.title')}
  89. type={TemplateType.customHeader}
  90. header={headerView()}
  91. refresh={() => { }}
  92. triggered={false}
  93. titleShowStyle={NaviBarTitleShowType.scrollToShow}
  94. />
  95. </View>
  96. }