Skip to content

Commit

Permalink
Integrated the current cart data from firestore with the top items se…
Browse files Browse the repository at this point in the history
…ction of the statistics page
  • Loading branch information
almamarie committed Jan 21, 2023
1 parent 30b4fa1 commit dc9794a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
4 changes: 3 additions & 1 deletion components/statistics/SingleTopItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const SingleTopItem: React.FC<{ data: ExpectedSingleTopItemFormat }> = (
<li>
<div className={styles.head}>
<span>{props.data.name}</span>
<span className={styles.percentage}>{`${props.data.percentage}%`}</span>
<span className={styles.percentage}>{`${Math.floor(
props.data.percentage
)}%`}</span>
</div>
<div className={styles.bar}>
<div
Expand Down
41 changes: 36 additions & 5 deletions pages/statistics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ import TopItems, {
} from "../../components/statistics/TopItems";
import styles from "./index.module.css";
import { ExpectedChartData } from "../../components/statistics/Graph";
import { GetStaticProps } from "next";
import { GetStaticProps, NextPage } from "next";
import { GET_AJAX } from "../../public/utils/http";
import { generateTopItems } from "../../public/utils/statistics/generate-top-items";
const Graph = dynamic(import("../../components/statistics/Graph"), {
ssr: false,
});

const Home = () => {
type ExpectedData = {
topItemsData: ExpectedTopItemFormat;
topCategoriesData: ExpectedTopCategoryFormat;
monthlySpending: ExpectedChartData;
error: boolean;
};

const Home: NextPage<ExpectedData> = (props) => {
const [isloading, setIsloading] = useState(true);

console.log("Props: ", props);

const topItemsData: ExpectedTopItemFormat = [
{
name: "Banana",
Expand Down Expand Up @@ -73,7 +83,7 @@ const Home = () => {
return (
<React.Fragment>
<section className={styles["top-part"]}>
<TopItems fetchingData={isloading} data={topItemsData} />
<TopItems fetchingData={isloading} data={props.topItemsData} />
<TopCategories fetchingData={isloading} data={topCategoriesData} />
</section>
<section>
Expand All @@ -84,11 +94,32 @@ const Home = () => {
};

export const getStaticProps: GetStaticProps = async () => {
// initialize the error flag
let error = false;

// fetch the current cart data from the backend
const currentCart = await GET_AJAX("/current-cart");
console.log("current cart: ", currentCart.message);
// console.log("current cart: ", currentCart.message);

if (!currentCart.success) {
error = true;
}
// generate the "top items" data
console.log("Current cart: ", currentCart.message);
const topItems = generateTopItems(
currentCart.message.items,
currentCart.message.totalQuantity
);

const props: ExpectedData = {
topItemsData: topItems,
topCategoriesData: [],
monthlySpending: [],
error,
};

return {
props: {},
props,
revalidate: 1,
};
};
Expand Down
43 changes: 43 additions & 0 deletions public/utils/statistics/generate-top-items.ts
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;
};
1 change: 0 additions & 1 deletion public/utils/statistics/group-current-cart.ts

This file was deleted.

8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "src/custom.d.ts"],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"src/custom.d.ts",
"public/utils"
],
"exclude": ["node_modules"]
}

0 comments on commit dc9794a

Please sign in to comment.