ActivityHistory.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { View, Text } from "@tarojs/components";
  2. import './MetricHistory.scss'
  3. import RecordItem from "@/features/common/RecordItem";
  4. import { deleteActivityRecord } from "@/services/trackSomething";
  5. import { useEffect, useState } from "react";
  6. import { useSelector } from "react-redux";
  7. import Modal from "@/components/layout/Modal";
  8. import { ModalType, TimelineType } from "@/utils/types";
  9. import Timeline from "@/components/view/Timeline";
  10. import { TimeFormatter } from "@/utils/time_format";
  11. export default function Component(props: { records: any[] }) {
  12. const user = useSelector((state: any) => state.user);
  13. const [list, setList] = useState(props.records)
  14. useEffect(() => {
  15. setList(props.records)
  16. }, [props.records])
  17. function formateDate(date: string) {
  18. //yyyyMMdd转换成日期,如果是今天,返回今天,如果是昨天,返回昨天,如果是昨天之前,返回日期
  19. const dt = new Date(date.substring(0, 4) + '/' +
  20. date.substring(4, 6) + '/' +
  21. date.substring(6));
  22. const now = new Date();
  23. const diff = now.getTime() - dt.getTime();
  24. const day = 1000 * 60 * 60 * 24;
  25. if (diff < day) {
  26. return '今天';
  27. } else if (diff < 2 * day) {
  28. return '昨天';
  29. } else {
  30. return date.substring(0, 4) + '-' +
  31. date.substring(4, 6) + '-' +
  32. date.substring(6)//dt.toISOString().slice(0, 10);
  33. }
  34. }
  35. function formateHourMinutes(timestamp: number) {
  36. var date = new Date(timestamp)
  37. var hour = date.getHours()
  38. var minutes = date.getMinutes()
  39. return `${hour < 10 ? '0' + hour : hour}:${minutes < 10 ? '0' + minutes : minutes}`
  40. }
  41. function clear() {
  42. }
  43. function deleteRecord(record: any) {
  44. deleteActivityRecord({ id: record.id }).then(res => {
  45. list.splice(list.findIndex(item => item.records[0].id == record.id), 1)
  46. setList([...list])
  47. global.refreshActivity()
  48. })
  49. }
  50. function schedules(item) {
  51. var timelineItems: any = [];
  52. for (var i = item.records.length - 1; i > 0; i--) {
  53. var type = item.records[i].type == 'total' ? '总计' : item.records[i].type == 'sync' ? '同步' : '打卡'
  54. timelineItems.push(
  55. {
  56. status: 'done',
  57. title: type + item.records[i].items[0].value + '步',
  58. content: TimeFormatter.timelineFormatTime(item.records[i].timestamp),
  59. }
  60. )
  61. }
  62. return <View>
  63. <Timeline items={timelineItems} title={formateDate(item.date + '')} type={TimelineType.timeSecond}/>
  64. </View>
  65. }
  66. function showDetail(item) {
  67. var node = (<Modal children={schedules(item)}
  68. modalType={ModalType.center}
  69. dismiss={() => {
  70. global.showModal(false, null)
  71. }}
  72. confirm={() => { }} />);
  73. global.showModal(true, node);
  74. }
  75. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  76. {
  77. user.test_user && <Text style={{ color: '#fff', position: 'absolute', right: 50, top: 0 }} onClick={() => global.clearHistory()}>删除全部</Text>
  78. }
  79. {
  80. (list as any).map(item => {
  81. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  82. <Text className="operate_day">{formateDate(item.date + '')}</Text>
  83. <RecordItem onClick={() => showDetail(item)} delete={() => deleteRecord(item.records[0])}>
  84. <View className="operate_item">
  85. <View className="status_bg">
  86. <Text className="status_text">{item.records[0].type == 'total' ? '总计' : item.records[0].type == 'sync' ? '同步' : '打卡'}</Text>
  87. </View>
  88. <View style={{ width: 12 }} />
  89. <Text className="value">{item.records[0].items[0].value}</Text>
  90. <Text className="unit">步</Text>
  91. <View style={{ flex: 1 }} />
  92. <Text className="time">{formateHourMinutes(item.records[0].timestamp)}</Text>
  93. </View>
  94. </RecordItem>
  95. </View>
  96. })
  97. }
  98. </View>
  99. // return <View style={{ display: 'flex', flexDirection: 'column' }}>
  100. // {
  101. // user.test_user && <Text style={{ color: '#fff', position: 'absolute', right: 50, top: 0 }} onClick={()=>global.clearHistory()}>删除全部</Text>
  102. // }
  103. // {
  104. // (list as any).map(item => {
  105. // return <View style={{ display: 'flex', flexDirection: 'column' }}>
  106. // <Text className="operate_day">{formateDate(item.date + '')}</Text>
  107. // {
  108. // item.records.map(record => {
  109. // return <RecordItem delete={() => deleteRecord(record)}>
  110. // <View className="operate_item">
  111. // <View className="status_bg">
  112. // <Text className="status_text">{record.type == 'total' ? '总计' : record.type == 'sync' ? '同步' : '打卡'}</Text>
  113. // </View>
  114. // <View style={{ width: 12 }} />
  115. // <Text className="value">{record.items[0].value}</Text>
  116. // <Text className="unit">步</Text>
  117. // <View style={{ flex: 1 }} />
  118. // <Text className="time">{formateHourMinutes(record.timestamp)}</Text>
  119. // </View>
  120. // </RecordItem>
  121. // })
  122. // }
  123. // </View>
  124. // })
  125. // }
  126. // </View>
  127. }