22
33import Link from "next/link" ;
44import React , { useCallback , useEffect , useState } from "react" ;
5- import LoadingSpinner from "@/lib/client/LoadingSpinner" ;
65import { DropdownKey , ProfileData } from "@/lib/client/schema" ;
76import { dropdownConfig , ProfileFilters } from "./ProfileFilters" ;
87import Image from "next/image" ;
@@ -26,19 +25,68 @@ const initialState = {
2625 forceRun : false ,
2726} ;
2827
28+ type ProfileFilters = {
29+ gender : string ;
30+ minAge : number | null ;
31+ maxAge : number | null ;
32+ minIntroversion : number | null ;
33+ maxIntroversion : number | null ;
34+ interests : string [ ] ;
35+ coreValues : string [ ] ;
36+ causeAreas : string [ ] ;
37+ connections : string [ ] ;
38+ searchQuery : string ;
39+ forceRun : boolean ;
40+ } ;
41+
2942
3043export default function ProfilePage ( ) {
3144 const [ profiles , setProfiles ] = useState < ProfileData [ ] > ( [ ] ) ;
3245 const [ loading , setLoading ] = useState ( true ) ;
3346 const [ error , setError ] = useState ( '' ) ;
3447 const [ _ , setShowFilters ] = useState ( true ) ;
35- const [ images , setImages ] = useState < string [ ] > ( [ ] )
48+ const [ images , setImages ] = useState < string [ ] > ( [ ] ) ;
49+
50+ const [ text , setText ] = useState < string > ( '' ) ;
51+
3652 const [ filters , setFilters ] = useState ( initialState ) ;
3753
54+ useEffect ( ( ) => {
55+ const params = new URLSearchParams ( window . location . search ) ;
56+ const newFilters = { ...initialState } ;
57+
58+ for ( const [ key , value ] of params . entries ( ) ) {
59+ // Type guard to check if the key is a valid filter key
60+ if ( key in newFilters ) {
61+ const filterKey = key as keyof ProfileFilters ;
62+
63+ if ( key === 'searchQuery' ) {
64+ setText ( value ) ;
65+ newFilters [ filterKey ] = value as never ;
66+ }
67+ else if ( [ 'interests' , 'coreValues' , 'causeAreas' , 'connections' ] . includes ( key ) ) {
68+ const arrayKey = filterKey as 'interests' | 'coreValues' | 'causeAreas' | 'connections' ;
69+ newFilters [ arrayKey ] = [ ...newFilters [ arrayKey ] , value ] ;
70+ }
71+ }
72+ }
73+
74+ console . log ( newFilters ) ;
75+
76+ setFilters ( newFilters ) ;
77+ } , [ ] ) ;
78+
79+ const [ isStart , setIsStart ] = useState ( true ) ;
80+
3881 const fetchProfiles = useCallback ( async ( ) => {
3982 try {
4083 setLoading ( true ) ;
41- const params = new URLSearchParams ( ) ;
84+ let params = new URLSearchParams ( ) ;
85+ if ( isStart ) {
86+ params = new URLSearchParams ( window . location . search ) ;
87+ setIsStart ( false ) ;
88+ }
89+ console . log ( 'fetchProfiles' , params ) ;
4290
4391 if ( filters . gender ) params . append ( 'gender' , filters . gender ) ;
4492 if ( filters . minAge ) params . append ( 'minAge' , filters . minAge . toString ( ) ) ;
@@ -54,9 +102,12 @@ export default function ProfilePage() {
54102 }
55103 }
56104
57- if ( filters . searchQuery ) params . append ( 'search' , filters . searchQuery ) ;
105+ if ( filters . searchQuery ) params . append ( 'searchQuery' , filters . searchQuery ) ;
106+
107+ let s = params . toString ( ) ;
108+ window . history . pushState ( { } , '' , `?${ s } ` ) ;
58109
59- const response = await fetch ( `/api/profiles?${ params . toString ( ) } ` ) ;
110+ const response = await fetch ( `/api/profiles?${ s } ` ) ;
60111 const data = await response . json ( ) ;
61112 if ( ! response . ok ) {
62113 console . log ( response ) ;
@@ -124,10 +175,9 @@ export default function ProfilePage() {
124175 const resetFilters = ( ) => {
125176 setFilters ( initialState ) ;
126177 setText ( '' ) ;
178+ // window.history.pushState({}, '', '');
127179 } ;
128180
129- const [ text , setText ] = useState < string > ( '' ) ;
130-
131181 const onFilterChange = handleFilterChange
132182
133183 return (
@@ -205,9 +255,10 @@ export default function ProfilePage() {
205255 < div className = "flex-1" >
206256 { loading ? (
207257 < div className = "flex justify-center py-8" >
208- < div className = "flex justify-center min-h-screen py-8" >
209- < div data-testid = "spinner" className = "animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500" > </ div >
210- </ div >
258+ < div className = "flex justify-center min-h-screen py-8" >
259+ < div data-testid = "spinner"
260+ className = "animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500" > </ div >
261+ </ div >
211262 </ div >
212263 ) : error ? (
213264 < div className = "flex justify-center py-2" >
0 commit comments