Skip to content

Commit

Permalink
refactor: Update data retrieval in Genshin character and teams pages
Browse files Browse the repository at this point in the history
  • Loading branch information
dvaJi committed Sep 9, 2024
1 parent d76ab3d commit 4fe8512
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
10 changes: 8 additions & 2 deletions app/[lang]/(genshin)/character/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ export default async function GenshinCharacterPage({ params }: Props) {
id: params.id,
},
}),
getRemoteData<Teams>("genshin", "teams"),
getGenshinData<Teams>({
resource: "teams",
language: langData,
filter: {
id: params.id,
},
}),
getGenshinData<Character[]>({
resource: "characters",
language: langData as any,
Expand Down Expand Up @@ -252,7 +258,7 @@ export default async function GenshinCharacterPage({ params }: Props) {
);
const ascensionTotal = calculateTotalAscensionMaterials(character.ascension);

const recommendedTeams: TeamData[] = teams[character.id]?.teams || [];
const recommendedTeams: TeamData[] = teams?.teams || [];

const jsonLd: WithContext<BreadcrumbList> = {
"@context": "https://schema.org",
Expand Down
11 changes: 8 additions & 3 deletions app/[lang]/(genshin)/teams/[character]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import useTranslations from "@hooks/use-translations";
import type { Artifact, Beta, Character, Weapon } from "@interfaces/genshin";
import { AD_ARTICLE_SLOT } from "@lib/constants";
import { getGenshinData } from "@lib/dataApi";
import { getData, getRemoteData } from "@lib/localData";
import { getData } from "@lib/localData";
import { capitalize } from "@utils/capitalize";
import { localeToLang } from "@utils/locale-to-lang";

Expand Down Expand Up @@ -118,8 +118,13 @@ export default async function GenshinCharacterTeams({ params }: Props) {
return redirect(`/${params.lang}/teams`);
}

const teams = await getRemoteData<Teams>("genshin", "teams");
const characterTeams = teams[params.character];
const characterTeams = await getGenshinData<Teams>({
resource: "teams",
language: langData,
filter: {
id: params.character,
},
});

// No teams for this character, redirect to the character page instead.
if (!characterTeams) {
Expand Down
16 changes: 9 additions & 7 deletions app/[lang]/(genshin)/teams/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import useTranslations from "@hooks/use-translations";
import type { Character } from "@interfaces/genshin";
import { AD_ARTICLE_SLOT } from "@lib/constants";
import { getGenshinData } from "@lib/dataApi";
import { getRemoteData } from "@lib/localData";

const Ads = importDynamic(() => import("@components/ui/Ads"), { ssr: false });
const FrstAds = importDynamic(() => import("@components/ui/FrstAds"), {
Expand Down Expand Up @@ -59,7 +58,10 @@ export default async function GenshinCharacters({ params }: Props) {
"teams"
);

const teams = await getRemoteData<Teams>("genshin", "teams");
const teams = await getGenshinData<Teams[]>({
resource: "teams",
language: langData,
});

const characters = await getGenshinData<Record<string, Character>>({
resource: "characters",
Expand All @@ -68,17 +70,17 @@ export default async function GenshinCharacters({ params }: Props) {
asMap: true,
});

const teamsByName: Record<string, TeamData[]> = Object.entries(teams).reduce(
(map, [id, characterTeams]) => {
if (!characters[id]?.name || !characterTeams.teams[0]) return map;
const teamsByName: Record<string, TeamData[]> = teams.reduce(
(map, { id, teams }) => {
if (!characters[id]?.name || !teams[0]) return map;

if (!map[characters[id].name]) {
map[characters[id].name] = [];
}

map[characters[id].name].push({
name: characterTeams.teams[0].name,
characters: characterTeams.teams[0].characters.map((c) => ({
name: teams[0].name,
characters: teams[0].characters.map((c) => ({
id: c.id,
name: characters[c.id]?.name || "",
role: c.role,
Expand Down
2 changes: 1 addition & 1 deletion interfaces/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export type TeamData = {
characters: CharacterTeam[];
};

export type Teams = Record<string, { teams: TeamData[]; overview: string }>;
export type Teams = { id: string; teams: TeamData[]; overview: string };

0 comments on commit 4fe8512

Please sign in to comment.