MetricHistory.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { View, Text, Image } from "@tarojs/components";
  2. import './MetricHistory.scss'
  3. import RecordItem from "@/features/common/RecordItem";
  4. import { deleteMetricRecord } from "@/services/trackSomething";
  5. import { useEffect, useState } from "react";
  6. import { useSelector } from "react-redux";
  7. import { TimeFormatter } from "@/utils/time_format";
  8. import { ColorType } from "@/context/themes/color";
  9. import { rpxToPx } from "@/utils/tools";
  10. import Taro from "@tarojs/taro";
  11. export default function Component(props: { records: any[] }) {
  12. const user = useSelector((state: any) => state.user);
  13. const [list, setList] = useState(props.records)
  14. const [selReference, setSelReference] = useState(global.selMetricItem && global.selMetricItem.references.length > 0 ? global.selMetricItem.references[0] : null)
  15. useEffect(() => {
  16. setList(props.records)
  17. }, [props.records])
  18. function formateDate(date: string) {
  19. //yyyyMMdd转换成日期,如果是今天,返回今天,如果是昨天,返回昨天,如果是昨天之前,返回日期
  20. const dt = new Date(date.substring(0, 4) + '/' +
  21. date.substring(4, 6) + '/' +
  22. date.substring(6));
  23. const now = new Date();
  24. const diff = now.getTime() - dt.getTime();
  25. const day = 1000 * 60 * 60 * 24;
  26. if (diff < day) {
  27. return TimeFormatter.getTodayUnit();
  28. } else if (diff < 2 * day) {
  29. return TimeFormatter.getYesterdayUnit();
  30. } else {
  31. return date.substring(0, 4) + '-' +
  32. date.substring(4, 6) + '-' +
  33. date.substring(6)//dt.toISOString().slice(0, 10);
  34. }
  35. }
  36. function formateHourMinutes(timestamp: number) {
  37. var date = new Date(timestamp)
  38. var hour = date.getHours()
  39. var minutes = date.getMinutes()
  40. return `${hour < 10 ? '0' + hour : hour}:${minutes < 10 ? '0' + minutes : minutes}`
  41. }
  42. function deleteRecord(record: any) {
  43. deleteMetricRecord({ id: record.id }).then(res => {
  44. list.map(item => {
  45. var index = item.records.findIndex(item => item.id == record.id);
  46. if (index >= 0)
  47. item.records.splice(index, 1)
  48. })
  49. for (let i = list.length; i > 0; i--) {
  50. if (list[i - 1].records.length == 0) {
  51. list.splice(i - 1, 1)
  52. }
  53. }
  54. // console.log(list)
  55. setList(JSON.parse(JSON.stringify(list)))
  56. global.refreshMetric()
  57. })
  58. }
  59. function showMore() {
  60. var list:any = []
  61. global.selMetricItem.references.map((item)=>{
  62. list.push((item as any).standard)
  63. })
  64. Taro.showActionSheet({
  65. itemList: list,
  66. success: function (res) {
  67. // console.log(res.tapIndex)
  68. setSelReference(global.selMetricItem.references[res.tapIndex])
  69. },
  70. fail: function (res) {
  71. console.log(res.errMsg)
  72. }
  73. })
  74. }
  75. function metricStandard() {
  76. if (global.selMetricItem && global.selMetricItem.references.length > 0) {
  77. return <View style={{ display: 'flex', flexDirection: 'column', position: 'relative' }}>
  78. <Text className="cell_header">{selReference.standard}</Text>
  79. <Text onClick={showMore} style={{ color: global.selMetricItem.theme_color, position: 'absolute', right: rpxToPx(86), top: -rpxToPx(10) }}>More</Text>
  80. {
  81. selReference.categories.map((item, index) => {
  82. var cellClassName = 'cell_full'
  83. var showLine = false
  84. if (selReference.categories.length > 1) {
  85. if (index == 0) {
  86. cellClassName = 'cell_top'
  87. showLine = true
  88. }
  89. else if (index == selReference.categories.length - 1) {
  90. cellClassName = 'cell_bottom'
  91. showLine = false
  92. }
  93. else {
  94. cellClassName = 'cell_center'
  95. showLine = true
  96. }
  97. }
  98. return <View className={cellClassName} key={index}>
  99. <Text className="cell_title" style={{ color: global.selMetricItem.theme_color }}>{item.value_range}</Text>
  100. <Text className="cell_value" style={{ color: '#fff' }}>{item.category}</Text>
  101. {
  102. showLine && <View className="cell_line" style={{ height: 1 }} />
  103. }
  104. </View>
  105. })
  106. }
  107. </View>
  108. }
  109. }
  110. var lastYearStr = '2025年'
  111. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  112. {
  113. user.test_user && <Text style={{ color: '#fff', position: 'absolute', right: 50, top: 0 }} onClick={() => global.clearHistory()}>删除全部</Text>
  114. }
  115. {
  116. metricStandard()
  117. }
  118. {
  119. (list as any).map(item => {
  120. var showYear = lastYearStr != TimeFormatter.getYearByDate(item.date)
  121. lastYearStr = TimeFormatter.getYearByDate(item.date)
  122. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  123. {
  124. showYear && <Text className="year" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>{TimeFormatter.getYearByDate(item.date)}</Text>
  125. }
  126. <Text className="operate_day" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>{TimeFormatter.getMonthAndDayByDate(item.date)}</Text>
  127. {
  128. item.records.map(record => {
  129. return <RecordItem delete={() => deleteRecord(record)}>
  130. <View className="operate_item1">
  131. {/* <View className="status_bg">
  132. </View>
  133. <View style={{ width: 12 }} /> */}
  134. <Text className="value" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>{record.items[0].value}</Text>
  135. {
  136. record.items.length > 1 && <Text className="value">/{record.items[1].value}</Text>
  137. }
  138. {
  139. record.items.length > 2 && <Text className="value">/{record.items[2].value}</Text>
  140. }
  141. <Text className="unit">{record.items[0].unit}</Text>
  142. <View style={{ flex: 1 }} />
  143. <Text className="time">{formateHourMinutes(record.timestamp)}</Text>
  144. </View>
  145. </RecordItem>
  146. })
  147. }
  148. </View>
  149. })
  150. }
  151. </View>
  152. }