import { Box, Card, CardContent, Chip, Grid, Typography } from '@mui/joy' import moment from 'moment' import React, { useState } from 'react' import Calendar from 'react-calendar' import 'react-calendar/dist/Calendar.css' import { useNavigate } from 'react-router-dom' import { UserContext } from '../../contexts/UserContext' import { getTextColorFromBackgroundColor, TASK_COLOR } from '../../utils/Colors' import './Calendar.css' const getAssigneeColor = (assignee, userProfile) => { return assignee === userProfile.id ? TASK_COLOR.ASSIGNED_TO_ME : TASK_COLOR.ASSIGNED_TO_OTHER } const CalendarView = ({ chores }) => { const { userProfile } = React.useContext(UserContext) const [selectedDate, setSeletedDate] = useState(null) const Navigate = useNavigate() const tileContent = ({ date, view }) => { if (view === 'month') { const dayChores = chores.filter(chore => { // Validate chore.nextDueDate before using it if (!chore.nextDueDate) return false const choreDate = new Date(chore.nextDueDate) if (isNaN(choreDate)) return false // Check if the date is invalid if (!date) return false return choreDate.toLocaleDateString() === date.toLocaleDateString() }) return (
{dayChores.map((chore, index) => { if (index > 6) { return null } return ( ) })}
) } return null } return (
{ setSeletedDate(new Date(d.toLocaleDateString())) }} /> {!selectedDate && ( {[ { name: 'Assigned to me', color: TASK_COLOR.ASSIGNED_TO_ME }, { name: 'Assigned to other', color: TASK_COLOR.ASSIGNED_TO_OTHER }, ].map((item, index) => ( {item.name} ))} )} {selectedDate && ( {chores .filter( chore => new Date(chore.nextDueDate)?.toLocaleDateString() === selectedDate.toLocaleDateString(), ) .map((chore, idx) => ( { Navigate('/chores/' + chore.id) }} sx={{ mb: 0.4, py: 1, px: 1, cursor: 'pointer', }} > {moment(chore.nextDueDate).format('hh:mm A')} {chore.name} ))} )}
) } export default CalendarView