import {Calendar, Cascader, Tag} from "antd-mobile"; import dayjs from "dayjs"; import {TaskCount} from "../TaskCount"; import React, {Fragment, useEffect, useLayoutEffect, useMemo, useRef, useState} from "react"; import {getTaskByPid, getTaskCount} from "../../utils"; import {FrownFill, SmileFill} from "antd-mobile-icons"; import {NEW, OVERDUE, UNDER_WAY} from "../../utils/commonConstant"; import {dateStartUtcFormat} from "../../utils/timeFormatUtil"; import './index.css' const ToDoCal = (props) => { const today = new Date() const calRef = useRef(null); const [currentDay, setCurrentDay] = React.useState(new Date()) const [currentMonth, setCurrentMonth] = React.useState(dayjs().set("date", 1).format('YYYY-MM-DD')); // Map key为当前月value为当前月的向 const [listTaskMap, setListTaskMap] = useState(new Map()); // useLayoutEffect useEffect(() => { console.log("getCurrentShowDay", getCurrentShowDay()) const {startDay, endDay} = getCurrentShowDay(); listTaskCount(startDay, endDay); }, [currentMonth]) let loading = false async function listTaskCount(start, end) { if (loading) { return; } loading = true; if (listTaskMap.has(currentMonth)) return const res = await getTaskCount(start, end); setListTaskMap(prevState => { const newMap = new Map(prevState); newMap.set(currentMonth, res); return newMap; }) loading = false; } /* 获取当前展示的日期 */ function getCurrentShowDay() { const firstDay = dayjs(currentMonth).set("date", 1); const startDay = firstDay.subtract(firstDay.day(), 'day'); const endDay = startDay.add(6 * 7, 'day'); return {startDay: dateStartUtcFormat(startDay), endDay: dateStartUtcFormat(endDay)}; } function backToToday() { calRef.current.jumpToToday(); setCurrentDay(today) } return ( { // // 没有任务不显示 // return listTaskMap?.get(currentMonth)?.filter(taskC => dayjs(taskC.todoDay).isSame(dayjs(date), 'date'))?.map(taskC => { // // 如果有逾期的 红色 // // Object.keys() 返回一个包含对象自身所有可枚举属性的数组 // // Object.entries() 返回一个包含对象所有可枚举属性的键值对数组。 // if (Object.keys(taskC.state).length > 0) { // console.log("taskC.state", taskC.state) // if (taskC.state[OVERDUE]) { // return ; // } // // 如果有未完成的任务 warn // if (taskC.state[NEW] || taskC.state[UNDER_WAY]) { // return ; // } // // 任务全部完成 绿色 // return ; // } // }) // // // if (dayjs(date).isSame(today, 'day')) return '今天' // // if (date.getDay() === 0 || date.getDay() === 6) { // // return '周末' // // } // }} renderDate={date => { return listTaskMap?.get(currentMonth)?.filter(taskC => dayjs(taskC.todoDay).isSame(dayjs(date), 'date'))?.map(taskC => { // 如果有逾期的 红色 // Object.keys() 返回一个包含对象自身所有可枚举属性的数组 // Object.entries() 返回一个包含对象所有可枚举属性的键值对数组。 if (Object.keys(taskC.state).length > 0) { console.log("taskC.state", taskC.state) if (taskC.state[OVERDUE]) { return
{dayjs(date).date()}
; } // 如果有未完成的任务 warn if (taskC.state[NEW] || taskC.state[UNDER_WAY]) { return
{dayjs(date).date()}
; } // 任务全部完成 绿色 return
{dayjs(date).date()}
; } return {dayjs(date).date()}; }) }} defaultValue={currentDay} onChange={val => { setCurrentDay(val) }} onPageChange={(year, month) => { console.log(year, month) setCurrentMonth(`${year}-${month}-01`) }} />
) } export default ToDoCal;