()
| 7 | } |
| 8 | |
| 9 | export default function Board() { |
| 10 | const [pokemon, setPokemon] = useState<Array<IPokemon>>([]); |
| 11 | |
| 12 | useEffect(() => { |
| 13 | const fetch10Pokemon = () => { |
| 14 | let promises = []; |
| 15 | for (let i = 1; i <= 5; i++) { |
| 16 | const pokemonNumber = Math.round(Math.random() * 150 + 1); |
| 17 | promises.push( |
| 18 | fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonNumber}`) |
| 19 | ); |
| 20 | } |
| 21 | return Promise.all(promises); |
| 22 | }; |
| 23 | |
| 24 | const awaitJson = (responses: Response[]) => { |
| 25 | return Promise.all( |
| 26 | responses.map((response) => { |
| 27 | if (response.ok) return response.json(); |
| 28 | throw new Error(response.statusText); |
| 29 | }) |
| 30 | ); |
| 31 | }; |
| 32 | |
| 33 | fetch10Pokemon() |
| 34 | .then(awaitJson) |
| 35 | .then((pokemonData: Array<any>) => { |
| 36 | const mappedPokemonData = pokemonData.map((pd) => { |
| 37 | return { |
| 38 | name: pd.name, |
| 39 | url: pd.sprites.front_default, |
| 40 | }; |
| 41 | }); |
| 42 | setPokemon(mappedPokemonData); |
| 43 | }); |
| 44 | }, []); // eslint-disable-line react-hooks/exhaustive-deps |
| 45 | |
| 46 | const updatePokemonOrdering = (result: any) => { |
| 47 | const { destination, source, draggableId } = result; |
| 48 | if (!destination) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if ( |
| 53 | destination.droppableId === source.droppableId && |
| 54 | destination.index === source.index |
| 55 | ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | const sourcePokemon: IPokemon = pokemon.find( |
| 60 | (pokemon) => pokemon.name === draggableId |
| 61 | ) as IPokemon; |
| 62 | const updatedPokemon: IPokemon[] = Array.from(pokemon); |
| 63 | updatedPokemon.splice(source.index, 1); |
| 64 | updatedPokemon.splice(destination.index, 0, sourcePokemon); |
| 65 | setPokemon(updatedPokemon); |
| 66 | }; |
nothing calls this directly
no test coverage detected