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

Display items in categories #31

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 49 additions & 0 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,52 @@ export async function deleteItem() {
* this function must accept!
*/
}

export function comparePurchaseUrgency(data) {
const dayItemIsInactive = -60;
const dayOfExpectedPurchase = 0;
const dayItemIsBuySoon = 7;
const dayItemIsBuySoonish = 14;

const now = new Date();
const newData = data.map((item) => {
const daysBeforePurchase = getDaysBetweenDates(
item.dateNextPurchased.toDate(),
now,
);

item.daysBeforePurchase = daysBeforePurchase;

if (daysBeforePurchase < dayItemIsInactive) {
item.category = 'Inactive';
} else if (
daysBeforePurchase >= dayItemIsInactive &&
daysBeforePurchase < dayOfExpectedPurchase
) {
item.category = 'Overdue';
} else if (
daysBeforePurchase >= dayOfExpectedPurchase &&
daysBeforePurchase <= dayItemIsBuySoon
) {
item.category = 'Buy Soon';
} else if (
daysBeforePurchase > dayItemIsBuySoon &&
daysBeforePurchase <= dayItemIsBuySoonish
) {
item.category = 'Buy Soonish';
} else {
item.category = 'Buy Not Soon';
}

return item;
});

const dataSortedByDaysLeft = newData.sort((itemA, itemB) => {
if (itemA.daysBeforePurchase === itemB.daysBeforePurchase) {
return itemA.name.localeCompare(itemB.name);
}

return itemA.daysBeforePurchase - itemB.daysBeforePurchase;
});
return dataSortedByDaysLeft;
}
48 changes: 48 additions & 0 deletions src/components/ContainerItems.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useEffect, useState } from 'react';
import { ListItem } from './ListItem';
import { Fragment } from 'react';

export const ContainerItems = ({
category,
newList,
wasRecentlyPurchased,
listPath,
updatePurchaseDate,
}) => {
const [filteredItemsList, setFilteredItemsList] = useState([]);

useEffect(() => {
const newItemList = newList.filter((item) => {
if (item.category === category) {
return true;
} else return false;
});
setFilteredItemsList(newItemList);
}, [newList, category]);

return filteredItemsList[0] ? (
<section>
<h2>{category}</h2>
<ul>
{filteredItemsList.map((item, i) => {
if (item.category === category) {
return (
<ListItem
key={item.id}
dateLastPurchased={item.dateLastPurchased}
isRecentlyPurchased={wasRecentlyPurchased(item)}
itemId={item.id}
listPath={listPath}
name={item.name}
purchaseDate={item.dateLastPurchased}
updatePurchaseDate={updatePurchaseDate}
/>
);
} else return <Fragment key={i} />;
})}
</ul>
</section>
) : (
<> </>
);
};
4 changes: 2 additions & 2 deletions src/components/SearchList.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react';

const SearchList = ({ data, setNewList }) => {
export const SearchList = ({ data, setNewList }) => {
const [value, setValue] = useState('');

const handleFiltering = (e) => {
Expand Down Expand Up @@ -37,4 +37,4 @@ const SearchList = ({ data, setNewList }) => {
);
};

export default SearchList;
// export default SearchList;
2 changes: 2 additions & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './ListItem';
export * from './SingleList';
export * from './ContainerItems';
export * from './SearchList';
45 changes: 26 additions & 19 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { useState, useEffect } from 'react';
import { ListItem } from '../components';
import SearchList from '../components/SearchList';
import { ContainerItems } from '../components';
import { SearchList } from '../components';
import { useParams, useNavigate } from 'react-router-dom';
import { updateItem } from '../api/firebase';
import { updateItem, comparePurchaseUrgency } from '../api/firebase';
import { isMoreThanADayAgo } from '../utils';
import './List.css';
import addFirstItem from '../pictures/addFirstItem.png';

export function List({ data, lists, listPath }) {
const [newList, setNewList] = useState([]);
const [sortedList, setSortedList] = useState([]);
const { path } = useParams();
const navigate = useNavigate();
const categoryArray = [
'Overdue',
'Buy Soon',
'Buy Soonish',
'Buy Not Soon',
'Inactive',
];

useEffect(() => {
setNewList(data);
const getDataSorted = comparePurchaseUrgency(data);
setNewList(getDataSorted);
setSortedList(getDataSorted);
}, [data]);

const wasRecentlyPurchased = (item) => {
Expand Down Expand Up @@ -64,23 +74,20 @@ export function List({ data, lists, listPath }) {
</button>
</div>
)}

{data.length > 0 && (
<div>
<SearchList data={data} setNewList={setNewList} />
<ul>
{newList.map((item) => (
<ListItem
dateLastPurchased={item.dateLastPurchased}
isRecentlyPurchased={wasRecentlyPurchased(item)}
itemId={item.id}
key={item.id}
listPath={listPath}
name={item.name}
purchaseDate={item.dateLastPurchased}
updatePurchaseDate={updatePurchaseDate}
/>
))}
</ul>
<SearchList data={sortedList} setNewList={setNewList} />
{categoryArray.map((category, i) => (
<ContainerItems
key={i}
category={category}
newList={newList}
wasRecentlyPurchased={wasRecentlyPurchased}
listPath={listPath}
updatePurchaseDate={updatePurchaseDate}
/>
))}
</div>
)}
</>
Expand Down
Loading