| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import { View, Text } from "@tarojs/components";
- import './MetricHistory.scss'
- import RecordItem from "@/features/common/RecordItem";
- import { deleteActivityRecord } from "@/services/trackSomething";
- import { useEffect, useState } from "react";
- import { useSelector } from "react-redux";
- import Modal from "@/components/layout/Modal";
- import { ModalType, TimelineType } from "@/utils/types";
- import Timeline from "@/components/view/Timeline";
- import { TimeFormatter } from "@/utils/time_format";
- export default function Component(props: { records: any[] }) {
- const user = useSelector((state: any) => state.user);
- const [list, setList] = useState(props.records)
- useEffect(() => {
- setList(props.records)
- }, [props.records])
- function formateDate(date: string) {
- //yyyyMMdd转换成日期,如果是今天,返回今天,如果是昨天,返回昨天,如果是昨天之前,返回日期
- const dt = new Date(date.substring(0, 4) + '/' +
- date.substring(4, 6) + '/' +
- date.substring(6));
- const now = new Date();
- const diff = now.getTime() - dt.getTime();
- const day = 1000 * 60 * 60 * 24;
- if (diff < day) {
- return '今天';
- } else if (diff < 2 * day) {
- return '昨天';
- } else {
- return date.substring(0, 4) + '-' +
- date.substring(4, 6) + '-' +
- date.substring(6)//dt.toISOString().slice(0, 10);
- }
- }
- function formateHourMinutes(timestamp: number) {
- var date = new Date(timestamp)
- var hour = date.getHours()
- var minutes = date.getMinutes()
- return `${hour < 10 ? '0' + hour : hour}:${minutes < 10 ? '0' + minutes : minutes}`
- }
- function clear() {
- }
- function deleteRecord(record: any) {
- deleteActivityRecord({ id: record.id }).then(res => {
- list.splice(list.findIndex(item => item.records[0].id == record.id), 1)
- setList([...list])
- global.refreshActivity()
- })
- }
- function schedules(item) {
- var timelineItems: any = [];
- for (var i = item.records.length - 1; i > 0; i--) {
- var type = item.records[i].type == 'total' ? '总计' : item.records[i].type == 'sync' ? '同步' : '打卡'
- timelineItems.push(
- {
- status: 'done',
- title: type + item.records[i].items[0].value + '步',
- content: TimeFormatter.timelineFormatTime(item.records[i].timestamp),
- }
- )
- }
- return <View>
- <Timeline items={timelineItems} title={formateDate(item.date + '')} type={TimelineType.timeSecond}/>
- </View>
- }
- function showDetail(item) {
- var node = (<Modal children={schedules(item)}
- modalType={ModalType.center}
- dismiss={() => {
- global.showModal(false, null)
- }}
- confirm={() => { }} />);
- global.showModal(true, node);
- }
- return <View style={{ display: 'flex', flexDirection: 'column' }}>
- {
- user.test_user && <Text style={{ color: '#fff', position: 'absolute', right: 50, top: 0 }} onClick={() => global.clearHistory()}>删除全部</Text>
- }
- {
- (list as any).map(item => {
- return <View style={{ display: 'flex', flexDirection: 'column' }}>
- <Text className="operate_day">{formateDate(item.date + '')}</Text>
- <RecordItem onClick={() => showDetail(item)} delete={() => deleteRecord(item.records[0])}>
- <View className="operate_item">
- <View className="status_bg">
- <Text className="status_text">{item.records[0].type == 'total' ? '总计' : item.records[0].type == 'sync' ? '同步' : '打卡'}</Text>
- </View>
- <View style={{ width: 12 }} />
- <Text className="value">{item.records[0].items[0].value}</Text>
- <Text className="unit">步</Text>
- <View style={{ flex: 1 }} />
- <Text className="time">{formateHourMinutes(item.records[0].timestamp)}</Text>
- </View>
- </RecordItem>
- </View>
- })
- }
- </View>
- // return <View style={{ display: 'flex', flexDirection: 'column' }}>
- // {
- // user.test_user && <Text style={{ color: '#fff', position: 'absolute', right: 50, top: 0 }} onClick={()=>global.clearHistory()}>删除全部</Text>
- // }
- // {
- // (list as any).map(item => {
- // return <View style={{ display: 'flex', flexDirection: 'column' }}>
- // <Text className="operate_day">{formateDate(item.date + '')}</Text>
- // {
- // item.records.map(record => {
- // return <RecordItem delete={() => deleteRecord(record)}>
- // <View className="operate_item">
- // <View className="status_bg">
- // <Text className="status_text">{record.type == 'total' ? '总计' : record.type == 'sync' ? '同步' : '打卡'}</Text>
- // </View>
- // <View style={{ width: 12 }} />
- // <Text className="value">{record.items[0].value}</Text>
- // <Text className="unit">步</Text>
- // <View style={{ flex: 1 }} />
- // <Text className="time">{formateHourMinutes(record.timestamp)}</Text>
- // </View>
- // </RecordItem>
- // })
- // }
- // </View>
- // })
- // }
- // </View>
- }
|