Skip to content

Commit

Permalink
chore: starts epicweb-dev#1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoKloganB committed Mar 20, 2022
1 parent 9fdf1c1 commit 2b0f245
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/exercise/1.extra-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Simple Data-fetching
// http://localhost:3000/isolated/exercise/01.js

import * as React from 'react'
import {fetchPokemon, PokemonDataView, PokemonErrorBoundary} from '../pokemon'


function createResource(promise) {
let status = 'pending'
let result = promise.then(
resolved => {
status = 'success'
result = resolved
},
rejected => {
status = 'error'
result = rejected
},
)

return {
read() {
if (status === 'pending') throw result
if (status === 'error') throw result
if (status === 'success') return result
throw new Error('Unable to fetch pokemon')
},
}
}

let pokemonResource = createResource(fetchPokemon('pikachu'))

function PokemonInfo() {
const pokemon = pokemonResource.read()
return (
<div>
<div className="pokemon-info__img-wrapper">
<img src={pokemon.image} alt={pokemon.name} />
</div>
<PokemonDataView pokemon={pokemon} />
</div>
)
}

function LoadingPokemon() {
return <div>loading pokemon...</div>
}

function App() {
return (
<div className="pokemon-info-app">
<div className="pokemon-info">
<PokemonErrorBoundary>
<React.Suspense fallback={<LoadingPokemon />}>
<PokemonInfo />
</React.Suspense>
</PokemonErrorBoundary>
</div>
</div>
)
}

export default App

0 comments on commit 2b0f245

Please sign in to comment.