-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jo qg sort shopping list #30
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,47 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { ListItem, SearchBar } from '../components'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { comparePurchaseUrgency } from '../api/firebase'; | ||
import { getDaysBetweenDates } from '../utils/dates'; | ||
|
||
export function List({ data, listPath }) { | ||
const [search, setSearch] = useState(''); | ||
const [displayData, setDisplayData] = useState([]); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
setDisplayData([...data]); | ||
const getIndicator = (item) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moving getIndicator to utils would improve the organization. |
||
const dateLastPurchasedJavaScriptObject = item.dateLastPurchased | ||
? item.dateLastPurchased.toDate() | ||
: item.dateCreated.toDate(); | ||
|
||
const daysSinceLastPurchase = getDaysBetweenDates( | ||
new Date(), | ||
dateLastPurchasedJavaScriptObject, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we used the daysSinceLastPurchase logic twice now, it could be useful to create a function for it in the utils to use in both getIndicator and updateItem. |
||
|
||
const daysUntilNextPurchase = getDaysBetweenDates( | ||
item.dateNextPurchased.toDate(), | ||
new Date(), | ||
); | ||
|
||
if (daysSinceLastPurchase > 60) { | ||
return 'Inactive'; | ||
} else if (daysSinceLastPurchase > 30 && daysSinceLastPurchase < 60) { | ||
return 'Overdue'; | ||
} else if (daysUntilNextPurchase <= 7) { | ||
return 'Soon'; | ||
} else if (daysUntilNextPurchase > 7 && daysUntilNextPurchase < 30) { | ||
return 'Kind of soon'; | ||
} else if (daysUntilNextPurchase >= 30) { | ||
return 'Not soon'; | ||
} | ||
}; | ||
const arrayWithIndicator = data.map((item) => ({ | ||
...item, | ||
indicator: getIndicator(item), | ||
})); | ||
setDisplayData([...comparePurchaseUrgency(arrayWithIndicator)]); | ||
}, [data]); | ||
|
||
return ( | ||
|
@@ -31,6 +64,8 @@ export function List({ data, listPath }) { | |
totalPurchases={item.totalPurchases} | ||
dayInterval={item.dayInterval} | ||
dateCreated={item.dateCreated} | ||
dateNextPurchased={item.dateNextPurchased} | ||
indicator={item.indicator} | ||
/> | ||
))} | ||
</ul> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe grouping the items by their indicators in a single loop by using an object to hold the arrays for each indicator, then sort and concatenate them after that could really enhance both performance and readability.