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

Jo qg sort shopping list #30

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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);
}
Copy link
Collaborator

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.

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) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 (
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