-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
astro-pokedex: add grid and detail page
- Loading branch information
Showing
13 changed files
with
428 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
import { defineConfig } from "astro/config"; | ||
import tailwind from "@astrojs/tailwind"; | ||
import react from "@astrojs/react"; | ||
|
||
import node from "@astrojs/node"; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [tailwind()] | ||
output: "server", | ||
integrations: [tailwind(), react()], | ||
vite: { | ||
ssr: { | ||
noExternal: ["bulma"] | ||
} | ||
}, | ||
adapter: node({ | ||
mode: "standalone" | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useState, useEffect } from "react"; | ||
import ItemCard from "./ItemCard.jsx"; | ||
|
||
const FETCH_LIMIT = 30; | ||
|
||
function getPostIdFromUrl(url) { | ||
return url.split("/").reverse()[1]; | ||
} | ||
|
||
function Grid({ items = [], startPage }) { | ||
console.log("items", items); | ||
const [data, setData] = useState(items); | ||
const [displayData, setDisplayData] = useState(items); | ||
const [searchValue, setSearchValue] = useState(""); | ||
const [page, setPage] = useState(startPage); | ||
|
||
useEffect(() => { | ||
// prevent run on first render | ||
if (page !== startPage) { | ||
const fetchData = async () => { | ||
const response = await fetch( | ||
`https://pokeapi.co/api/v2/pokemon?limit=${FETCH_LIMIT}&offset=${ | ||
page * FETCH_LIMIT | ||
}` | ||
); | ||
const res = await response.json(); | ||
setData([...data, ...res.results]); | ||
}; | ||
|
||
fetchData(); | ||
} | ||
}, [page]); | ||
|
||
useEffect(() => { | ||
const filteredCollection = data.filter((item) => | ||
item.name.includes(searchValue) | ||
); | ||
setDisplayData(filteredCollection); | ||
}, [data, searchValue]); | ||
|
||
return ( | ||
<div className="grid-container"> | ||
<div className="grid-search"> | ||
<input | ||
id="search-input" | ||
className="input is-medium" | ||
type="text" | ||
placeholder="Search" | ||
onChange={(e) => setSearchValue(e.target.value)} | ||
/> | ||
</div> | ||
<div className="grid-wrapper"> | ||
{displayData.map((item) => { | ||
const postId = getPostIdFromUrl(item.url); | ||
return ( | ||
<a href={`/detail/${postId}`} key={postId}> | ||
<ItemCard postId={postId} name={item.name} /> | ||
</a> | ||
); | ||
})} | ||
</div> | ||
<button | ||
className="load-more button is-primary is-fullwidth" | ||
onClick={() => setPage(page + 1)} | ||
> | ||
Load More | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default Grid; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
function DetailTable({ height, weight, types }) { | ||
return ( | ||
height && | ||
weight && | ||
types && ( | ||
<table className="mb-4 mt-4"> | ||
<tbody> | ||
<tr> | ||
<th>Height:</th> | ||
<td>{height}</td> | ||
</tr> | ||
<tr> | ||
<th>Weight:</th> | ||
<td>{weight}</td> | ||
</tr> | ||
<tr> | ||
<th>Types:</th> | ||
<td>{types.map((type) => type.type.name).join(', ')}</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
) | ||
) | ||
} | ||
|
||
function ItemCard({ postId, name, height, weight, types }) { | ||
return ( | ||
<div className="tile"> | ||
<figure className="image"> | ||
<img | ||
src={`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${postId}.png`} | ||
alt={name} | ||
/> | ||
</figure> | ||
<div className="content"> | ||
<h3> | ||
<span className="prefix">#{postId}: </span> | ||
{name} | ||
</h3> | ||
<DetailTable {...{ height, weight, types }} /> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default ItemCard | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
import Layout from "../layouts/Layout.astro"; | ||
export const prerender = true; | ||
--- | ||
|
||
<Layout title="Pokedex App - About"> | ||
<div class="content about-page"> | ||
<h1>About</h1> | ||
<p> | ||
This app serves as an objective example for comparison of different web | ||
frontend frameworks (Next.js, Nuxt, SvelteKit). By developing the same | ||
(identical) app three times, one can estimate more closely the strenghts | ||
and weaknesses of a certain framework. | ||
</p> | ||
</div> | ||
</Layout> | ||
|
||
<style></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
import ItemCard from "../../components/ItemCard.jsx"; | ||
import Layout from "../../layouts/Layout.astro"; | ||
const { id } = Astro.params; | ||
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`); | ||
const data = await response.json(); | ||
--- | ||
|
||
<Layout title={`Pokedex App - ${data.name}`}> | ||
<a className="button is-ghost mb-4" href="/">‹ Back</a> | ||
<ItemCard postId={data.id} {...data} /> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
--- | ||
import Grid from "../components/Grid.jsx"; | ||
import Layout from "../layouts/Layout.astro"; | ||
export const prerender = true; | ||
const FETCH_LIMIT = 30; | ||
const response = await fetch( | ||
`https://pokeapi.co/api/v2/pokemon?limit=${FETCH_LIMIT} | ||
}` | ||
); | ||
const data = await response.json(); | ||
--- | ||
|
||
<Layout title="Welcome to Astro."> | ||
<main> | ||
<h1 class="text-3xl font-bold color-blue underline">Hello world!</h1> | ||
</main> | ||
<Layout title="Pokedex App"> | ||
<Grid items={data.results} startPage={0} client:load /> | ||
</Layout> | ||
|
||
<style></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"extends": "astro/tsconfigs/base" | ||
} | ||
"extends": "astro/tsconfigs/base", | ||
"compilerOptions": { | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "react" | ||
} | ||
} |
Oops, something went wrong.