Skip to content

Commit 9cfe565

Browse files
committed
fix: Refactor use fetch plugins
1 parent c1296ed commit 9cfe565

File tree

3 files changed

+53
-28
lines changed

3 files changed

+53
-28
lines changed

src/components/Plugins/PluginCard.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { Plugin } from "../../interfaces/Plugin";
44
type PluginCardProps = {
55
plugin: Plugin;
66
}
7-
export function PluginCard({plugin}:PluginCardProps) {
8-
return <section className="border gap-4 flex flex-col max-w-[314px] hover:scale-[1.01] p-4 pt-6 m-auto w-full border-[#27272A] rounded-md h-[300px]">
7+
export function PluginCard({ plugin }: PluginCardProps) {
8+
return <section className="border gap-4 flex flex-col max-w-[314px] hover:scale-[1.01] p-4 pt-6 m-auto w-full border-[#27272A] rounded-md min-h-[300px]">
99
<div className="flex-1 flex flex-col gap-4">
1010
<div>
1111
<div>
@@ -18,7 +18,7 @@ export function PluginCard({plugin}:PluginCardProps) {
1818
<div className="text-white text-sm">
1919
{plugin.description}
2020
</div>
21-
<div className="w-full">
21+
<div className="w-full max-w-[200px]">
2222
<img className="w-full" src={plugin.imageURL} />
2323
</div>
2424
</div>

src/hooks/useFetchPlugins.ts

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,59 @@ function useFetchPlugins(
1919
const [plugins, setPlugins] = useState<Plugin[]>([]);
2020
const [isLoading, setIsLoading] = useState(false);
2121
const [error, setError] = useState("");
22-
useEffect(function(){
23-
24-
const fetchPlugins = async() => {
25-
try{
26-
setIsLoading(true)
27-
if(searchQuery){
28-
url += `?searchQuery=${searchQuery}`
29-
}
30-
console.log(url)
31-
const data = await galleryApiFetch(url);
32-
const plugins = await data.json()
33-
setPlugins(plugins.data);
34-
} catch(e){
35-
36-
}
37-
finally{
38-
setIsLoading(false)
39-
}
22+
useEffect(function() {
23+
// avoid race conditions
24+
const controller = new AbortController()
25+
let isAbort = false
26+
27+
setIsLoading(true)
28+
setError("")
29+
setPlugins([])
30+
31+
fetchPlugins(controller, url, searchQuery)
32+
33+
.then((plugins)=> {
34+
setPlugins(plugins)
35+
setError("")
36+
})
37+
38+
.catch((err: Error) => {
39+
if(err.name != 'AbortError') {
40+
setError(err.message);
4041
}
42+
else {
43+
isAbort = true
44+
}
45+
})
4146

42-
fetchPlugins();
47+
.finally(() => {
48+
if(!isAbort) setIsLoading(false)
49+
})
4350

44-
},[searchQuery])
51+
return () => controller.abort()
52+
},[searchQuery, url, pageNum, pageSize])
4553

4654

4755

4856
return {plugins, isLoading, error }
4957
}
5058

59+
const fetchPlugins = async(controller: AbortController, url: string, searchQuery?: string) => {
60+
try{
61+
if(searchQuery){
62+
url += `?searchQuery=${searchQuery}`
63+
}
64+
const data = await galleryApiFetch(url,{
65+
signal: controller.signal
66+
});
67+
const plugins = await data.json()
68+
return plugins.data
69+
} catch(e: unknown){
70+
if((e as Error).name != 'AbortError')
71+
throw new Error("Problem fetching plugins!")
72+
else throw e
73+
}
74+
}
75+
5176

5277
export default useFetchPlugins

src/pages/Plugins.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ const Plugins: React.FC = () => {
1717

1818
const { plugins, isLoading, error } = useFetchPlugins(Endpoints.fetchApiPlugins, 30, 1,searchQuery );
1919

20-
2120
return (
2221
<div className=" min-h-screen bg-black">
2322
<div className='p-8 flex flex-col gap-8 max-w-[1024px] m-auto'>
2423
<PluginSearchBar />
2524
<div className='grid xs:grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-x-16 gap-y-8'>
26-
{isLoading ? Array.from({length : 9}).map((_,i)=><Skeleton width="300px" containerClassName='m-auto' height="300px" />) :
25+
{isLoading ? Array.from({length : 9}).map((_ , i)=><Skeleton key={i} width="280px" containerClassName='m-auto' height="300px" />) :
2726
plugins.length > 0 && plugins.map(plugin =>{
28-
return <PluginCard plugin={plugin} />
27+
return <PluginCard key={plugin.id} plugin={plugin} />
2928
})}
3029
</div>
30+
{!isLoading && error && <h1 className='text-white text-lg flex items-center justify-center '>{error}</h1>
31+
}
3132
{
32-
!isLoading && plugins.length == 0 &&
33+
!error && !isLoading && plugins.length == 0 &&
3334
<h1 className='text-white text-lg flex items-center justify-center '>No search results found...</h1>
34-
3535
}
3636
</div>
3737
</div>

0 commit comments

Comments
 (0)