Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code updates for alternative API #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions completed-apps/infinite-swapi/src/people/InfinitePeople.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import InfiniteScroll from "react-infinite-scroller";
import { useInfiniteQuery } from "@tanstack/react-query";
import { Person } from "./Person";

const initialUrl = "https://swapi.dev/api/people/";
const baseUrl = "https://swapi-node.vercel.app";
const initialUrl = baseUrl + "/api/people/";
const fetchUrl = async (url) => {
const response = await fetch(url);
return response.json();
Expand All @@ -21,7 +22,7 @@ export function InfinitePeople() {
queryKey: ["sw-people"],
queryFn: ({ pageParam = initialUrl }) => fetchUrl(pageParam),
getNextPageParam: (lastPage) => {
return lastPage.next || undefined;
return lastPage.next ? baseUrl + lastPage.next : undefined;
},
});

Expand All @@ -48,10 +49,10 @@ export function InfinitePeople() {
return pageData.results.map((person) => {
return (
<Person
key={person.name}
name={person.name}
hairColor={person.hair_color}
eyeColor={person.eye_color}
key={person.fields.name}
name={person.fields.name}
hairColor={person.fields.hair_color}
eyeColor={person.fields.eye_color}
/>
);
});
Expand Down
15 changes: 9 additions & 6 deletions completed-apps/infinite-swapi/src/species/InfiniteSpecies.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { useInfiniteQuery } from "@tanstack/react-query";

import { Species } from "./Species";

const initialUrl = "https://swapi.dev/api/species/";
const baseUrl = "https://swapi-node.vercel.app";
const initialUrl = baseUrl + "/api/species/";
const fetchUrl = async (url) => {
const response = await fetch(url);
return response.json();
Expand All @@ -21,7 +22,9 @@ export function InfiniteSpecies() {
} = useInfiniteQuery({
queryKey: ["sw-species"],
queryFn: ({ pageParam = initialUrl }) => fetchUrl(pageParam),
getNextPageParam: (lastPage) => lastPage.next || undefined,
getNextPageParam: (lastPage) => {
return lastPage.next ? baseUrl + lastPage.next : undefined;
},
});

if (isLoading) return <div className="loading">Loading...</div>;
Expand All @@ -40,10 +43,10 @@ export function InfiniteSpecies() {
return pageData.results.map((species) => {
return (
<Species
key={species.name}
name={species.name}
language={species.language}
averageLifespan={species.average_lifespan}
key={species.fields.name}
name={species.fields.name}
language={species.fields.language}
averageLifespan={species.fields.average_lifespan}
/>
);
});
Expand Down