Skip to content
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

Sort shopping list by urgency #30

Merged
merged 7 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,24 @@ export async function deleteItem() {
* this function must accept!
*/
}

export function comparePurchaseUrgency(arr) {
const soonArray = arr
.filter((item) => item.indicator === 'Soon')
.sort((a, b) => (a.name > b.name ? 1 : -1));
const kindOfSoon = arr
.filter((item) => item.indicator === 'Kind of soon')
.sort((a, b) => (a.name > b.name ? 1 : -1));
const notSoon = arr
.filter((item) => item.indicator === 'Not soon')
.sort((a, b) => (a.name > b.name ? 1 : -1));
const inactive = arr
.filter((item) => item.indicator === 'Inactive')
.sort((a, b) => (a.name > b.name ? 1 : -1));
const overdue = arr
.filter((item) => item.indicator === 'Overdue')
.sort((a, b) => (a.name > b.name ? 1 : -1));
return overdue.length > 0
? overdue.concat(soonArray, kindOfSoon, notSoon, inactive)
: soonArray.concat(kindOfSoon, notSoon, inactive);
}
Hudamabkhoot marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function ListItem({
totalPurchases,
dayInterval,
dateCreated,
indicator,
}) {
const handleOnChange = async (event) => {
let { checked } = event.target;
Expand Down Expand Up @@ -51,6 +52,8 @@ export function ListItem({
disabled={isChecked}
/>
<label htmlFor={`${id}`}>{name}</label>
{/* Add CSS to dynamically change bg-color for badges? */}
<p className="TimeBadge">{indicator}</p>
</li>
);
}
11 changes: 11 additions & 0 deletions src/components/SingleList.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@
.SingleList-label {
margin-left: 0.2em;
}

.ListItem {
display: flex;
flex-direction: row;
justify-content: flex-start;
}

.TimeBadge {
margin: 8px;
font-size: small;
}
37 changes: 36 additions & 1 deletion src/views/List.jsx
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) => {
Hudamabkhoot marked this conversation as resolved.
Show resolved Hide resolved
const dateLastPurchasedJavaScriptObject = item.dateLastPurchased
? item.dateLastPurchased.toDate()
: item.dateCreated.toDate();

const daysSinceLastPurchase = getDaysBetweenDates(
new Date(),
dateLastPurchasedJavaScriptObject,
);
Hudamabkhoot marked this conversation as resolved.
Show resolved Hide resolved

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 (
Expand All @@ -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>
Expand Down
Loading