MainHistory.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import { View, Text, Image } from "@tarojs/components";
  2. import { forwardRef, useEffect, useImperativeHandle, 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, { useReady } from "@tarojs/taro";
  11. import { getThemeColor } from "./hooks/health_hooks";
  12. import { TimeFormatter } from "@/utils/time_format";
  13. import dayjs from "dayjs";
  14. import { MainColorType } from "@/context/themes/color";
  15. import { IconArrow, IconCellArrow } from "@/components/basic/Icons";
  16. import NoRecord from "@/_health/components/no_record";
  17. import ListFooter from "@/_health/components/list_footer";
  18. let lastMode = ''
  19. export default forwardRef((props: { type?: string, fast_type?: string, updateDate?: any, refreshSuccess?: any }, ref) => {
  20. const [itemLayouts, setItemLayouts] = useState<any>([])
  21. const [list, setList] = useState<any>([])
  22. const [page, setPage] = useState(1)
  23. const [total, setTotal] = useState(0)
  24. const [loaded, setLoaded] = useState(false)
  25. const refDemo = useRef()
  26. const health = useSelector((state: any) => state.health);
  27. const user = useSelector((state: any) => state.user);
  28. const [loading, setLoading] = useState(false)
  29. const healthRef = useRef(health)
  30. useImperativeHandle(ref, () => ({
  31. onScroll: onScroll,
  32. refresh: refresh,
  33. more: more
  34. }))
  35. useEffect(() => {
  36. if (list.length > 0) {
  37. setTimeout(() => {
  38. measureItemLayouts()
  39. }, 300)
  40. }
  41. }, [list])
  42. useEffect(() => {
  43. if (!user.isLogin) {
  44. setList([])
  45. }
  46. }, [user.isLogin])
  47. global.refreshHistory = () => {
  48. refresh()
  49. }
  50. useEffect(() => {
  51. if (props.type) {
  52. loadData(1)
  53. }
  54. }, [props.type])
  55. useEffect(() => {
  56. if (lastMode != health.mode) {
  57. lastMode = health.mode
  58. loadData(1)
  59. }
  60. }, [health.mode])
  61. function measureItemLayouts() {
  62. const query = Taro.createSelectorQuery()
  63. list.forEach((item, index) => {
  64. query.select(`#history-${index}`).boundingClientRect()
  65. });
  66. query.exec((res) => {
  67. var layouts: any = []
  68. res.forEach((rect, index) => {
  69. if (rect) {
  70. layouts[index] = rect.top
  71. }
  72. });
  73. setItemLayouts(layouts)
  74. })
  75. }
  76. function onScroll(e) {
  77. var top = e.detail.scrollTop
  78. if (itemLayouts.length > 0) {
  79. var i = -1
  80. var date = ''
  81. list.forEach((item, index) => {
  82. if (top >= itemLayouts[index] - 50) {
  83. i = index
  84. date = dayjs(item.window_range.start_timestamp).format('YYYY年M月')
  85. }
  86. })
  87. if (props.updateDate) {
  88. props.updateDate({
  89. show: i != -1,
  90. date: date
  91. })
  92. }
  93. }
  94. else {
  95. if (props.updateDate) {
  96. props.updateDate({
  97. show: false,
  98. date: ''
  99. })
  100. }
  101. }
  102. }
  103. function refresh() {
  104. loadData(1)
  105. setPage(1)
  106. }
  107. function more() {
  108. if (loading) return;
  109. if (total == list.length) return;
  110. var index = page;
  111. index++;
  112. setPage(index)
  113. loadData(index)
  114. }
  115. function loadData(index: number) {
  116. var params: any = {
  117. window: props.type ? props.type : health.mode,
  118. limit: 10,
  119. page: index
  120. }
  121. if (props.fast_type) {
  122. params.fast_type = props.fast_type
  123. }
  124. setPage(index)
  125. setLoading(true)
  126. records(params).then(res => {
  127. setLoading(false)
  128. setLoaded(true)
  129. if (index == 1) {
  130. setList((res as any).data)
  131. setTotal((res as any).total)
  132. if (props.refreshSuccess) {
  133. props.refreshSuccess()
  134. }
  135. }
  136. else {
  137. setList([...list, ...(res as any).data])
  138. }
  139. // if ((res as any).data.length > 0) {
  140. // setTimeout(() => {
  141. // // var array:any = [];
  142. // // (res as any).data.map((item,index)=>{
  143. // // array.push('#history_id_'+index)
  144. // // })
  145. // // var ids = array.join(',')
  146. // // console.log(array)
  147. // // console.log(ids)
  148. // const query = Taro.createSelectorQuery();
  149. // query.selectAll('#history_id_0').boundingClientRect((rect) => {
  150. // console.log(rect)
  151. // }).exec();
  152. // }, 1000)
  153. // }
  154. }).catch(e => {
  155. setLoading(false)
  156. })
  157. }
  158. function historyMonth(index) {
  159. var showDate = false;
  160. var dateStr = ''
  161. if (index == 0) {
  162. var currentDate = dayjs(list[index].window_range.start_timestamp).format('YYYY年M月')
  163. var now = dayjs().format('YYYY年M月')
  164. showDate = true
  165. dateStr = currentDate
  166. }
  167. else {
  168. var currentDate = dayjs(list[index].window_range.start_timestamp).format('YYYY年M月')
  169. var now = dayjs(list[index - 1].window_range.start_timestamp).format('YYYY年M月')
  170. if (currentDate != now) {
  171. showDate = true
  172. dateStr = currentDate
  173. }
  174. }
  175. if (showDate) {
  176. return <View className="history_year_month h30 g01">{dateStr}</View>
  177. }
  178. return <View />
  179. }
  180. if (!loaded || health.mode == 'DAY' || health.mode == 'NIGHT')
  181. return <View />
  182. if (!user.isLogin) return <View />
  183. return <View style={{ width: rpxToPx(750), marginTop: rpxToPx(36) }}>
  184. {
  185. (list.length >= 0 || health.mode == 'EAT') && <View className="recent">
  186. <Text className="h50 bold" >Recent</Text>
  187. {
  188. health.mode == 'EAT' && <View onClick={() => {
  189. jumpPage('/_health/pages/archive')
  190. }}>
  191. <Image className="archive" src={require('@assets/_health/archive.png')} />
  192. </View>
  193. }
  194. {/* <View className="border_footer_line" /> */}
  195. </View>
  196. }
  197. {
  198. health.mode == 'EAT' && health.eatArchived && health.eatArchived.archived && <View style={{
  199. display: 'flex',
  200. flexDirection: 'column',
  201. alignItems: 'center',
  202. paddingTop: rpxToPx(36),
  203. paddingBottom: rpxToPx(36),
  204. backgroundColor: '#fff',
  205. position: 'relative'
  206. }}>
  207. <View className="archived_bg" onClick={() => {
  208. jumpPage('/_health/pages/archive')
  209. }}>
  210. <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>
  211. {
  212. health.eatArchived.images.map((item, index) => {
  213. return <Image src={item} key={index} className="archived_img" mode="aspectFill" />
  214. })
  215. }
  216. <IconArrow color={MainColorType.g03} width={rpxToPx(34)} />
  217. {/* <IconCellArrow color={MainColorType.g03} width={rpxToPx(34)} /> */}
  218. {/* <Image src={require('@assets/_health/cell_arrow.png')} style={{
  219. width: rpxToPx(24),
  220. height: rpxToPx(24),
  221. marginLeft: rpxToPx(4),
  222. }} /> */}
  223. </View>
  224. <View className="border_footer_line" />
  225. </View>
  226. }
  227. {
  228. list.length == 0 && <NoRecord />
  229. }
  230. {
  231. list.map((item, index) => {
  232. return <View id={`history-${index}`} key={index}>
  233. {
  234. historyMonth(index)
  235. }
  236. <HistoryItem
  237. data={item}
  238. preData={index > 0 ? list[index - 1] : null}
  239. index={index}
  240. mode={props.type ?? health.mode}
  241. fast_type={props.fast_type}
  242. onClick={() => {
  243. jumpPage('/_health/pages/moment_detail')
  244. }} />
  245. {/* {
  246. props.type == 'EAT' && <HistoryEatItem data={item} index={index} />
  247. }
  248. {
  249. props.type == 'FAST' && <HistoryFastItem data={item} index={index} />
  250. }
  251. {
  252. props.type == 'ACTIVE' && <HistoryActiveItem data={item} index={index} />
  253. }
  254. {
  255. props.type == 'SLEEP' && <HistorySleepItem data={item} index={index} />
  256. } */}
  257. </View>
  258. })
  259. }
  260. <ListFooter noMore={(list.length > 0) && (total == list.length)} />
  261. </View>
  262. })