FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tomateto-react/src/routes/Search.tsx at main · adamscript/tomateto-react · GitHub
adamscript
/
tomateto-react
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tomateto-react
/
src
/
routes
/
Search.tsx
Copy path
More file actions
More file actions
Latest commit
History
History
History
138 lines (124 loc) · 5.14 KB
Breadcrumbs
tomateto-react
/
src
/
routes
/
Search.tsx
Copy path
File metadata and controls
138 lines (124 loc) · 5.14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import
{
Box
,
CircularProgress
,
Stack
,
Typography
}
from
"@mui/material"
;
import
{
useEffect
,
useState
}
from
"react"
;
import
{
Navigate
,
useSearchParams
}
from
"react-router-dom"
;
import
{
useAppDispatch
,
useAppSelector
}
from
"../app/hooks"
;
import
{
PageLabel
}
from
"../components/page"
;
import
{
FeedPost
}
from
"../components/post"
;
import
{
UserRecommendation
}
from
"../components/user"
;
import
{
openSnackbarError
}
from
"../features/app/snackbarSlice"
;
import
{
loadPosts
}
from
"../features/post/feedPostSlice"
;
import
insertErrorLog
from
"../features/utility/errorLogging"
;
import
{
auth
}
from
"../firebase"
;
const
Search
=
(
)
=>
{
const
[
searchParams
]
=
useSearchParams
(
)
;
const
[
postLoaded
,
setPostLoaded
]
=
useState
(
false
)
;
const
[
userLoaded
,
setUserLoaded
]
=
useState
(
false
)
;
const
[
isLoaded
,
setLoaded
]
=
useState
(
false
)
;
const
authState
=
useAppSelector
(
(
state
)
=>
state
.
authState
)
;
const
dispatch
=
useAppDispatch
(
)
;
const
searchPostItems
=
useAppSelector
(
(
state
)
=>
state
.
feedPost
)
;
const
[
searchUserItems
,
setSearchUserItems
]
=
useState
(
[
]
)
;
const
listSearchPost
=
searchPostItems
.
map
(
(
items
,
index
)
=>
<
FeedPost
key
=
{
index
}
items
=
{
items
}
/>
)
const
listSearchUser
=
searchUserItems
.
map
(
(
items
,
index
)
=>
<
UserRecommendation
key
=
{
index
}
items
=
{
items
}
/>
)
useEffect
(
(
)
=>
{
document
.
title
=
`
${
searchParams
.
get
(
'q'
)
}
- Tomateto`
;
function
fetchListSearchPost
(
res
?:
string
)
{
fetch
(
`
${
process
.
env
.
REACT_APP_API_URL
}
/api/feed/search?q=
${
searchParams
.
get
(
'q'
)
}
`
,
{
mode
:
'cors'
,
headers
:
{
'Authorization'
:
res
?
`Bearer
${
res
}
`
:
'none'
}
}
)
.
then
(
(
res
)
=>
{
return
res
.
json
(
)
;
}
)
.
then
(
(
res
)
=>
{
dispatch
(
loadPosts
(
res
.
items
)
)
;
setPostLoaded
(
true
)
;
}
)
.
catch
(
(
err
)
=>
{
setLoaded
(
true
)
;
dispatch
(
openSnackbarError
(
"An error occurred while processing your request. Please try again later."
)
)
;
insertErrorLog
(
"Fetching Post for search page / Search"
,
err
)
;
}
)
}
function
fetchListSearchUser
(
res
?:
string
)
{
fetch
(
`
${
process
.
env
.
REACT_APP_API_URL
}
/api/user/search?q=
${
searchParams
.
get
(
'q'
)
}
`
,
{
mode
:
'cors'
,
headers
:
{
'Authorization'
:
res
?
`Bearer
${
res
}
`
:
'none'
}
}
)
.
then
(
(
res
)
=>
{
return
res
.
json
(
)
;
}
)
.
then
(
(
res
)
=>
{
setSearchUserItems
(
res
.
items
)
;
setUserLoaded
(
true
)
;
}
)
.
catch
(
(
err
)
=>
{
setLoaded
(
true
)
;
dispatch
(
openSnackbarError
(
"An error occurred while processing your request. Please try again later."
)
)
;
insertErrorLog
(
"Fetching User for search page / Search"
,
err
)
;
}
)
}
if
(
authState
.
isLoggedIn
)
{
auth
.
currentUser
?.
getIdToken
(
)
.
then
(
(
res
)
=>
{
if
(
searchParams
.
get
(
'q'
)
)
{
fetchListSearchPost
(
res
)
;
fetchListSearchUser
(
res
)
;
}
}
)
.
catch
(
(
err
)
=>
{
setLoaded
(
true
)
;
dispatch
(
openSnackbarError
(
"An error occurred while processing your request. Please try again later."
)
)
;
insertErrorLog
(
"Get Id token / Search"
,
err
)
;
}
)
}
else
{
if
(
searchParams
.
get
(
'q'
)
)
{
fetchListSearchPost
(
)
;
fetchListSearchUser
(
)
;
}
}
}
,
[
searchParams
]
)
;
useEffect
(
(
)
=>
{
if
(
postLoaded
&&
userLoaded
)
{
setLoaded
(
true
)
;
}
else
{
setLoaded
(
false
)
;
}
}
,
[
postLoaded
,
userLoaded
]
)
return
(
<
Box
sx
=
{
{
display
:
'flex'
,
flexDirection
:
'column'
,
alignItems
:
!
isLoaded
?
'center'
:
'stretch'
}
}
>
{
!
isLoaded
?
<
CircularProgress
/>
:
<
Box
>
{
listSearchUser
.
length
>
0
&&
<
PageLabel
>
Tomates
</
PageLabel
>
}
{
listSearchUser
}
{
listSearchPost
.
length
>
0
&&
<
PageLabel
>
Posts
</
PageLabel
>
}
{
listSearchPost
}
{
!
searchParams
.
get
(
'q'
)
&&
<
Navigate
to
=
'/explore'
/>
}
{
listSearchUser
.
length
==
0
&&
listSearchPost
.
length
==
0
&&
<
NoSearchFound
q
=
{
searchParams
.
get
(
'q'
)
}
/>
}
</
Box
>
}
</
Box
>
)
}
interface
NoSearchFoundProps
{
q
:
string
|
null
;
}
const
NoSearchFound
=
(
props
:
NoSearchFoundProps
)
=>
{
return
(
<
Box
sx
=
{
{
p
:
5
}
}
>
<
Stack
spacing
=
{
3
}
alignItems
=
"center"
justifyContent
=
"center"
>
<
Typography
variant
=
"h5"
align
=
"center"
sx
=
{
{
fontWeight
:
700
}
}
>
No results found for "
{
props
.
q
}
"
</
Typography
>
<
Typography
align
=
"center"
>
Try searching with less or different keywords.
</
Typography
>
</
Stack
>
</
Box
>
)
}
export
default
Search
;
Back
|
FazBrowse Home
|
New Git URL