Skip to content

Commit

Permalink
Merge pull request #30 from the-collab-lab/sm-ar-items-in-order-issue-13
Browse files Browse the repository at this point in the history
Sm ar items in order issue 13
  • Loading branch information
arandel1 authored Sep 23, 2024
2 parents dd5ebac + 547cad0 commit c46af99
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
53 changes: 53 additions & 0 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,56 @@ export async function deleteItem(listPath, item) {
console.log(error);
}
}

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(
currentDate,
dateNextPurchasedAsDateA,
);
const daysUntilNextPurchaseB = getDaysBetweenDates(
currentDate,
dateNextPurchasedAsDateB,
);

return daysUntilNextPurchaseB > daysUntilNextPurchaseA ? -1 : 1;
});

sortedList.forEach((item) => {
const dateNextPurchasedAsDate = item.dateNextPurchased?.toDate();

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

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]) => (
<>
<div>
<h3>{labels[timeBucket]}</h3>
</div>
{list.map((item) => (
<ListItem key={item.id} item={item} />
))}
</>
))}
</ul>
</>
);
Expand Down

0 comments on commit c46af99

Please sign in to comment.