import CancelIcon from '@mui/icons-material/Cancel' import CheckCircleIcon from '@mui/icons-material/CheckCircle' import CircleIcon from '@mui/icons-material/Circle' import { Cell, Legend, Pie, PieChart, Tooltip } from 'recharts' import { EventBusy, Toll } from '@mui/icons-material' import { Box, Button, Card, Chip, Container, Divider, Grid, Link, Stack, Tab, TabList, Tabs, Typography, } from '@mui/joy' import React, { useEffect } from 'react' import { UserContext } from '../../contexts/UserContext' import { useChores, useChoresHistory } from '../../queries/ChoreQueries' import { ChoresGrouper } from '../../utils/Chores' import { TASK_COLOR } from '../../utils/Colors.jsx' import LoadingComponent from '../components/Loading' const groupByDate = history => { const aggregated = {} for (let i = 0; i < history.length; i++) { const item = history[i] const date = new Date(item.completedAt).toLocaleDateString() if (!aggregated[date]) { aggregated[date] = [] } aggregated[date].push(item) } return aggregated } const ChoreHistoryItem = ({ time, name, points, status }) => { const statusIcon = { completed: , missed: , pending: , } return ( {time} {statusIcon[status] ? statusIcon[status] : statusIcon['completed']} {name} {points && ( }> {`${points} points`} )} ) } const ChoreHistoryTimeline = ({ history }) => { const groupedHistory = groupByDate(history) return ( Activities Timeline {Object.entries(groupedHistory).map(([date, items]) => ( {new Date(date).toLocaleDateString([], { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric', })} {items.map(record => ( <> ))} ))} ) } const renderPieChart = (data, size, isPrimary) => ( {data.map((entry, index) => ( ))} {isPrimary && } {isPrimary && ( `${label}: ${value.payload.value}`} /> )} ) const UserActivites = () => { const { userProfile } = React.useContext(UserContext) const [tabValue, setTabValue] = React.useState(30) const [selectedHistory, setSelectedHistory] = React.useState([]) const [selectedChart, setSelectedChart] = React.useState('history') const [historyPieChartData, setHistoryPieChartData] = React.useState([]) const [choreDuePieChartData, setChoreDuePieChartData] = React.useState([]) const [choresAssignedChartData, setChoresAssignedChartData] = React.useState( [], ) const [choresPriorityChartData, setChoresPriorityChartData] = React.useState( [], ) const { data: choresData, isLoading: isChoresLoading } = useChores(true) const { data: choresHistory, isChoresHistoryLoading, handleLimitChange: refetchHistory, } = useChoresHistory(tabValue ? tabValue : 30, false) useEffect(() => { if (!isChoresHistoryLoading && !isChoresLoading && choresHistory) { const enrichedHistory = choresHistory.res.map(item => { const chore = choresData.res.find(chore => chore.id === item.choreId) return { ...item, choreName: chore?.name, } }) setSelectedHistory(enrichedHistory) setHistoryPieChartData(generateHistoryPieChartData(enrichedHistory)) } }, [isChoresHistoryLoading, isChoresLoading, choresHistory]) useEffect(() => { if (!isChoresLoading && choresData) { const choreDuePieChartData = generateChoreDuePieChartData(choresData.res) setChoreDuePieChartData(choreDuePieChartData) setChoresAssignedChartData(generateChoreAssignedChartData(choresData.res)) setChoresPriorityChartData( generateChorePriorityPieChartData(choresData.res), ) } }, [isChoresLoading, choresData]) const generateChoreAssignedChartData = chores => { var assignedToMe = 0 var assignedToOthers = 0 chores.forEach(chore => { if (chore.assignedTo === userProfile.id) { assignedToMe++ } else assignedToOthers++ }) const group = [] if (assignedToMe > 0) { group.push({ label: `Assigned to me`, value: assignedToMe, color: TASK_COLOR.ASSIGNED_TO_ME, id: 1, }) } if (assignedToOthers > 0) { group.push({ label: `Assigned to others`, value: assignedToOthers, color: TASK_COLOR.ASSIGNED_TO_OTHERS, id: 2, }) } return group } const generateChoreDuePieChartData = chores => { const groups = ChoresGrouper('due_date', chores) return groups .map(group => { return { label: group.name, value: group.content.length, color: group.color, id: group.name, } }) .filter(item => item.value > 0) } const generateChorePriorityPieChartData = chores => { const groups = ChoresGrouper('priority', chores) return groups .map(group => { return { label: group.name, value: group.content.length, color: group.color, id: group.name, } }) .filter(item => item.value > 0) } const generateHistoryPieChartData = history => { const totalCompleted = history.filter(item => item.dueDate > item.completedAt).length || 0 const totalLate = history.filter(item => item.dueDate < item.completedAt).length || 0 const totalNoDueDate = history.filter(item => !item.dueDate).length || 0 return [ { label: `On time`, value: totalCompleted, color: TASK_COLOR.COMPLETED, id: 1, }, { label: `Late`, value: totalLate, color: TASK_COLOR.LATE, id: 2, }, { label: `Completed`, value: totalNoDueDate, color: TASK_COLOR.ANYTIME, id: 3, }, ] } if (isChoresHistoryLoading || isChoresLoading) { return } const COLORS = historyPieChartData.map(item => item.color) const chartData = { history: { data: historyPieChartData, title: 'Status', description: 'Completed tasks status', }, due: { data: choreDuePieChartData, title: 'Due Date', description: 'Current tasks due date', }, assigned: { data: choresAssignedChartData, title: 'Assignee', description: 'Tasks assigned to you vs others', }, priority: { data: choresPriorityChartData, title: 'Priority', description: 'Tasks by priority', }, } if (!choresData.res?.length > 0 || !choresHistory?.res?.length > 0) { return ( No activities You have no activities for the selected period. ) } return ( { setTabValue(tabValue) refetchHistory(tabValue) }} defaultValue={7} sx={{ py: 0.5, borderRadius: 16, maxWidth: 400, mb: 1, }} > {[ { label: '7 Days', value: 7 }, { label: '30 Days', value: 30 }, { label: '90 Days', value: 90 }, ].map((tab, index) => ( {tab.label} ))} {chartData[selectedChart].title} {chartData[selectedChart].description} {renderPieChart(chartData[selectedChart].data, 250, true)} {Object.entries(chartData) .filter(([key]) => key !== selectedChart) .map(([key, { data, title }]) => ( setSelectedChart(key)} sx={{ cursor: 'pointer', p: 1 }} > {title} {renderPieChart(data, 75, false)} ))} ) } export default UserActivites