FoodJournal.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, getFoodScales, 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 { setFoodScales, 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. getConfigs()
  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. //0点刷新一下数据
  45. useEffect(() => {
  46. const now = new Date();
  47. const nextMidnight = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 0, 0, 0);
  48. const timeUntilMidnight = nextMidnight.getTime() - now.getTime();
  49. setTimeout(() => {
  50. refreshData()
  51. }, timeUntilMidnight);
  52. }, [])
  53. usePullDownRefresh(() => {
  54. setPageIndex(1)
  55. getList(1)
  56. })
  57. useReachBottom(() => {
  58. console.log('bottom')
  59. more()
  60. // setPageIndex(pageIndex+1)
  61. // getHistory()
  62. })
  63. //0点刷新一下数据
  64. function refreshData() {
  65. list.map(item => {
  66. item.showDate = false
  67. })
  68. var selDate = ''
  69. for (var i = 0; i < list.length; i++) {
  70. var obj = list[i]
  71. if (obj.start.date != selDate) {
  72. obj.showDate = true
  73. selDate = obj.start.date
  74. }
  75. }
  76. setList(list)
  77. setCount(count + 1)
  78. }
  79. function getConfigs() {
  80. getFoodTags({
  81. type: 'meal_tag'
  82. }).then(res => {
  83. dispatch(setMealTags((res as any).data))
  84. }).catch(e => {
  85. })
  86. getFoodScales({
  87. type: 'hunger_fullness'
  88. }).then(res => {
  89. dispatch(setFoodScales((res as any).data))
  90. }).catch(e => { })
  91. }
  92. async function getStorage(key: string) {
  93. try {
  94. const res = await Taro.getStorage({ key });
  95. return res.data;
  96. } catch {
  97. return '';
  98. }
  99. }
  100. function more() {
  101. if (total <= list.length || isLoading) {
  102. return;
  103. }
  104. setIsLoading(true)
  105. var page = pageIndex + 1
  106. getList(page)
  107. }
  108. function getList(index) {
  109. setPageIndex(index)
  110. getFoodJournals({
  111. page: index,
  112. limit: 10
  113. }).then(res => {
  114. Taro.stopPullDownRefresh()
  115. setShowError(false)
  116. setLoaded(true)
  117. setTotal((res as any).total)
  118. var array;
  119. if (index == 1) {
  120. array = (res as any).data
  121. // setList((res as any).data)
  122. } else {
  123. array = list.concat((res as any).data)
  124. // setList(list.concat((res as any).data))
  125. }
  126. var selDate = ''
  127. for (var i = 0; i < array.length; i++) {
  128. var obj = array[i]
  129. if (obj.start.date != selDate) {
  130. obj.showDate = true
  131. selDate = obj.start.date
  132. }
  133. }
  134. setList(array)
  135. setIsLoading(false)
  136. }).catch(e => {
  137. Taro.stopPullDownRefresh()
  138. if (pageIndex == 1) {
  139. setShowError(true)
  140. }
  141. })
  142. }
  143. function addItem(item) {
  144. setCount(count + 1)
  145. var temps: any = list
  146. if (!temps) {
  147. temps = []
  148. }
  149. temps.unshift(item)
  150. temps.map(item => {
  151. item.showDate = false
  152. })
  153. var selDate = ''
  154. for (var i = 0; i < temps.length; i++) {
  155. var obj = temps[i]
  156. if (obj.start.date != selDate) {
  157. obj.showDate = true
  158. selDate = obj.start.date
  159. }
  160. }
  161. setList(temps)
  162. }
  163. function headerView() {
  164. return <TitleView title={t('page.food.title')} showAddBtn={false}>
  165. </TitleView>
  166. }
  167. function detail() {
  168. var tempFirst = list.length > 0 ? list[0] : null
  169. if (!user.isLogin) {
  170. tempFirst = null
  171. }
  172. return <View style={{ position: 'relative', marginTop: rpxToPx(52) }}>
  173. {
  174. (loaded || !user.isLogin) && <FoodConsole addItem={addItem} firstItem={tempFirst} />
  175. }
  176. {
  177. user.isLogin && list && <FoodTimeline array={list} refresh={() => refreshData()} forceRefresh={() => getList(1)} />
  178. }
  179. {
  180. user.isLogin && showError && <NoData refresh={() => { getList(1) }} />
  181. }
  182. <View style={{ height: 60 }} />
  183. {/* {
  184. user.isLogin && <View className="center_line" style={{ height: list.length == 0 ? rpxToPx(304) : rpxToPx(364 * (list.length + 1) - 120) }} />
  185. } */}
  186. </View>
  187. }
  188. return <View>
  189. <Layout children={detail()}
  190. title={t('page.food.title')}
  191. type={TemplateType.customHeader}
  192. header={headerView()}
  193. refresh={() => { }}
  194. triggered={false}
  195. titleShowStyle={NaviBarTitleShowType.scrollToShow}
  196. />
  197. </View>
  198. }