-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutils.ts
129 lines (125 loc) · 3.24 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import data from "./pages/api/data.json";
import {
IAllProducts,
ICartProduct,
ICategory,
INotFound,
IProduct,
IProducutWithCategory,
ITopCategories,
} from "./types";
export function getData() {
const { top_categories, top_products } = data;
return { top_categories, top_products };
}
const notFound = { message: "Not Found" };
export function getProduct(
categoryId: number,
productId: number
): IProducutWithCategory | INotFound {
if (!categoryId && !productId) {
return notFound;
}
const foundCategory = data.all_products.find(
(prod) => prod.category_id === Number(categoryId)
);
if (!foundCategory) {
return notFound;
}
if (Array.isArray(foundCategory.products)) {
const foundProduct = (foundCategory.products as Array<IProduct>).find(
(el) => el.id === Number(productId)
);
if (!foundProduct) {
return notFound;
}
return {
category_id: foundCategory.category_id,
category_name: foundCategory.category_name,
product: foundProduct,
};
}
return notFound;
}
interface StaticPropsReturn {
params: {
categoryId: string;
productId: string;
};
}
export function getTopProductsIds(): StaticPropsReturn[] {
// Get the top products and make static files for them
const result: StaticPropsReturn[] = [];
data.top_products.forEach((topProduct) => {
topProduct.products.forEach((product) => {
if (product) {
result.push({
params: {
categoryId: topProduct.category_id.toString(),
productId: product.id.toString(),
},
});
}
// else {
// console.log(
// "🚀 ~ file: utils.ts ~ line 54 ~ topProduct.products.forEach ~ product",
// product,
// );
// }
});
});
return result;
}
export function getCategory(category_id: string): IAllProducts | undefined {
const foundCategory = data.top_products.find(
(product) => product.category_id === Number(category_id)
);
if (!foundCategory) {
return undefined;
}
return foundCategory;
}
export function getCategoryList(): ITopCategories[] {
return data.top_categories;
}
export function getPercentageDecreased(
originalPrice: number,
basePrice: number
) {
return Math.round(((originalPrice - basePrice) / originalPrice) * 100);
}
/**
* Find the products for a give query
*/
export function findProducts(query: string): IProduct[] {
if (!query.trim().length) {
return [];
}
// Loop over all the products of all categories and check if
// their title contains the searched query
const foundProducts: IProduct[] = [];
data.all_products.forEach((category) => {
category.products.forEach((product: IProduct) => {
if (product.name.toLowerCase().includes(query.toLowerCase())) {
foundProducts.push(product);
}
});
});
return foundProducts;
}
const breakpoints = {
sm: "640px",
md: "768px",
lg: "1024px",
xl: "1280px",
"2xl": "1536px",
};
export const mediaQueries = (key: keyof typeof breakpoints) => {
return (style: TemplateStringsArray | String) =>
`@media (min-width: ${breakpoints[key]}) { ${style} }`;
};
export const getTotalItems = (cart: ICartProduct[]) => {
return cart.reduce((acc, curr) => {
return acc + curr.count;
}, 0);
};