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 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
41 changes: 32 additions & 9 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
onSnapshot,
updateDoc,
addDoc,
Timestamp,
} from 'firebase/firestore';
import { useEffect, useState } from 'react';
import { db } from './config';
import { getFutureDate } from '../utils';
import toast from 'react-hot-toast';
import { calculateEstimate } from '@the-collab-lab/shopping-list-utils';
import { getDaysBetweenDates } from '../utils/dates';
import { getDaysSinceLastPurchase } from '../utils/helpers';
/**
* A custom hook that subscribes to the user's shopping lists in our Firestore
* database and returns new data whenever the lists change.
Expand Down Expand Up @@ -199,14 +198,8 @@ export async function updateItem(listPath, checked, itemData) {
const today = new Date();
const currentTotalPurchases = itemData.totalPurchases;
const currentDayInterval = itemData.dayInterval;
const dateLastPurchasedJavaScriptObject = itemData.dateLastPurchased
? itemData.dateLastPurchased.toDate()
: itemData.dateCreated.toDate();

const daysSinceLastPurchase = getDaysBetweenDates(
today,
dateLastPurchasedJavaScriptObject,
);
const daysSinceLastPurchase = getDaysSinceLastPurchase(itemData);
const estimate = calculateEstimate(
currentDayInterval,
daysSinceLastPurchase,
Expand Down Expand Up @@ -239,3 +232,33 @@ export async function deleteItem() {
* this function must accept!
*/
}

export function comparePurchaseUrgency(arr) {
const groupedItems = {
Overdue: [],
Soon: [],
'Kind of soon': [],
'Not soon': [],
Inactive: [],
};
arr.forEach((item) => {
if (groupedItems[item.indicator]) {
groupedItems[item.indicator].push(item);
}
});
Object.keys(groupedItems).forEach((key) => {
groupedItems[key].sort((a, b) => (a.name > b.name ? 1 : -1));
});
return groupedItems['Overdue'].length > 0
? groupedItems['Overdue'].concat(
groupedItems['Soon'],
groupedItems['Kind of soon'],
groupedItems['Not soon'],
groupedItems['Inactive'],
)
: groupedItems['Soon'].concat(
groupedItems['Kind of soon'],
groupedItems['Not soon'],
groupedItems['Inactive'],
);
}
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>
);
}
10 changes: 5 additions & 5 deletions src/components/SearchBar.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export function SearchBar({ data, setDisplayData, setSearch, search }) {
export function SearchBar({ allData, setDisplayData, setSearch, search }) {
const handleInputChange = (e) => {
const searchTerm = e.target.value;
setSearch(searchTerm);

const filteredUsers = data.filter((item) =>
const filteredData = allData.filter((item) =>
item.name.toLowerCase().includes(searchTerm.trim().toLowerCase()),
);
setDisplayData(filteredUsers);
};

setDisplayData(filteredData);
};
const handleClear = () => {
setDisplayData(data);
setDisplayData(allData);
setSearch('');
};

Expand Down
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;
}
30 changes: 30 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getDaysBetweenDates } from './dates';

export function getDaysSinceLastPurchase(item) {
const dateLastPurchasedJavaScriptObject = item.dateLastPurchased
? item.dateLastPurchased.toDate()
: item.dateCreated.toDate();

return getDaysBetweenDates(new Date(), dateLastPurchasedJavaScriptObject);
}

export function getIndicator(item) {
const daysSinceLastPurchase = getDaysSinceLastPurchase(item);

const daysUntilNextPurchase = getDaysBetweenDates(
item.dateNextPurchased.toDate(),
new Date(),
);

if (daysSinceLastPurchase > 60) {
return 'Inactive';
} else if (daysSinceLastPurchase > 30 && daysSinceLastPurchase < 60) {
return 'Overdue';
Comment on lines +21 to +22
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 when dateNextPurchasedhas passed is how an item becomes Overdue.

} else if (daysUntilNextPurchase <= 7) {
return 'Soon';
} else if (daysUntilNextPurchase > 7 && daysUntilNextPurchase < 30) {
return 'Kind of soon';
} else if (daysUntilNextPurchase >= 30) {
return 'Not soon';
}
}
14 changes: 12 additions & 2 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { useEffect, useState } from 'react';
import { ListItem, SearchBar } from '../components';
import { useNavigate } from 'react-router-dom';
import { comparePurchaseUrgency } from '../api/firebase';
import { getIndicator } from '../utils/helpers';

export function List({ data, listPath }) {
const [search, setSearch] = useState('');
const [allData, setAllData] = useState([]);
const [displayData, setDisplayData] = useState([]);
const navigate = useNavigate();

useEffect(() => {
setDisplayData([...data]);
const arrayWithIndicator = data.map((item) => ({
...item,
indicator: getIndicator(item),
}));
setAllData([...comparePurchaseUrgency(arrayWithIndicator)]);
setDisplayData([...comparePurchaseUrgency(arrayWithIndicator)]);
Comment on lines +18 to +19
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think its best to use a variable to be stored in the states instead of calling comparePurchaseUrgency twice.

}, [data]);

return (
<>
<SearchBar
setDisplayData={setDisplayData}
data={data}
allData={allData}
setSearch={setSearch}
search={search}
/>
Expand All @@ -31,6 +39,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