Skip to content

Commit

Permalink
feat: search by hashtags
Browse files Browse the repository at this point in the history
  • Loading branch information
warmachine028 committed Dec 1, 2024
1 parent d0b7d50 commit 5a28043
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 260 deletions.
6 changes: 4 additions & 2 deletions client/src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export const getPosts = async (cursor, limit) => {
}
}

export const searchPosts = async (query) => {
export const searchPosts = async (query, cursor, limit) => {
try {
const { data } = await api.get('/posts/search', { params: { query } })
const { data } = await api.get('/posts/search', {
params: { query, cursor, limit }
})
return data
} catch (error) {
throw handleApiError(error)
Expand Down
11 changes: 11 additions & 0 deletions client/src/api/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ export const searchTags = async (q) => {
throw handleApiError(error)
}
}

export const searchPostsByTags = async (tag, cursor, limit) => {
try {
const { data } = await api.get(`/tags/posts/${tag}`, {
params: { cursor, limit }
})
return data
} catch (error) {
throw handleApiError(error)
}
}
15 changes: 13 additions & 2 deletions client/src/components/AccountMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@ import { Link } from 'react-router'
import { useTheme } from '@/hooks'
import { useState } from 'react'
import { UserAvatar } from '.'
import { useStore } from '@/store'

const AccountMenuItems = ({ handleClose, handleClick, open }) => {
const { user } = useUser()
const { user, sign } = useUser()
const { signOut } = useAuth()
const { openSnackbar } = useStore()

const logOut = () => {
try {
signOut()
openSnackbar('Logged in successfully')
} catch (error) {
openSnackbar(error.message, 'error')
}
}

return (
<>
Expand Down Expand Up @@ -52,7 +63,7 @@ const AccountMenuItems = ({ handleClose, handleClick, open }) => {
<ChevronRight fontSize="small" />
</ListItemIcon>
</MenuItem>
<MenuItem onClick={signOut}>
<MenuItem onClick={logOut}>
<ListItemIcon>
<Logout fontSize="small" />
</ListItemIcon>
Expand Down
28 changes: 9 additions & 19 deletions client/src/components/PostCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,13 @@ const EditCard = ({ post, setEditing }) => {
if (!validateInputs()) {
return
}
try {
updatePost({
...editedPost,
media: editedPost.media
? await convertToBase64(editedPost.media)
: editedPost.imageUrl
})
setEditing(false)
} catch (error) {
console.error(error)
}
updatePost({
...editedPost,
media: editedPost.media
? await convertToBase64(editedPost.media)
: editedPost.imageUrl
})
setEditing(false)
}

return (
Expand Down Expand Up @@ -333,15 +329,9 @@ const EditCard = ({ post, setEditing }) => {
const StaticCard = ({ post, setEditing }) => {
const { user } = useUser()
const navigate = useNavigate()
console.log(post)

return (
<Card
sx={{
position: 'relative',
cursor: 'pointer',
height: 1
}}
>
<Card sx={{ position: 'relative', cursor: 'pointer', height: 1, maxHeight:400 }}>
<CardHeader
avatar={
<UserAvatar
Expand Down
86 changes: 48 additions & 38 deletions client/src/components/TrendingTags.jsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
import { useTrendingTags } from '@/hooks'
import {
List,
ListItem,
ListItemText,
Typography,
Box,
Paper
Paper,
CircularProgress
} from '@mui/material'
import { Link, useNavigate } from 'react-router'
import { Link } from 'react-router'

const TrendingTags = ({ tags }) => {
const navigate = useNavigate()
const TrendingTags = () => {
const { data: tags, isLoading } = useTrendingTags()
return (
<Paper sx={{ p: 2 }} role="presentation" elevation={0}>
<Typography variant="h6" gutterBottom>
Trending Topics
</Typography>
<List sx={{ p: 0 }}>
{tags.map((tag) => (
<ListItem
key={tag.hashtag}
to={`/hashtag/${tag.hashtag}`}
sx={{
cursor: 'pointer',
':hover': {
bgcolor: 'primary.main',
transition: 'background-color 0.2s'
},
p: 0.5,
px: 1,
textDecoration: 'none',
color: 'inherit',
borderRadius: 1
}}
component={Link}
>
<ListItemText
primary={`#${tag.hashtag}`}
secondary={`${tag.count} posts`}
primaryTypographyProps={{
fontWeight: 'medium'
}}
secondaryTypographyProps={{
fontSize: '0.8rem',
color: 'text.muted'
}}
/>
</ListItem>
))}
</List>
{isLoading ? (
<CircularProgress size={20} />
) : (
<TagsList tags={tags} />
)}
</Paper>
)
}

const TagsList = ({ tags }) => {
return (
<List sx={{ p: 0 }}>
{tags.map((tag) => (
<ListItem
key={tag.hashtag}
to={`/hashtag/${tag.hashtag}`}
sx={{
cursor: 'pointer',
':hover': {
bgcolor: 'primary.main',
transition: 'background-color 0.2s'
},
p: 0.5,
px: 1,
textDecoration: 'none',
color: 'inherit',
borderRadius: 1
}}
component={Link}
>
<ListItemText
primary={`#${tag.hashtag}`}
secondary={`${tag.count} posts`}
primaryTypographyProps={{
fontWeight: 'medium'
}}
secondaryTypographyProps={{
fontSize: '0.8rem',
color: 'text.muted'
}}
/>
</ListItem>
))}
</List>
)
}
export default TrendingTags
14 changes: 7 additions & 7 deletions client/src/hooks/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export const useGetPosts = () =>
getNextPageParam: (lastPage) => lastPage.nextCursor
})

export const useSearchPosts = (query) =>
useInfiniteQuery({
queryKey: ['search-posts', query],
queryFn: ({ pageParam }) => searchPosts(query, pageParam, 6),
getNextPageParam: (lastPage) => lastPage.nextCursor
})

export const useGetPost = (id) =>
useQuery({
queryKey: ['post', id],
Expand Down Expand Up @@ -163,13 +170,6 @@ export const useDeletePost = () => {
})
}

export const useSearchPosts = () => {
return useQuery({
queryKey: ['searchPosts'],
queryFn: () => searchPosts
})
}

export const useRefresh = () => {
const queryClient = useQueryClient()
const [refreshing, setRefreshing] = useState(false)
Expand Down
18 changes: 16 additions & 2 deletions client/src/hooks/tags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useQuery } from '@tanstack/react-query'
import { searchTags } from '@/api'
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
import { getTrendingTags, searchPostsByTags, searchTags } from '@/api'

export const useSearchTags = (input) =>
useQuery({
Expand All @@ -9,3 +9,17 @@ export const useSearchTags = (input) =>
staleTime: 1000 * 60 * 5,
gcTime: 1000 * 60 * 30
})

export const useTrendingTags = () =>
useQuery({
queryKey: ['tags'],
queryFn: getTrendingTags,
staleTime: Infinity
})

export const useSearchPostsByTag = (tag) =>
useInfiniteQuery({
queryKey: ['search-tags', tag],
queryFn: ({ pageParam }) => searchPostsByTags(tag, pageParam, 6),
getNextPageParam: (lastPage) => lastPage.nextCursor
})
4 changes: 3 additions & 1 deletion client/src/pages/LogIn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LockOutlined } from '@mui/icons-material'
import { useSignIn } from '@clerk/clerk-react'
import { OAuthButtons } from '@/components'
import { useState } from 'react'
import { useStore } from '@/store'

const Form = () => {
const initialState = { email: '', password: '' }
Expand All @@ -25,7 +26,7 @@ const Form = () => {

const handleChange = (e) =>
setFormData({ ...formData, [e.target.name]: e.target.value })

const { openSnackbar } = useStore()
const handleSubmit = async (event) => {
event.preventDefault()
setError('')
Expand All @@ -40,6 +41,7 @@ const Form = () => {

if (result.status === 'complete') {
await setActive({ session: result.createdSessionId })
openSnackbar('Logged in successfully')
navigate('/')
} else {
console.error('Sign-in failed', result)
Expand Down
2 changes: 2 additions & 0 deletions client/src/pages/Post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const PostCard = ({ post, isLoading, error }) => {
if (error?.message === 'API Error: Post not found') {
navigate('/not-found')
}
document.title = `Memories | ${post.title}`
return (
<Card sx={{ width: 1 }}>
<CardMedia component="img" image={post.imageUrl} alt="Post cover" />
Expand Down Expand Up @@ -149,6 +150,7 @@ const Top3Reactions = ({ post }) => {
const Post = () => {
const { id } = useParams()
const { data: post, isLoading, error } = useGetPost(id)

return (
<Container sx={{ py: { xs: 2, md: 4 }, mb: 10 }} maxWidth="xl">
<Grid container spacing={3}>
Expand Down
29 changes: 3 additions & 26 deletions client/src/pages/Posts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,15 @@ const PostGrid = () => {
}

const Posts = () => {
// const { data: tags } = useGetTrendingTags()
const tags = [
{
hashtag: 'media',
count: 5
},
{
hashtag: 'post',
count: 5
},
{
hashtag: 'social',
count: 5
},
{
hashtag: 'love',
count: 3
},
{
hashtag: 'chocolates',
count: 2
}
]
return (
<Container sx={{ py: { xs: 2, md: 4 }, height: '100vh' }} maxWidth="xl">
<Grid container spacing={3}>
<Grid
container
size={{ xs: 12, md: 8, xl: 9 }}
overflow="auto"
height={'calc(100vh - 100px)'}
height={'calc(100vh - 170px)'}
minHeight={400}
sx={{
'&::-webkit-scrollbar': { display: 'none' },
scrollbarWidth: 'none',
Expand All @@ -112,8 +90,7 @@ const Posts = () => {
</Paper>
</Grid>
<Grid size={{ xs: 12 }}>
{/* <SearchForm /> */}
<TrendingTags tags={tags} />
<TrendingTags />
</Grid>
</Grid>
</Grid>
Expand Down
Loading

0 comments on commit 5a28043

Please sign in to comment.