|
| 1 | +import { FileType, SnippetType } from "../types"; |
1 | 2 | import slugify from "../utils/slugify"; |
2 | 3 |
|
3 | | -export const fetchLanguages = async ({ params }: any) => { |
4 | | - try { |
5 | | - const res = await fetch(`/data/${params.language}.json`); |
6 | | - if (!res.ok) { |
7 | | - throw new Error("Failed to fetch languages"); |
8 | | - } |
9 | | - |
10 | | - console.log(`/data/${params.language}.json`); |
11 | | - console.log("data fetch"); |
12 | | - |
13 | | - const data = await res.json(); |
14 | | - console.log(data); |
15 | | - |
16 | | - return data; |
17 | | - } catch (error) { |
18 | | - return new Error(`Error occured: ${error}`); |
19 | | - } |
20 | | -}; |
21 | | - |
22 | 4 | export const fetchCategories = async (params: any) => { |
23 | 5 | try { |
24 | 6 | const res = await fetch(`/data/${params.language}.json`); |
25 | | - const data = await res.json(); |
26 | | - |
27 | | - const newData = data.map((category: any) => category.categoryName); |
| 7 | + const data: FileType = await res.json(); |
28 | 8 |
|
| 9 | + const newData = data.map((category) => category.categoryName); |
29 | 10 | console.log(newData); |
| 11 | + |
30 | 12 | return newData; |
31 | 13 | } catch (error) { |
32 | | - return new Error(`Error occured: ${error}`); |
| 14 | + console.error("Error occured"); |
| 15 | + return []; |
33 | 16 | } |
34 | 17 | }; |
35 | 18 |
|
36 | 19 | export const fetchSnippets = async (params: any) => { |
37 | 20 | try { |
38 | 21 | const res = await fetch(`/data/${params.language}.json`); |
39 | | - const data = await res.json(); |
| 22 | + const data: FileType = await res.json(); |
40 | 23 |
|
41 | 24 | const categoryData = data.find( |
42 | | - (cat: any) => slugify(cat.categoryName) === slugify(params.category) |
| 25 | + (cat) => slugify(cat.categoryName) === slugify(params.category) |
43 | 26 | ); |
44 | 27 | return categoryData?.snippets.find( |
45 | | - (snip: any) => slugify(snip.title) === params.snippet |
| 28 | + (snip) => slugify(snip.title) === params.snippet |
46 | 29 | ); |
47 | 30 | } catch (error) { |
48 | 31 | console.error("Error occured: ", error); |
| 32 | + return {}; |
49 | 33 | } |
50 | 34 | }; |
51 | 35 |
|
|
0 commit comments