2023-03-26 22:26:15 +11:00
|
|
|
import {
|
|
|
|
createApi,
|
|
|
|
FetchArgs,
|
|
|
|
fetchBaseQuery,
|
|
|
|
} from '@reduxjs/toolkit/query/react';
|
2023-03-19 16:53:07 +11:00
|
|
|
import type { PokemonProps as Pokemon } from './Pokemon';
|
2023-03-26 22:26:15 +11:00
|
|
|
import {
|
2023-03-27 22:52:15 +11:00
|
|
|
pokeApiAllPagesCustomBaseQuery,
|
2023-03-26 22:26:15 +11:00
|
|
|
pokeApiFullListFetchArgs,
|
|
|
|
} from './paginationBaseQuery';
|
2023-03-19 16:53:07 +11:00
|
|
|
|
2023-03-19 22:42:09 +11:00
|
|
|
export interface Region {
|
|
|
|
name: string;
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Type {
|
|
|
|
name: string;
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
|
2023-03-26 22:26:15 +11:00
|
|
|
export interface PokemonListResponse {
|
|
|
|
count: number;
|
|
|
|
results: Pokemon[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RegionListResponse {
|
2023-03-19 22:42:09 +11:00
|
|
|
count: number;
|
|
|
|
results: Region[];
|
|
|
|
}
|
|
|
|
|
2023-03-26 22:26:15 +11:00
|
|
|
export interface TypeListResponse {
|
2023-03-19 22:42:09 +11:00
|
|
|
count: number;
|
|
|
|
results: Type[];
|
|
|
|
}
|
|
|
|
|
2023-03-26 22:26:15 +11:00
|
|
|
const pokeApiBaseQuery = async (
|
|
|
|
args: pokeApiFullListFetchArgs,
|
|
|
|
api: any,
|
|
|
|
extra: any,
|
|
|
|
) => {
|
|
|
|
const baseUrl = 'https://pokeapi.co/api/v2/';
|
|
|
|
|
|
|
|
if (args.fetchAllPages) {
|
2023-03-27 22:52:15 +11:00
|
|
|
return pokeApiAllPagesCustomBaseQuery(args, api, extra, baseUrl);
|
2023-03-26 22:26:15 +11:00
|
|
|
} else {
|
|
|
|
return fetchBaseQuery({ baseUrl })(args, api, extra);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-19 16:53:07 +11:00
|
|
|
export const pokedexApi = createApi({
|
|
|
|
reducerPath: 'pokedexApi',
|
2023-03-26 22:26:15 +11:00
|
|
|
baseQuery: pokeApiBaseQuery,
|
2023-03-19 16:53:07 +11:00
|
|
|
endpoints: builder => ({
|
2023-03-26 22:26:15 +11:00
|
|
|
getPokemonList: builder.query<PokemonListResponse, void>({
|
|
|
|
query: () => ({ url: `pokemon`, fetchAllPages: true }),
|
2023-03-19 16:53:07 +11:00
|
|
|
}),
|
2023-03-26 22:26:15 +11:00
|
|
|
getRegionList: builder.query<RegionListResponse, void>({
|
|
|
|
query: () => ({ url: 'region', fetchAllPages: true }),
|
2023-03-19 22:42:09 +11:00
|
|
|
}),
|
2023-03-26 22:26:15 +11:00
|
|
|
getTypeList: builder.query<TypeListResponse, void>({
|
|
|
|
query: () => ({ url: 'type', fetchAllPages: true }),
|
|
|
|
transformResponse: (response: RegionListResponse) => {
|
2023-03-19 22:42:09 +11:00
|
|
|
return {
|
|
|
|
...response,
|
|
|
|
results: [{ name: 'All Types', url: '' }, ...response.results],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}),
|
2023-03-26 18:39:04 +11:00
|
|
|
getPokemon: builder.query<Pokemon, number | string>({
|
2023-03-26 22:26:15 +11:00
|
|
|
query: IdOrName => ({ url: `pokemon/${IdOrName}` }),
|
2023-03-26 18:39:04 +11:00
|
|
|
}),
|
|
|
|
getRegion: builder.query<Region, number | string>({
|
2023-03-26 22:26:15 +11:00
|
|
|
query: IdOrName => ({ url: `region/${IdOrName}` }),
|
|
|
|
}),
|
|
|
|
getType: builder.query<Type, number | string>({
|
|
|
|
query: IdOrName => ({ url: `type/${IdOrName}` }),
|
2023-03-26 18:39:04 +11:00
|
|
|
}),
|
2023-03-19 16:53:07 +11:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2023-03-19 22:42:09 +11:00
|
|
|
export const {
|
|
|
|
useGetPokemonListQuery,
|
|
|
|
useGetRegionListQuery,
|
|
|
|
useGetTypeListQuery,
|
2023-03-26 22:26:15 +11:00
|
|
|
useGetPokemonQuery,
|
|
|
|
useGetRegionQuery,
|
|
|
|
useGetTypeQuery,
|
2023-03-19 22:42:09 +11:00
|
|
|
} = pokedexApi;
|