-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathproducts.ts
More file actions
67 lines (52 loc) · 1.52 KB
/
products.ts
File metadata and controls
67 lines (52 loc) · 1.52 KB
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
import {
CatalogProducts,
CategoriesType,
PathType,
Product,
} from '../types/Types';
import { getData } from './fetchClient';
export const getProducts = () => {
return getData<CatalogProducts[]>(`${PathType.PRODUCTS}.json`);
};
export const getPhones = async () => {
const products = await getProducts();
return products.filter(product => product.category === CategoriesType.PHONES);
};
export const getTablets = async () => {
const products = await getProducts();
return products.filter(
product => product.category === CategoriesType.TABLETS,
);
};
export const getAccessories = async () => {
const products = await getProducts();
return products.filter(
product => product.category === CategoriesType.ACCESSORIES,
);
};
export const getProductById = async (category: string, itemId: string) => {
let path = '';
switch (category) {
case CategoriesType.PHONES:
path = PathType.PHONES;
break;
case CategoriesType.TABLETS:
path = PathType.TABLETS;
break;
case CategoriesType.ACCESSORIES:
path = PathType.ACCESSORIES;
break;
default:
throw new Error('Unknown category');
}
const products = await getData<Product[]>(`${path}.json`);
const product = products.find(item => item.id === itemId);
if (!product) {
throw new Error('Product not found');
}
return product;
};
export const getSuggestedProducts = async () => {
const products = await getProducts();
return [...products].sort(() => Math.random() - 0.5).slice(0, 12);
};