ActivityHistory.tsx 7.2 KB

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