diff options
author | Mo Tarbin <mhed.t91@gmail.com> | 2024-07-20 03:40:41 -0400 |
---|---|---|
committer | Mo Tarbin <mhed.t91@gmail.com> | 2024-07-20 03:40:41 -0400 |
commit | 90efb5be001393134aaef07fe11e9ad605692043 (patch) | |
tree | 9cbce35ea79248aaef510f93e8f731e9a0ef6468 /src/views/History | |
parent | aed179d6b3aa9c294893b352a7e61f93076a2ad9 (diff) | |
download | donetick-frontend-90efb5be001393134aaef07fe11e9ad605692043.tar.gz donetick-frontend-90efb5be001393134aaef07fe11e9ad605692043.tar.bz2 donetick-frontend-90efb5be001393134aaef07fe11e9ad605692043.zip |
Support for history Modification and deletion
Diffstat (limited to 'src/views/History')
-rw-r--r-- | src/views/History/ChoreHistory.jsx | 57 | ||||
-rw-r--r-- | src/views/History/HistoryCard.jsx | 18 |
2 files changed, 68 insertions, 7 deletions
diff --git a/src/views/History/ChoreHistory.jsx b/src/views/History/ChoreHistory.jsx index 1db1f06..9419f59 100644 --- a/src/views/History/ChoreHistory.jsx +++ b/src/views/History/ChoreHistory.jsx @@ -15,9 +15,14 @@ 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 { + DeleteChoreHistory, + GetAllCircleMembers, + UpdateChoreHistory, +} from '../../utils/Fetcher' import { Fetch } from '../../utils/TokenManager' import LoadingComponent from '../components/Loading' +import EditHistoryModal from '../Modals/EditHistoryModal' import HistoryCard from './HistoryCard' const ChoreHistory = () => { @@ -28,6 +33,8 @@ const ChoreHistory = () => { const [isLoading, setIsLoading] = useState(true) // Add loading state const { choreId } = useParams() + const [isEditModalOpen, setIsEditModalOpen] = useState(false) + const [editHistory, setEditHistory] = useState({}) useEffect(() => { setIsLoading(true) // Start loading @@ -63,12 +70,12 @@ const ChoreHistory = () => { const averageDelay = histories.reduce((acc, chore) => { - if (chore.dueDate) { + if (chore.dueDate && chore.completedAt) { // Only consider chores with a due date return acc + moment(chore.completedAt).diff(chore.dueDate, 'hours') } return acc - }, 0) / histories.length + }, 0) / histories.filter(chore => chore.dueDate).length const averageDelayMoment = moment.duration(averageDelay, 'hours') const maximumDelay = histories.reduce((acc, chore) => { if (chore.dueDate) { @@ -215,6 +222,10 @@ const ChoreHistory = () => { <List sx={{ p: 0 }}> {choreHistory.map((historyEntry, index) => ( <HistoryCard + onClick={() => { + setIsEditModalOpen(true) + setEditHistory(historyEntry) + }} historyEntry={historyEntry} performers={performers} allHistory={choreHistory} @@ -224,6 +235,46 @@ const ChoreHistory = () => { ))} </List> </Sheet> + <EditHistoryModal + config={{ + isOpen: isEditModalOpen, + onClose: () => { + setIsEditModalOpen(false) + }, + onSave: updated => { + UpdateChoreHistory(choreId, editHistory.id, { + completedAt: updated.completedAt, + dueDate: updated.dueDate, + notes: updated.notes, + }).then(res => { + if (!res.ok) { + console.error('Failed to update chore history:', res) + return + } + + const newRecord = res.json().then(data => { + const newRecord = data.res + const newHistory = choreHistory.map(record => + record.id === newRecord.id ? newRecord : record, + ) + setChoresHistory(newHistory) + setEditHistory(newRecord) + setIsEditModalOpen(false) + }) + }) + }, + onDelete: () => { + DeleteChoreHistory(choreId, editHistory.id).then(() => { + const newHistory = choreHistory.filter( + record => record.id !== editHistory.id, + ) + setChoresHistory(newHistory) + setIsEditModalOpen(false) + }) + }, + }} + historyRecord={editHistory} + /> </Container> ) } diff --git a/src/views/History/HistoryCard.jsx b/src/views/History/HistoryCard.jsx index 5e71f52..34a0b33 100644 --- a/src/views/History/HistoryCard.jsx +++ b/src/views/History/HistoryCard.jsx @@ -11,7 +11,13 @@ import { } from '@mui/joy' import moment from 'moment' -const HistoryCard = ({ allHistory, performers, historyEntry, index }) => { +const HistoryCard = ({ + allHistory, + performers, + historyEntry, + index, + onClick, +}) => { function formatTimeDifference(startDate, endDate) { const diffInMinutes = moment(startDate).diff(endDate, 'minutes') let timeValue = diffInMinutes @@ -64,7 +70,7 @@ const HistoryCard = ({ allHistory, performers, historyEntry, index }) => { icon = <Timelapse /> } else { text = 'No Due Date' - color = 'info' + color = 'neutral' icon = <CalendarViewDay /> } @@ -77,7 +83,7 @@ const HistoryCard = ({ allHistory, performers, historyEntry, index }) => { return ( <> - <ListItem sx={{ gap: 1.5, alignItems: 'flex-start' }}> + <ListItem sx={{ gap: 1.5, alignItems: 'flex-start' }} onClick={onClick}> {' '} {/* Adjusted spacing and alignment */} <ListItemDecorator> @@ -98,7 +104,11 @@ const HistoryCard = ({ allHistory, performers, historyEntry, index }) => { }} > <Typography level='body1' sx={{ fontWeight: 'md' }}> - {moment(historyEntry.completedAt).format('ddd MM/DD/yyyy HH:mm')} + {historyEntry.completedAt + ? moment(historyEntry.completedAt).format( + 'ddd MM/DD/yyyy HH:mm', + ) + : 'Skipped'} </Typography> {getCompletedChip(historyEntry)} </Box> |