generated from freeCodeCamp/template
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: sidemt <25644062+sidemt@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React, { useState } from "react"; | ||
import { | ||
IconButton, | ||
Modal, | ||
ModalContent, | ||
ModalOverlay, | ||
Input, | ||
useDisclosure, | ||
InputGroup, | ||
InputLeftElement, | ||
Text, | ||
Card, | ||
CardBody, | ||
} from "@chakra-ui/react"; | ||
import { SearchIcon } from "@chakra-ui/icons"; | ||
import { getAllPosts } from "@/lib/posts"; | ||
import { isEditor } from "@/lib/current-user"; | ||
import NextLink from "next/link"; | ||
|
||
const PostSearch = ({ user }) => { | ||
const [posts, setPosts] = useState([]); | ||
|
||
const { isOpen, onOpen, onClose } = useDisclosure(); | ||
|
||
const search = async (query) => { | ||
const result = await getAllPosts(user.jwt, { | ||
publicationState: "preview", | ||
fields: ["title", "id"], | ||
populate: ["author"], | ||
filters: { | ||
title: { | ||
$containsi: query, | ||
}, | ||
// only show posts of that user if they are not an editor | ||
...(isEditor(user) ? {} : { author: { id: { $eq: user.id } } }), | ||
}, | ||
pagination: { | ||
pageSize: 5, | ||
}, | ||
}); | ||
|
||
setPosts(result.data); | ||
}; | ||
|
||
return ( | ||
<> | ||
<IconButton onClick={onOpen} variant={"ghost"}> | ||
<SearchIcon /> | ||
</IconButton> | ||
|
||
<Modal isOpen={isOpen} onClose={onClose}> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<InputGroup> | ||
<InputLeftElement pointerEvents={"none"} className="InputLeft"> | ||
<SearchIcon color={"gray.300"} /> | ||
</InputLeftElement> | ||
<Input | ||
type="text" | ||
placeholder="Search" | ||
onChange={(query) => search(query.target.value)} | ||
size="lg" | ||
/> | ||
</InputGroup> | ||
{posts.map((post) => ( | ||
<Card as={NextLink} href={`/posts/${post.id}`} key={post.id}> | ||
<CardBody> | ||
<Text>{post.attributes.title}</Text> | ||
</CardBody> | ||
</Card> | ||
))} | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
|
||
export default PostSearch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,8 @@ | |
width: 40%; | ||
height: 40%; | ||
} | ||
|
||
.InputLeft { | ||
top: 3px !important; | ||
left: 3px !important; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.