-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrated the current cart data from firestore with the top items se…
…ction of the statistics page
- Loading branch information
Showing
5 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ExpectedTopItemFormat } from "../../../components/statistics/TopItems"; | ||
|
||
type ItemType = { | ||
isCompleted: boolean; | ||
itemId: string; | ||
quantity: number; | ||
}; | ||
|
||
type ExpectedDataType = { categoryName: string; items: ItemType[] }[]; | ||
|
||
export const generateTopItems = ( | ||
items: ExpectedDataType, | ||
totalQuantity: number | ||
) => { | ||
// console.log("Items: ", items); | ||
// create a variable to store all the items | ||
let allItems: ItemType[] = [{ itemId: "", isCompleted: false, quantity: 0 }]; | ||
|
||
// create an array of all the items in each category | ||
items.forEach((category) => { | ||
// allItems.concat(category.items); | ||
const tmpItems = [...allItems, ...category.items]; | ||
allItems = tmpItems; | ||
}); | ||
|
||
// sort the extracted items in descending order | ||
|
||
allItems.sort((currentItem, nextItem) => { | ||
return nextItem.quantity - currentItem.quantity; | ||
}); | ||
|
||
// take the highest 3 items and convert them to the expected top items type | ||
const topItemsWithPercentages: ExpectedTopItemFormat = allItems | ||
.slice(0, 3) | ||
.map((item) => { | ||
return { | ||
name: item.itemId, | ||
percentage: (item.quantity / totalQuantity) * 100, | ||
}; | ||
}); | ||
// return the highest 3 items | ||
return topItemsWithPercentages; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters