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

Conversation

joriordan332
Copy link
Collaborator

Description

Code implements a sort function to sort our shopping list items by urgency and alphabetical order. Overdue items are pushed to the top of the list and inactive items are pushed to the bottom.

Related Issue

Closes #13

Acceptance Criteria

  • Items in the list are shown with an indicator that tells the user they should buy the item “soon”, “kind of soon”, or “not soon”; or that the item is “inactive”
    • This urgency indicator does not rely on only color
  • api/firestore.js exports a new comparePurchaseUrgency function with the following behaviors
    • sorts inactive items last, then
    • sorts items in ascending order of days until purchase, and
    • sorts items with the same days until purchase alphabetically

Type of Changes

Feature

Updates

Before

After

image

image

Testing Steps / QA Criteria

  • Do a git pull and git checkout jo-qg-sort-shopping-list
  • Open the homepage by running npm start
  • Navigate to the list page
  • Each item on your list should have an indicator next to it and be ordered as followed-overdue, soon, kind of soon, not soon & inactive
  • For each urgency category the items will be alphabetised
  • Adding a new item to the list should automatically sort the items

Copy link

github-actions bot commented Sep 18, 2024

Visit the preview URL for this PR (updated for commit f749273):

https://tcl-79-smart-shopping-list--pr30-jo-qg-sort-shopping-vj0pf99x.web.app

(expires Thu, 26 Sep 2024 09:58:27 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: d91d9ddbda780208241c52942f544acf8e81407a

Copy link
Collaborator

@Hudamabkhoot Hudamabkhoot left a comment

Choose a reason for hiding this comment

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

Great job with this tough issue impressive work! 👏. I left suggestions as comments i believe addressing them would enhance performance and improve clarity.

I also noticed that the indicators disappear when filtering with the search bar and they don’t reappear after clearing the filter since data goes back to before the indicators are added.

Comment on lines 243 to 262
export function comparePurchaseUrgency(arr) {
const soonArray = arr
.filter((item) => item.indicator === 'Soon')
.sort((a, b) => (a.name > b.name ? 1 : -1));
const kindOfSoon = arr
.filter((item) => item.indicator === 'Kind of soon')
.sort((a, b) => (a.name > b.name ? 1 : -1));
const notSoon = arr
.filter((item) => item.indicator === 'Not soon')
.sort((a, b) => (a.name > b.name ? 1 : -1));
const inactive = arr
.filter((item) => item.indicator === 'Inactive')
.sort((a, b) => (a.name > b.name ? 1 : -1));
const overdue = arr
.filter((item) => item.indicator === 'Overdue')
.sort((a, b) => (a.name > b.name ? 1 : -1));
return overdue.length > 0
? overdue.concat(soonArray, kindOfSoon, notSoon, inactive)
: soonArray.concat(kindOfSoon, notSoon, inactive);
}
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 grouping the items by their indicators in a single loop by using an object to hold the arrays for each indicator, then sort and concatenate them after that could really enhance both performance and readability.


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

useEffect(() => {
setDisplayData([...data]);
const getIndicator = (item) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

moving getIndicator to utils would improve the organization.

Comment on lines 14 to 21
const dateLastPurchasedJavaScriptObject = item.dateLastPurchased
? item.dateLastPurchased.toDate()
: item.dateCreated.toDate();

const daysSinceLastPurchase = getDaysBetweenDates(
new Date(),
dateLastPurchasedJavaScriptObject,
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we used the daysSinceLastPurchase logic twice now, it could be useful to create a function for it in the utils to use in both getIndicator and updateItem.

Comment on lines +21 to +22
} else if (daysSinceLastPurchase > 30 && daysSinceLastPurchase < 60) {
return 'Overdue';
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.

Comment on lines +18 to +19
setAllData([...comparePurchaseUrgency(arrayWithIndicator)]);
setDisplayData([...comparePurchaseUrgency(arrayWithIndicator)]);
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants