Skip to content

Instantly share code, notes, and snippets.

@ozcanzaferayan
Last active December 19, 2022 07:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozcanzaferayan/777878aea81ca32bee2a214a20823822 to your computer and use it in GitHub Desktop.
Save ozcanzaferayan/777878aea81ca32bee2a214a20823822 to your computer and use it in GitHub Desktop.
I want you to act as a Senior Frontend developer. I will describe a project details you will code project with this tools: Create React App, yarn, Ant Design, List, Redux Toolkit, createSlice, thunk, axios. You should merge files in single index.js file and nothing else. Do not write explanations. My first request is "Create Pokemon App that lis…
// I want you to act as a Senior Frontend developer. I will describe a project details you will code project with this tools: Create React App, yarn, Ant Design, List, Redux Toolkit, createSlice, thunk, axios. You should merge files in single index.js file and nothing else. Do not write explanations. My first request is "Create Pokemon App that lists pokemons with images that come from PokeAPI sprites endpoint"
import React, { useEffect } from "react";
import ReactDOM from "react-dom";
import { Provider, useDispatch, useSelector } from "react-redux";
import { createStore, applyMiddleware, combineReducers } from "redux";
import thunk from "redux-thunk";
import axios from "axios";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { List } from "antd";
const fetchPokemons = createAsyncThunk("pokemons/fetchPokemons", async () => {
const response = await axios.get(
"https://pokeapi.co/api/v2/pokemon?limit=151"
);
return response.data.results;
});
const pokemonsSlice = createSlice({
name: "pokemons",
initialState: {
list: [],
loading: false,
error: null,
},
reducers: {},
extraReducers: {
[fetchPokemons.pending]: (state) => {
state.loading = true;
},
[fetchPokemons.fulfilled]: (state, action) => {
state.list = action.payload;
state.loading = false;
state.error = null;
},
[fetchPokemons.rejected]: (state, action) => {
state.loading = false;
state.error = action.error;
},
},
});
export const { reducer: pokemonsReducer } = pokemonsSlice;
const rootReducer = combineReducers({
pokemons: pokemonsReducer,
});
const store = createStore(rootReducer, applyMiddleware(thunk));
const App = () => {
const dispatch = useDispatch();
const pokemons = useSelector((state) => state.pokemons.list);
useEffect(() => {
dispatch(fetchPokemons());
}, [dispatch]);
return (
<List
dataSource={pokemons}
renderItem={(pokemon) => (
<List.Item>
<img
src={`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${
pokemon.url.split("/")[pokemon.url.split("/").length - 2]
}.png`}
alt="Pokemon"
/>
<span>{pokemon.name}</span>
</List.Item>
)}
rowKey="id"
/>
);
};
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment