2024-07-16 19:37:18 -04:00
|
|
|
import { Checklist, EventBusy, Group, Timelapse } from '@mui/icons-material'
|
2024-06-30 18:55:39 -04:00
|
|
|
import {
|
|
|
|
Avatar,
|
|
|
|
Button,
|
2024-07-16 19:37:18 -04:00
|
|
|
Chip,
|
2024-06-30 18:55:39 -04:00
|
|
|
Container,
|
|
|
|
Grid,
|
|
|
|
List,
|
|
|
|
ListItem,
|
|
|
|
ListItemContent,
|
|
|
|
Sheet,
|
|
|
|
Typography,
|
|
|
|
} from '@mui/joy'
|
|
|
|
import moment from 'moment'
|
|
|
|
import React, { useEffect, useState } from 'react'
|
|
|
|
import { Link, useParams } from 'react-router-dom'
|
|
|
|
import { API_URL } from '../../Config'
|
|
|
|
import { GetAllCircleMembers } from '../../utils/Fetcher'
|
|
|
|
import { Fetch } from '../../utils/TokenManager'
|
2024-07-16 19:37:18 -04:00
|
|
|
import LoadingComponent from '../components/Loading'
|
2024-07-09 17:39:16 -04:00
|
|
|
import HistoryCard from './HistoryCard'
|
2024-06-30 18:55:39 -04:00
|
|
|
|
|
|
|
const ChoreHistory = () => {
|
|
|
|
const [choreHistory, setChoresHistory] = useState([])
|
|
|
|
const [userHistory, setUserHistory] = useState([])
|
|
|
|
const [performers, setPerformers] = useState([])
|
|
|
|
const [historyInfo, setHistoryInfo] = useState([])
|
|
|
|
|
|
|
|
const [isLoading, setIsLoading] = useState(true) // Add loading state
|
|
|
|
const { choreId } = useParams()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setIsLoading(true) // Start loading
|
|
|
|
|
|
|
|
Promise.all([
|
|
|
|
Fetch(`${API_URL}/chores/${choreId}/history`).then(res => res.json()),
|
|
|
|
GetAllCircleMembers().then(res => res.json()),
|
|
|
|
])
|
|
|
|
.then(([historyData, usersData]) => {
|
|
|
|
setChoresHistory(historyData.res)
|
|
|
|
|
|
|
|
const newUserChoreHistory = {}
|
|
|
|
historyData.res.forEach(choreHistory => {
|
|
|
|
const userId = choreHistory.completedBy
|
|
|
|
newUserChoreHistory[userId] = (newUserChoreHistory[userId] || 0) + 1
|
|
|
|
})
|
|
|
|
setUserHistory(newUserChoreHistory)
|
|
|
|
|
|
|
|
setPerformers(usersData.res)
|
|
|
|
updateHistoryInfo(historyData.res, newUserChoreHistory, usersData.res)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error('Error fetching data:', error)
|
|
|
|
// Handle errors, e.g., show an error message to the user
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setIsLoading(false) // Finish loading
|
|
|
|
})
|
|
|
|
}, [choreId])
|
|
|
|
|
|
|
|
const updateHistoryInfo = (histories, userHistories, performers) => {
|
|
|
|
// average delay for task completaion from due date:
|
|
|
|
|
|
|
|
const averageDelay =
|
|
|
|
histories.reduce((acc, chore) => {
|
|
|
|
if (chore.dueDate) {
|
|
|
|
// Only consider chores with a due date
|
|
|
|
return acc + moment(chore.completedAt).diff(chore.dueDate, 'hours')
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, 0) / histories.length
|
|
|
|
const averageDelayMoment = moment.duration(averageDelay, 'hours')
|
|
|
|
const maximumDelay = histories.reduce((acc, chore) => {
|
|
|
|
if (chore.dueDate) {
|
|
|
|
// Only consider chores with a due date
|
|
|
|
const delay = moment(chore.completedAt).diff(chore.dueDate, 'hours')
|
|
|
|
return delay > acc ? delay : acc
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, 0)
|
|
|
|
|
|
|
|
const maxDelayMoment = moment.duration(maximumDelay, 'hours')
|
|
|
|
|
|
|
|
// find max value in userHistories:
|
|
|
|
const userCompletedByMost = Object.keys(userHistories).reduce((a, b) =>
|
|
|
|
userHistories[a] > userHistories[b] ? a : b,
|
|
|
|
)
|
|
|
|
const userCompletedByLeast = Object.keys(userHistories).reduce((a, b) =>
|
|
|
|
userHistories[a] < userHistories[b] ? a : b,
|
|
|
|
)
|
|
|
|
|
|
|
|
const historyInfo = [
|
|
|
|
{
|
2024-07-16 19:37:18 -04:00
|
|
|
icon: <Checklist />,
|
|
|
|
text: 'Total Completed',
|
|
|
|
subtext: `${histories.length} times`,
|
2024-06-30 18:55:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-16 19:37:18 -04:00
|
|
|
icon: <Timelapse />,
|
|
|
|
text: 'Usually Within',
|
|
|
|
subtext: moment.duration(averageDelayMoment).humanize(),
|
2024-06-30 18:55:39 -04:00
|
|
|
},
|
|
|
|
{
|
2024-07-16 19:37:18 -04:00
|
|
|
icon: <Timelapse />,
|
|
|
|
text: 'Maximum Delay',
|
|
|
|
subtext: moment.duration(maxDelayMoment).humanize(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: <Avatar />,
|
|
|
|
text: ' Completed Most',
|
|
|
|
subtext: `${
|
2024-06-30 18:55:39 -04:00
|
|
|
performers.find(p => p.userId === Number(userCompletedByMost))
|
|
|
|
?.displayName
|
2024-07-16 19:37:18 -04:00
|
|
|
} `,
|
|
|
|
},
|
|
|
|
// contributes:
|
|
|
|
{
|
|
|
|
icon: <Group />,
|
|
|
|
text: 'Total Performers',
|
|
|
|
subtext: `${Object.keys(userHistories).length} users`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: <Avatar />,
|
|
|
|
text: 'Last Completed',
|
|
|
|
subtext: `${
|
|
|
|
performers.find(p => p.userId === Number(histories[0].completedBy))
|
|
|
|
?.displayName
|
|
|
|
}`,
|
2024-06-30 18:55:39 -04:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
setHistoryInfo(historyInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLoading) {
|
2024-07-16 19:37:18 -04:00
|
|
|
return <LoadingComponent />
|
2024-06-30 18:55:39 -04:00
|
|
|
}
|
|
|
|
if (!choreHistory.length) {
|
|
|
|
return (
|
|
|
|
<Container
|
|
|
|
maxWidth='md'
|
|
|
|
sx={{
|
|
|
|
textAlign: 'center',
|
|
|
|
display: 'flex',
|
|
|
|
// make sure the content is centered vertically:
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
flexDirection: 'column',
|
|
|
|
height: '50vh',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<EventBusy
|
|
|
|
sx={{
|
|
|
|
fontSize: '6rem',
|
|
|
|
// color: 'text.disabled',
|
|
|
|
mb: 1,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Typography level='h3' gutterBottom>
|
|
|
|
No History Yet
|
|
|
|
</Typography>
|
|
|
|
<Typography level='body1'>
|
|
|
|
You haven't completed any tasks. Once you start finishing tasks,
|
|
|
|
they'll show up here.
|
|
|
|
</Typography>
|
|
|
|
<Button variant='soft' sx={{ mt: 2 }}>
|
|
|
|
<Link to='/my/chores'>Go back to chores</Link>
|
|
|
|
</Button>
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Container maxWidth='md'>
|
2024-07-16 19:37:18 -04:00
|
|
|
<Typography level='title-md' mb={1.5}>
|
2024-06-30 18:55:39 -04:00
|
|
|
Summary:
|
|
|
|
</Typography>
|
2024-07-16 19:37:18 -04:00
|
|
|
<Sheet
|
|
|
|
// sx={{
|
|
|
|
// mb: 1,
|
|
|
|
// borderRadius: 'lg',
|
|
|
|
// p: 2,
|
|
|
|
// }}
|
|
|
|
sx={{ borderRadius: 'sm', p: 2 }}
|
|
|
|
variant='outlined'
|
|
|
|
>
|
|
|
|
<Grid container spacing={1}>
|
|
|
|
{historyInfo.map((info, index) => (
|
|
|
|
<Grid item xs={4} key={index}>
|
|
|
|
{/* divider between the list items: */}
|
|
|
|
|
|
|
|
<ListItem key={index}>
|
2024-06-30 18:55:39 -04:00
|
|
|
<ListItemContent>
|
2024-07-16 19:37:18 -04:00
|
|
|
<Typography level='body-xs' sx={{ fontWeight: 'md' }}>
|
2024-06-30 18:55:39 -04:00
|
|
|
{info.text}
|
|
|
|
</Typography>
|
2024-07-16 19:37:18 -04:00
|
|
|
<Chip color='primary' size='md' startDecorator={info.icon}>
|
|
|
|
{info.subtext ? info.subtext : '--'}
|
|
|
|
</Chip>
|
2024-06-30 18:55:39 -04:00
|
|
|
</ListItemContent>
|
|
|
|
</ListItem>
|
2024-07-16 19:37:18 -04:00
|
|
|
</Grid>
|
|
|
|
))}
|
|
|
|
</Grid>
|
|
|
|
</Sheet>
|
|
|
|
|
2024-06-30 18:55:39 -04:00
|
|
|
{/* User History Cards */}
|
2024-07-16 19:37:18 -04:00
|
|
|
<Typography level='title-md' my={1.5}>
|
2024-06-30 18:55:39 -04:00
|
|
|
History:
|
|
|
|
</Typography>
|
2024-07-16 19:37:18 -04:00
|
|
|
<Sheet sx={{ borderRadius: 'sm', p: 2, boxShadow: 'md' }}>
|
2024-06-30 18:55:39 -04:00
|
|
|
{/* Chore History List (Updated Style) */}
|
|
|
|
|
|
|
|
<List sx={{ p: 0 }}>
|
2024-07-09 17:39:16 -04:00
|
|
|
{choreHistory.map((historyEntry, index) => (
|
|
|
|
<HistoryCard
|
|
|
|
historyEntry={historyEntry}
|
|
|
|
performers={performers}
|
|
|
|
allHistory={choreHistory}
|
|
|
|
key={index}
|
|
|
|
index={index}
|
|
|
|
/>
|
2024-06-30 18:55:39 -04:00
|
|
|
))}
|
|
|
|
</List>
|
2024-07-16 19:37:18 -04:00
|
|
|
</Sheet>
|
2024-06-30 18:55:39 -04:00
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ChoreHistory
|