FoodJournal.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, getFoodTags } from "@/services/foodJournal"
  10. import { usePullDownRefresh, useReachBottom, useReady } from "@tarojs/taro"
  11. import Taro from "@tarojs/taro"
  12. import { useDispatch, useSelector } from "react-redux"
  13. import { setMealTags } from "@/store/common"
  14. import { getInfoSuccess } from "@/store/user"
  15. import './FoodJournal.scss'
  16. import { rpxToPx } from "@/utils/tools"
  17. export default function Component() {
  18. const { t } = useTranslation()
  19. const dispatch = useDispatch();
  20. const user = useSelector((state: any) => state.user);
  21. const [pageIndex, setPageIndex] = useState(1)
  22. const [list, setList] = useState<any[]>([])
  23. const [count, setCount] = useState(0)
  24. const [total, setTotal] = useState(0)
  25. const [isLoading, setIsLoading] = useState(false)
  26. const [loaded, setLoaded] = useState(false)
  27. useEffect(() => {
  28. getTags()
  29. if (user.isLogin) {
  30. getList(1)
  31. }
  32. }, [user.isLogin])
  33. useReady(async () => {
  34. const userData = await getStorage('userData');
  35. if (userData) {
  36. dispatch(getInfoSuccess(JSON.parse(userData as string)) as any);
  37. setTimeout(() => {
  38. getList(1)
  39. }, 200)
  40. }
  41. })
  42. usePullDownRefresh(() => {
  43. setPageIndex(1)
  44. getList(1)
  45. })
  46. useReachBottom(() => {
  47. console.log('bottom')
  48. more()
  49. // setPageIndex(pageIndex+1)
  50. // getHistory()
  51. })
  52. function getTags() {
  53. getFoodTags({
  54. type: 'meal_tag'
  55. }).then(res => {
  56. dispatch(setMealTags((res as any).data))
  57. }).catch(e => {
  58. })
  59. }
  60. async function getStorage(key: string) {
  61. try {
  62. const res = await Taro.getStorage({ key });
  63. return res.data;
  64. } catch {
  65. return '';
  66. }
  67. }
  68. function more() {
  69. if (total <= list.length || isLoading) {
  70. return;
  71. }
  72. setIsLoading(true)
  73. var page = pageIndex + 1
  74. setPageIndex(page)
  75. getList(page)
  76. }
  77. function getList(index) {
  78. getFoodJournals({
  79. page: index,
  80. limit: 10
  81. }).then(res => {
  82. Taro.stopPullDownRefresh()
  83. setLoaded(true)
  84. setTotal((res as any).total)
  85. if (index == 1) {
  86. setList((res as any).data)
  87. } else {
  88. setList(list.concat((res as any).data))
  89. }
  90. setIsLoading(false)
  91. }).catch(e => {
  92. Taro.stopPullDownRefresh()
  93. })
  94. }
  95. function addItem(item) {
  96. setCount(count + 1)
  97. var temps: any = list
  98. if (!temps) {
  99. temps = []
  100. }
  101. temps.unshift(item)
  102. setList(temps)
  103. }
  104. function headerView() {
  105. return <TitleView title={t('page.food.title')} showAddBtn={false}>
  106. </TitleView>
  107. }
  108. function detail() {
  109. return <View style={{ position: 'relative', marginTop: rpxToPx(52) }}>
  110. {
  111. <FoodConsole addItem={addItem} />
  112. }
  113. {
  114. user.isLogin && list && <FoodTimeline array={list} />
  115. }
  116. <View style={{ height: 60 }} />
  117. {
  118. user.isLogin && <View className="center_line" style={{ height: list.length == 0 ? rpxToPx(304) : rpxToPx(364 * (list.length + 1) - 120) }} />
  119. }
  120. </View>
  121. }
  122. return <View>
  123. <Layout children={detail()}
  124. title={t('page.food.title')}
  125. type={TemplateType.customHeader}
  126. header={headerView()}
  127. refresh={() => { }}
  128. triggered={false}
  129. titleShowStyle={NaviBarTitleShowType.scrollToShow}
  130. />
  131. </View>
  132. }