ActivityHistory.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { View, Text, Image } 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. import './ActivityHistory.scss'
  12. import CenterContentTitleModal from "@/features/common/CenterContentTitleModal";
  13. import { useRouter } from "@tarojs/taro";
  14. export default function Component(props: { records: any[] }) {
  15. const user = useSelector((state: any) => state.user);
  16. const [list, setList] = useState(props.records)
  17. const router = useRouter();
  18. useEffect(() => {
  19. setList(props.records)
  20. }, [props.records])
  21. function formateDate(date: string) {
  22. //yyyyMMdd转换成日期,如果是今天,返回今天,如果是昨天,返回昨天,如果是昨天之前,返回日期
  23. const dt = new Date(date.substring(0, 4) + '/' +
  24. date.substring(4, 6) + '/' +
  25. date.substring(6));
  26. const now = new Date();
  27. const diff = now.getTime() - dt.getTime();
  28. const day = 1000 * 60 * 60 * 24;
  29. if (diff < day) {
  30. return '今天';
  31. } else if (diff < 2 * day) {
  32. return '昨天';
  33. } else {
  34. return date.substring(0, 4) + '-' +
  35. date.substring(4, 6) + '-' +
  36. date.substring(6)//dt.toISOString().slice(0, 10);
  37. }
  38. }
  39. function formateHourMinutes(timestamp: number) {
  40. var date = new Date(timestamp)
  41. var hour = date.getHours()
  42. var minutes = date.getMinutes()
  43. return `${hour < 10 ? '0' + hour : hour}:${minutes < 10 ? '0' + minutes : minutes}`
  44. }
  45. function clear() {
  46. }
  47. function deleteRecord(record: any) {
  48. deleteActivityRecord({ id: record.id }).then(res => {
  49. list.splice(list.findIndex(item => item.records[0].id == record.id), 1)
  50. setList([...list])
  51. global.refreshActivity()
  52. })
  53. }
  54. function schedules(item) {
  55. var timelineItems: any = [];
  56. for (var i = item.records.length - 1; i > 0; i--) {
  57. var type = item.records[i].type == 'total' ? '总计' : item.records[i].type == 'sync' ? '同步' : '打卡'
  58. timelineItems.push(
  59. {
  60. status: 'done',
  61. title: type + item.records[i].items[0].value + '步',
  62. content: TimeFormatter.timelineFormatTime(item.records[i].timestamp),
  63. date: TimeFormatter.dateDescription(item.records[i].timestamp),
  64. color: item.records[i].type == 'sync' ?'#fff':router.params.themeColor
  65. }
  66. )
  67. }
  68. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  69. <Text className="box_title">打卡详情</Text>
  70. <Text className="box_subtitle">{TimeFormatter.getMonthAndDayByDate(item.date)}</Text>
  71. <View style={{display:'flex',alignItems:'center',position:'relative',width:'100%',flexDirection:'column'}}>
  72. <Timeline items={timelineItems} type={TimelineType.timeFirst} />
  73. </View>
  74. </View>
  75. return <CenterContentTitleModal title={TimeFormatter.getMonthAndDayByDate(item.date)}>
  76. <Timeline items={timelineItems} type={TimelineType.timeFirst} />
  77. </CenterContentTitleModal>
  78. // return <View style={{ display: 'flex', width: '100%', flexDirection: 'column', alignItems: 'center' }}>
  79. // <View style={{display:'flex',flexDirection:'row',width:'100%'}}>
  80. // <Text className="modalTitle">{formateDate(item.date + '')}</Text>
  81. // </View>
  82. // <Timeline items={timelineItems} type={TimelineType.timeSecond} />
  83. // </View>
  84. }
  85. function showDetail(item) {
  86. var node = (<Modal children={schedules(item)}
  87. modalType={ModalType.center}
  88. dismiss={() => {
  89. global.showModal(false, null)
  90. }}
  91. confirm={() => { }} />);
  92. global.showModal(true, node);
  93. }
  94. var lastYearStr = '2023年'
  95. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  96. {
  97. user.test_user && <Text style={{ color: '#fff', position: 'absolute', right: 50, top: 0 }} onClick={() => global.clearHistory()}>删除全部</Text>
  98. }
  99. {
  100. (list as any).map(item => {
  101. var showYear = lastYearStr != TimeFormatter.getYearByDate(item.date) && lastYearStr != '2023年'
  102. lastYearStr = TimeFormatter.getYearByDate(item.date)
  103. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  104. {
  105. showYear && <Text className="year" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>{TimeFormatter.getYearByDate(item.date)}</Text>
  106. }
  107. <Text className="operate_day" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>{TimeFormatter.getMonthAndDayByDate(item.date)}</Text>
  108. <RecordItem onClick={() => showDetail(item)} delete={() => deleteRecord(item.records[0])}>
  109. <View className="operate_item">
  110. {/* <View className="status_bg">
  111. <Text className="status_text">{item.records[0].type == 'total' ? '总计' : item.records[0].type == 'sync' ? '同步' : '打卡'}</Text>
  112. </View>
  113. <View style={{ width: 12 }} /> */}
  114. <Text className="value" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>{item.records[0].items[0].value}</Text>
  115. <Text className="unit">步</Text>
  116. <View style={{ flex: 1 }} />
  117. <Text className="time">{formateHourMinutes(item.records[0].timestamp)}</Text>
  118. <Image className="operate_arrow" src={require('@assets/images/arrow3.png')} />
  119. </View>
  120. </RecordItem>
  121. </View>
  122. })
  123. }
  124. </View>
  125. // return <View style={{ display: 'flex', flexDirection: 'column' }}>
  126. // {
  127. // user.test_user && <Text style={{ color: '#fff', position: 'absolute', right: 50, top: 0 }} onClick={()=>global.clearHistory()}>删除全部</Text>
  128. // }
  129. // {
  130. // (list as any).map(item => {
  131. // return <View style={{ display: 'flex', flexDirection: 'column' }}>
  132. // <Text className="operate_day">{formateDate(item.date + '')}</Text>
  133. // {
  134. // item.records.map(record => {
  135. // return <RecordItem delete={() => deleteRecord(record)}>
  136. // <View className="operate_item">
  137. // <View className="status_bg">
  138. // <Text className="status_text">{record.type == 'total' ? '总计' : record.type == 'sync' ? '同步' : '打卡'}</Text>
  139. // </View>
  140. // <View style={{ width: 12 }} />
  141. // <Text className="value">{record.items[0].value}</Text>
  142. // <Text className="unit">步</Text>
  143. // <View style={{ flex: 1 }} />
  144. // <Text className="time">{formateHourMinutes(record.timestamp)}</Text>
  145. // </View>
  146. // </RecordItem>
  147. // })
  148. // }
  149. // </View>
  150. // })
  151. // }
  152. // </View>
  153. }