FoodJournal.tsx 4.8 KB

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