-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
9a972d9
f68f23c
0ed0a0d
b36e20d
4cc0191
ff64cbc
0c814b6
9dff0c9
7d314d1
0d550c9
547cad0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
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((item) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the approach you guys took here. It's interesting to me how you created empty array's for each Urgency and then in the if else statement are just pushing values dependent on what is selected by the user. |
||
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, | ||
}; | ||
} |
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; | ||
|
@@ -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 ( | ||
<> | ||
|
@@ -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" | ||
|
@@ -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]) => ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
</> | ||
); | ||
|
There was a problem hiding this comment.
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!