MainHistory.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { View, Text, Image } from "@tarojs/components";
  2. import { useEffect, useRef, useState } from "react";
  3. import { records } from "@/services/health";
  4. import './History.scss'
  5. import Calendar from "./calendar";
  6. import { useSelector } from "react-redux";
  7. import HistoryItem from "./HistoryItem";
  8. import { rpxToPx } from "@/utils/tools";
  9. import { jumpPage } from "../trackTimeDuration/hooks/Common";
  10. import Taro from "@tarojs/taro";
  11. import { getThemeColor } from "./hooks/health_hooks";
  12. import { TimeFormatter } from "@/utils/time_format";
  13. let lastMode = ''
  14. export default function MainHistory(props: { type?: string, fast_type?: string }) {
  15. const [list, setList] = useState<any>([])
  16. const [page, setPage] = useState(1)
  17. const [total, setTotal] = useState(0)
  18. const [loaded, setLoaded] = useState(false)
  19. const refDemo = useRef()
  20. const health = useSelector((state: any) => state.health);
  21. const healthRef = useRef(health)
  22. global.refreshHistory = () => {
  23. refresh()
  24. }
  25. useEffect(() => {
  26. if (props.type) {
  27. loadData(1)
  28. }
  29. }, [props.type])
  30. useEffect(() => {
  31. if (lastMode != health.mode) {
  32. lastMode = health.mode
  33. loadData(1)
  34. }
  35. }, [health.mode])
  36. function refresh() {
  37. loadData(1)
  38. setPage(1)
  39. }
  40. function more() {
  41. var index = page;
  42. index++;
  43. setPage(index)
  44. loadData(index)
  45. }
  46. function loadData(index: number) {
  47. var params: any = {
  48. window: props.type ? props.type : health.mode,
  49. limit: 10,
  50. page: index
  51. }
  52. if (props.fast_type) {
  53. params.fast_type = props.fast_type
  54. }
  55. records(params).then(res => {
  56. setLoaded(true)
  57. if (index == 1) {
  58. setList((res as any).data)
  59. setTotal((res as any).total)
  60. }
  61. else {
  62. setList([...list, ...(res as any).data])
  63. }
  64. if ((res as any).data.length > 0) {
  65. setTimeout(() => {
  66. // var array:any = [];
  67. // (res as any).data.map((item,index)=>{
  68. // array.push('#history_id_'+index)
  69. // })
  70. // var ids = array.join(',')
  71. // console.log(array)
  72. // console.log(ids)
  73. const query = Taro.createSelectorQuery();
  74. query.selectAll('#history_id_0').boundingClientRect((rect) => {
  75. console.log(rect)
  76. }).exec();
  77. }, 1000)
  78. }
  79. })
  80. }
  81. if (!loaded)
  82. return <View />
  83. return <View style={{ width: rpxToPx(750), marginTop: rpxToPx(35) }}>
  84. {/* <Calendar year={2024} month={8}/> */}
  85. {/* <View style={{
  86. // position: 'sticky',
  87. top: 0,
  88. height: 50,
  89. backgroundColor: 'blue',
  90. zIndex: 100
  91. }} /> */}
  92. {
  93. (list.length > 0 || health.mode == 'EAT') && <View className="recent">
  94. <Text className="h34 bold">Recent</Text>
  95. {
  96. health.mode == 'EAT' && <View onClick={() => {
  97. jumpPage('/_health/pages/archive')
  98. }}>
  99. <Image className="archive" src={require('@assets/_health/archive.png')} />
  100. </View>
  101. }
  102. <View className="border_footer_line" />
  103. </View>
  104. }
  105. {
  106. health.mode == 'EAT' && health.eatArchived && health.eatArchived.archived && <View style={{
  107. display:'flex',
  108. flexDirection:'column',
  109. alignItems:'center',
  110. paddingTop:rpxToPx(52),
  111. paddingBottom:rpxToPx(38),
  112. backgroundColor:'#fff'
  113. }}>
  114. <View className="archived_bg" onClick={() => {
  115. jumpPage('/_health/pages/archive')
  116. }}>
  117. <Text className="archived_text" style={{ color: getThemeColor(health.mode) }}>[{health.eatArchived.real_count}/{health.eatArchived.target_count} Meals] Archived {TimeFormatter.dateDescription(new Date(health.eatArchived.timestamp).getTime(), true, false)}</Text>
  118. {
  119. health.eatArchived.images.map((item, index) => {
  120. return <Image src={item} key={index} className="archived_img" mode="aspectFill" />
  121. })
  122. }
  123. <Image src={require('@assets/_health/cell_arrow.png')} style={{
  124. width:rpxToPx(24),
  125. height:rpxToPx(24),
  126. marginLeft:rpxToPx(4),
  127. }}/>
  128. </View>
  129. </View>
  130. }
  131. {
  132. list.map((item, index) => {
  133. return <View ref={refDemo} id={'history_id_0'} key={index}>
  134. <HistoryItem
  135. data={item}
  136. preData={index > 0 ? list[index - 1] : null}
  137. index={index}
  138. mode={props.type ?? health.mode}
  139. fast_type={props.fast_type}
  140. onClick={() => {
  141. jumpPage('/_health/pages/moment_detail')
  142. }} />
  143. {/* {
  144. props.type == 'EAT' && <HistoryEatItem data={item} index={index} />
  145. }
  146. {
  147. props.type == 'FAST' && <HistoryFastItem data={item} index={index} />
  148. }
  149. {
  150. props.type == 'ACTIVE' && <HistoryActiveItem data={item} index={index} />
  151. }
  152. {
  153. props.type == 'SLEEP' && <HistorySleepItem data={item} index={index} />
  154. } */}
  155. </View>
  156. })
  157. }
  158. {
  159. (list.length > 0) && (total == list.length) && <Text className="no_more">没有更多了</Text>
  160. }
  161. </View>
  162. }