Skip to content

Commit ce540cb

Browse files
Update frontend to handle the changed format of consolidated snippets
1 parent 73907f8 commit ce540cb

File tree

6 files changed

+23
-28
lines changed

6 files changed

+23
-28
lines changed

src/components/LanguageSelector.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const LanguageSelector = () => {
7979
>
8080
<div className="selector__value">
8181
<img src={language.icon} alt="" />
82-
<span>{language.lang || "Select a language"}</span>
82+
<span>{language.name || "Select a language"}</span>
8383
</div>
8484
<span className="selector__arrow" />
8585
</button>
@@ -92,18 +92,18 @@ const LanguageSelector = () => {
9292
>
9393
{fetchedLanguages.map((lang, index) => (
9494
<li
95-
key={lang.lang}
95+
key={lang.name}
9696
role="option"
9797
tabIndex={-1}
9898
onClick={() => handleSelect(lang)}
9999
className={`selector__item ${
100-
language.lang === lang.lang ? "selected" : ""
100+
language.name === lang.name ? "selected" : ""
101101
} ${focusedIndex === index ? "focused" : ""}`}
102-
aria-selected={language.lang === lang.lang}
102+
aria-selected={language.name === lang.name}
103103
>
104104
<label>
105105
<img src={lang.icon} alt="" />
106-
<span>{lang.lang}</span>
106+
<span>{lang.name}</span>
107107
</label>
108108
</li>
109109
))}

src/components/SnippetList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const SnippetList = () => {
6363
whileTap={{ scale: 0.98 }}
6464
>
6565
<div className="snippet__preview">
66-
<img src={language.icon} alt={language.lang} />
66+
<img src={language.icon} alt={language.name} />
6767
</div>
6868
<h3 className="snippet__title">{snippet.title}</h3>
6969
</motion.button>
@@ -77,7 +77,7 @@ const SnippetList = () => {
7777
<SnippetModal
7878
snippet={snippet}
7979
handleCloseModal={handleCloseModal}
80-
language={language.lang}
80+
language={language.name}
8181
/>
8282
)}
8383
</AnimatePresence>

src/contexts/AppContext.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { AppState, LanguageType, SnippetType } from "@types";
44

55
// tokens
66
const defaultLanguage: LanguageType = {
7-
lang: "JAVASCRIPT",
7+
name: "JAVASCRIPT",
88
icon: "/icons/javascript.svg",
9+
subIndexes: [],
910
};
1011

1112
// TODO: add custom loading and error handling

src/hooks/useCategories.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import { useMemo } from "react";
22

33
import { useAppContext } from "@contexts/AppContext";
4-
import { SnippetType } from "@types";
4+
import { CategoryType } from "@types";
55
import { slugify } from "@utils/slugify";
66

77
import { useFetch } from "./useFetch";
88

9-
type CategoryData = {
10-
categoryName: string;
11-
snippets: SnippetType[];
12-
};
13-
149
export const useCategories = () => {
1510
const { language } = useAppContext();
16-
const { data, loading, error } = useFetch<CategoryData[]>(
17-
`/consolidated/${slugify(language.lang)}.json`
11+
const { data, loading, error } = useFetch<CategoryType[]>(
12+
`/consolidated/${slugify(language.name)}.json`
1813
);
1914

2015
const fetchedCategories = useMemo(() => {
21-
return data ? data.map((item) => item.categoryName) : [];
16+
return data ? data.map((item) => item.name) : [];
2217
}, [data]);
2318

2419
return { fetchedCategories, loading, error };

src/hooks/useSnippets.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
import { useAppContext } from "@contexts/AppContext";
2-
import { SnippetType } from "@types";
2+
import { CategoryType } from "@types";
33
import { slugify } from "@utils/slugify";
44

55
import { useFetch } from "./useFetch";
66

7-
type CategoryData = {
8-
categoryName: string;
9-
snippets: SnippetType[];
10-
};
11-
127
export const useSnippets = () => {
138
const { language, category } = useAppContext();
14-
const { data, loading, error } = useFetch<CategoryData[]>(
15-
`/consolidated/${slugify(language.lang)}.json`
9+
const { data, loading, error } = useFetch<CategoryType[]>(
10+
`/consolidated/${slugify(language.name)}.json`
1611
);
1712

1813
const fetchedSnippets = data
19-
? data.find((item) => item.categoryName === category)?.snippets
14+
? data.find((item) => item.name === category)?.snippets
2015
: [];
2116

2217
return { fetchedSnippets, loading, error };

src/types/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
export type LanguageType = {
2-
lang: string;
2+
name: string;
33
icon: string;
4+
subIndexes: {
5+
name: string;
6+
icon: string;
7+
}[];
48
};
59

610
export type CategoryType = {
7-
categoryName: string;
11+
name: string;
812
snippets: SnippetType[];
913
};
1014

0 commit comments

Comments
 (0)