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

Sm ar items in order issue 13 #30

Merged
merged 11 commits into from
Sep 23, 2024
53 changes: 53 additions & 0 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,56 @@ export async function deleteItem() {
* this function must accept!
*/
}

export function comparePurchaseUrgency(list) {
const currentDate = new Date();
const soon = [];
const kindOfSoon = [];
const notSoon = [];
const inactive = [];
const overdue = [];

const sortedList = list.sort((a, b) => {
const dateNextPurchasedAsDateA = a.dateNextPurchased?.toDate();
const dateNextPurchasedAsDateB = b.dateNextPurchased?.toDate();

const daysUntilNextPurchaseA = getDaysBetweenDates(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how getDaysBetwenDates gets used again here!

currentDate,
dateNextPurchasedAsDateA,
);
const daysUntilNextPurchaseB = getDaysBetweenDates(
currentDate,
dateNextPurchasedAsDateB,
);

return daysUntilNextPurchaseB > daysUntilNextPurchaseA ? -1 : 1;
sar-mko marked this conversation as resolved.
Show resolved Hide resolved
});

sortedList.forEach((x) => {
const dateNextPurchasedAsDate = x.dateNextPurchased?.toDate();
sar-mko marked this conversation as resolved.
Show resolved Hide resolved

const daysUntilNextPurchase = getDaysBetweenDates(
currentDate,
dateNextPurchasedAsDate,
);
if (daysUntilNextPurchase < 0) {
overdue.push(x);
} else if (daysUntilNextPurchase >= 0 && daysUntilNextPurchase <= 7) {
soon.push(x);
} else if (daysUntilNextPurchase > 7 && daysUntilNextPurchase < 30) {
kindOfSoon.push(x);
} else if (daysUntilNextPurchase >= 30 && daysUntilNextPurchase < 60) {
notSoon.push(x);
} else if (daysUntilNextPurchase >= 60) {
inactive.push(x);
}
});

return {
overdue,
soon,
kindOfSoon,
notSoon,
inactive,
};
}
45 changes: 34 additions & 11 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ListItem } from '../components';
import { useState, useEffect } from 'react';
import BasicModal from './Modal';
import { comparePurchaseUrgency } from '../api';

export function List({ data, userId }) {
const [filterVal, setFilterVal] = useState('');
const [filteredList, setFilteredList] = useState([]);
const [filteredObject, setFilteredObject] = useState({});
const [sortedList, setSortedList] = useState([]);
const [showModal, setShowModal] = useState(false);

const dataEmpty = userId && !data.length;
Expand All @@ -28,12 +30,26 @@ export function List({ data, userId }) {
};

useEffect(() => {
setFilteredList(
data.filter((item) =>
setSortedList(comparePurchaseUrgency(data));
}, [data]);

const labels = {
overdue: 'Overdue (!!!!)',
soon: 'Soon (!!!)',
kindOfSoon: 'Kind of soon (!!)',
notSoon: 'Not soon (!)',
inactive: 'Inactive Items',
};

useEffect(() => {
const filteredObject = {};
Object.entries(sortedList).forEach(([timeBucket, list]) => {
filteredObject[timeBucket] = list.filter((item) =>
item.name.toLowerCase().includes(filterVal.toLowerCase()),
),
);
}, [filterVal, data]);
);
});
setFilteredObject(filteredObject);
}, [filterVal, sortedList]);

return (
<>
Expand All @@ -45,7 +61,7 @@ export function List({ data, userId }) {
)}

<form onSubmit={clearInput}>
<label htmlFor="item-name"> Item name:</label>
<label htmlFor="item-name">Item name:</label>
<input
id="item-name"
name="item-name"
Expand All @@ -57,10 +73,17 @@ export function List({ data, userId }) {
</form>

<ul>
{filteredList &&
filteredList.map((item) => {
return <ListItem key={item.id} item={item} />;
})}
{filteredObject &&
Object.entries(filteredObject).map(([timeBucket, list]) => (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the implementation of using Object.entries here. I definitely learned a bit about data type conversion by looking at this.

<>
<div>
<h3>{labels[timeBucket]}</h3>
</div>
{list.map((item) => (
<ListItem key={item.id} item={item} />
))}
</>
))}
</ul>
</>
);
Expand Down
Loading