diff --git a/src/views/Chores/MyChores.jsx b/src/views/Chores/MyChores.jsx index 72cd809..6d4acb7 100644 --- a/src/views/Chores/MyChores.jsx +++ b/src/views/Chores/MyChores.jsx @@ -2,15 +2,20 @@ import { Add, CancelRounded, EditCalendar, + ExpandCircleDown, FilterAlt, PriorityHigh, Style, } from '@mui/icons-material' import { + Accordion, + AccordionDetails, + AccordionGroup, Box, Button, Chip, Container, + Divider, IconButton, Input, List, @@ -38,6 +43,8 @@ const MyChores = () => { const [chores, setChores] = useState([]) const [filteredChores, setFilteredChores] = useState([]) const [selectedFilter, setSelectedFilter] = useState('All') + const [choreSections, setChoreSections] = useState([]) + const [openChoreSections, setOpenChoreSections] = useState({}) const [searchTerm, setSearchTerm] = useState('') const [activeUserId, setActiveUserId] = useState(0) const [performers, setPerformers] = useState([]) @@ -74,6 +81,108 @@ const MyChores = () => { return aDueDate - bDueDate // Sort ascending by due date } + const sectionSorter = (t, chores) => { + // sort by priority then due date: + chores.sort((a, b) => { + // no priority is lowest priority: + if (a.priority === 0) { + return 1 + } + if (a.priority !== b.priority) { + return a.priority - b.priority + } + if (a.nextDueDate === null) { + return 1 + } + if (b.nextDueDate === null) { + return -1 + } + return new Date(a.nextDueDate) - new Date(b.nextDueDate) + }) + + var groups = [] + switch (t) { + case 'due_date': + var groupRaw = { + Today: [], + 'In a week': [], + 'This month': [], + Later: [], + Overdue: [], + Anytime: [], + } + chores.forEach(chore => { + if (chore.nextDueDate === null) { + groupRaw['Anytime'].push(chore) + } else if (new Date(chore.nextDueDate) < new Date()) { + groupRaw['Overdue'].push(chore) + } else if ( + new Date(chore.nextDueDate).toDateString() === + new Date().toDateString() + ) { + groupRaw['Today'].push(chore) + } else if ( + new Date(chore.nextDueDate) < + new Date(Date.now() + 7 * 24 * 60 * 60 * 1000) && + new Date(chore.nextDueDate) > new Date() + ) { + groupRaw['In a week'].push(chore) + } else if ( + new Date(chore.nextDueDate).getMonth() === new Date().getMonth() + ) { + groupRaw['This month'].push(chore) + } else { + groupRaw['Later'].push(chore) + } + }) + groups = [ + { name: 'Overdue', content: groupRaw['Overdue'] }, + { name: 'Today', content: groupRaw['Today'] }, + { name: 'In a week', content: groupRaw['In a week'] }, + { name: 'This month', content: groupRaw['This month'] }, + { name: 'Later', content: groupRaw['Later'] }, + { name: 'Anytime', content: groupRaw['Anytime'] }, + ] + break + case 'priority': + groupRaw = { + p1: [], + p2: [], + p3: [], + p4: [], + no_priority: [], + } + chores.forEach(chore => { + switch (chore.priority) { + case 1: + groupRaw['p1'].push(chore) + break + case 2: + groupRaw['p2'].push(chore) + break + case 3: + groupRaw['p3'].push(chore) + break + case 4: + groupRaw['p4'].push(chore) + break + } + }) + break + case 'labels': + groupRaw = {} + chores.forEach(chore => { + chore.labelsV2.forEach(label => { + if (groupRaw[label.id] === undefined) { + groupRaw[label.id] = [] + } + groupRaw[label.id].push(chore) + }) + }) + } + return groups + } + useEffect(() => { if (userProfile === null) { GetUserProfile() @@ -100,6 +209,14 @@ const MyChores = () => { const sortedChores = choresData.res.sort(choreSorter) setChores(sortedChores) setFilteredChores(sortedChores) + const sections = sectionSorter('due_date', sortedChores) + setChoreSections(sections) + setOpenChoreSections( + Object.keys(sections).reduce((acc, key) => { + acc[key] = true + return acc + }, {}), + ) } }, [choresData, choresLoading]) @@ -231,11 +348,6 @@ const MyChores = () => { return ( - {/* - My Chores - */} - {/* */} - {/* Search box to filter */} { mt: 1, mb: 1, borderRadius: 24, - // border: '1px solid', height: 24, borderColor: 'text.disabled', padding: 1, @@ -271,7 +382,6 @@ const MyChores = () => { } /> } options={Priorities} selectedItem={selectedFilter} @@ -282,7 +392,6 @@ const MyChores = () => { isActive={selectedFilter.startsWith('Priority: ')} /> } // TODO : this need simplification we want to display both user labels and chore labels // that why we are merging them here. @@ -395,7 +504,6 @@ const MyChores = () => { Current Filter: {selectedFilter} )} - {/* */} {filteredChores.length === 0 && ( { {chores.length > 0 && ( <>