1
- import { useState } from "react" ;
1
+ import { useState , useRef } from "react" ;
2
2
import { SearchArtistByName } from "../../axios/spotifyAxios.js" ;
3
3
import LoaderComponent from "../Common/LoaderComponent.jsx" ;
4
- import ButtonSubmitDefault from "../Buttons/ButtonSubmitDefault.jsx" ;
5
4
import ReviewCardFindArtistComponent from "./reviewCardFindArtistComponent.jsx" ;
6
5
7
6
const ReviewFindArtistComponent = ( ) => {
8
7
const [ search , setSearch ] = useState ( "" ) ;
8
+ const [ searchLimit , setSearchLimit ] = useState ( 5 ) ;
9
+ const [ searchOffset , setSearchOffset ] = useState ( 0 ) ;
9
10
const [ searchResult , setSearchResult ] = useState ( [ ] ) ;
10
11
const [ isLoading , setIsLoading ] = useState ( false ) ;
11
12
const [ isResultNull , setIsResultNull ] = useState ( false ) ;
13
+ const elementRef = useRef ( null ) ; // Reference for the element to scroll to
12
14
13
- const getSubmitData = async ( e ) => {
14
- e . preventDefault ( ) ;
15
+ const getSubmitData = async ( limit , offset ) => {
15
16
setIsLoading ( true ) ;
16
17
try {
17
- const res = await SearchArtistByName ( search ) ;
18
- console . log ( res ) ;
19
- res ?. length === 0 ? setIsResultNull ( true ) : setIsResultNull ( false ) ;
20
- setSearchResult ( res || [ ] ) ; // Update based on actual response structure
18
+ const res = await SearchArtistByName ( search , limit , offset ) ;
19
+ if ( res ?. length === 0 ) {
20
+ setIsResultNull ( true ) ;
21
+ } else {
22
+ setIsResultNull ( false ) ;
23
+ setSearchResult ( ( prevResults ) => [ ...prevResults , ...res ] ) ;
24
+ }
21
25
} catch ( error ) {
22
26
console . log ( error ) ;
23
- setSearchResult ( [ ] ) ; // Clear results if there's an error
27
+ setIsResultNull ( true ) ;
24
28
} finally {
25
29
setIsLoading ( false ) ;
26
30
}
27
31
} ;
28
32
33
+ const handleSearch = async ( e ) => {
34
+ e . preventDefault ( ) ; // Prevent page reload
35
+ setSearchResult ( [ ] ) ; // Clear previous results
36
+ setSearchOffset ( 0 ) ; // Reset offset for the first search
37
+ setSearchLimit ( 5 ) ; // Reset limit for the first search
38
+ await getSubmitData ( 5 , 0 ) ; // Fetch first set of results
39
+ } ;
40
+
41
+ const loadMore = async ( ) => {
42
+ if ( searchOffset === 0 ) {
43
+ // Second search: Limit 10, Offset 6
44
+ setSearchLimit ( 10 ) ;
45
+ setSearchOffset ( 6 ) ;
46
+ await getSubmitData ( 10 , 6 ) ;
47
+
48
+ // Scroll to the element with index 6
49
+ setTimeout ( ( ) => {
50
+ elementRef . current ?. scrollIntoView ( {
51
+ behavior : "smooth" ,
52
+ block : "center" ,
53
+ } ) ;
54
+ } , 100 ) ;
55
+ }
56
+ } ;
57
+
29
58
return (
30
59
< div className = "w-full md:w-4/5 flex flex-col items-center" >
60
+ { /* Search Bar */ }
31
61
< div className = "w-[95vw] md:w-full h-10 flex items-center bg-gray-300 rounded-2xl pl-3 md:m-5" >
32
62
< span className = "w-auto text-xs md:text-sm text-gray-500 text-nowrap" > Search for an artist:</ span >
33
- < form onSubmit = { getSubmitData } className = "w-full h-full flex items-center pl-2 rounded-2xl" >
63
+ < form onSubmit = { handleSearch } className = "w-full h-full flex items-center pl-2 rounded-2xl" >
34
64
< input
35
65
type = "text"
36
66
name = "search"
@@ -44,24 +74,42 @@ const ReviewFindArtistComponent = () => {
44
74
</ form >
45
75
</ div >
46
76
47
- < hr className = "mt-5" />
48
-
49
77
{ /* Display loader or results */ }
50
78
{ isLoading ? (
51
79
< LoaderComponent />
52
80
) : searchResult . length > 0 ? (
53
- < div className = "w-full flex flex-wrap justify-start md:gap-1 pl-2 md:pl-0" >
54
- { searchResult
55
- . filter ( ( item ) => item . images . length > 0 ) // Filter items with non-empty images
56
- . map ( ( item ) => (
57
- < div key = { item . id } className = "w-1/2 md:w-1/6" >
58
- < ReviewCardFindArtistComponent item = { item } />
81
+ < div className = "flex flex-col w-full" >
82
+ < div className = "text-gray-500 pl-2 md:pl-0 pt-4 md:pt-0 pb-4" >
83
+ Results of your search:
84
+ </ div >
85
+ < div className = "w-full flex flex-wrap justify-start md:gap-1 pl-2 md:pl-0" >
86
+ { searchResult
87
+ . filter ( ( item ) => item . images . length > 0 )
88
+ . map ( ( item , index ) => (
89
+ < div
90
+ key = { item . id }
91
+ className = "w-1/2 md:w-1/6"
92
+ ref = { index === 5 ? elementRef : null } // Add ref to the 6th element
93
+ >
94
+ < ReviewCardFindArtistComponent item = { item } />
95
+ </ div >
96
+ ) ) }
97
+ </ div >
98
+ { searchResult . length < 15 && (
99
+ < div className = "w-full flex items-center justify-center mt-4 mb-0 md:mb-6" >
100
+ < div className = "w-full flex items-center gap-4 px-2" >
101
+ < div className = "flex-1 border-t border-gray-400" > </ div >
102
+ < button onClick = { loadMore } className = "px-4 py-2 bg-gray-400 text-white rounded" >
103
+ Load More
104
+ </ button >
105
+ < div className = "flex-1 border-t border-gray-400" > </ div >
106
+ </ div >
59
107
</ div >
60
- ) ) }
61
- </ div >
62
- ) : isResultNull ? (
108
+ ) }
109
+ </ div >
110
+ ) : isResultNull ? (
63
111
< p className = "text-center mt-4 text-gray-500" > No results found.</ p >
64
- ) : null }
112
+ ) : null }
65
113
</ div >
66
114
) ;
67
115
} ;
0 commit comments