diff --git a/app/create-prompt/page.jsx b/app/create-prompt/page.jsx index 0fcab0c..59d1054 100644 --- a/app/create-prompt/page.jsx +++ b/app/create-prompt/page.jsx @@ -38,9 +38,7 @@ const CreatePrompt = () => { setSubmitting(false); //do this regardless of if the api call works } } - - - + return (
{ + const searchParams = useSearchParams(); + const userName = searchParams.get("name"); + + const [userPosts, setUserPosts] = useState([]); + + useEffect(() => { + const fetchPosts = async () => { + const response = await fetch(`/api/users/${params?.id}/posts`); + const data = await response.json(); + + setUserPosts(data); + }; + + if (params?.id) fetchPosts(); + }, [params.id]); + + return ( + + ); +}; + +export default UserProfile; \ No newline at end of file diff --git a/app/profile/loading.jsx b/app/profile/loading.jsx new file mode 100644 index 0000000..41825e3 --- /dev/null +++ b/app/profile/loading.jsx @@ -0,0 +1,17 @@ +import Image from "next/image"; + +const Loading = () => { + return ( +
+ loader +
+ ); +}; + +export default Loading; \ No newline at end of file diff --git a/components/Feed.jsx b/components/Feed.jsx index c5f7f2a..5d62775 100644 --- a/components/Feed.jsx +++ b/components/Feed.jsx @@ -19,43 +19,78 @@ const PromptCardList = ({ data, handleTagClick }) => { } const Feed = () => { + const [searchedResults, setSearchedResults] = useState([]); + const [searchTimeout, setSearchTimeout] = useState(null); const [searchText, setSearchText] = useState(''); - const [posts, setPosts] = useState([]); + const [allPosts, setAllPosts] = useState([]); - const handleSearchChange = (e) => { + const handleTagClick = (tagName) => { + setSearchText(tagName); + + const searchResult = filterPrompts(tagName); + setSearchedResults(searchResult); } - useEffect(() => { - const fetchPosts = async () => { - const response = await fetch('/api/prompt'); - const data = await response.json(); - setPosts(data); - } + const fetchPosts = async () => { + const response = await fetch('/api/prompt'); + const data = await response.json(); + setAllPosts(data); + } + + useEffect(() => { fetchPosts(); }, []); + const filterPrompts = (searchtext) => { + const regex = new RegExp(searchtext, "i"); // 'i' flag for case-insensitive search + return allPosts.filter( + (item) => + regex.test(item.creator.username) || + regex.test(item.tag) || + regex.test(item.prompt) + ); + }; + const handleSearchChange = (e) => { + clearTimeout(searchTimeout); + setSearchText(e.target.value); + + // debounce method + setSearchTimeout( + setTimeout(() => { + const searchResult = filterPrompts(e.target.value); + setSearchedResults(searchResult); + }, 500) + ); + }; + + return ( -
- - + + - {}} - /> + {/* All Prompts */} + {searchText ? ( + + ) : ( + + )}
- ) -} + ); +}; export default Feed \ No newline at end of file diff --git a/components/PromptCard.jsx b/components/PromptCard.jsx index 2037b3b..402fb48 100644 --- a/components/PromptCard.jsx +++ b/components/PromptCard.jsx @@ -19,6 +19,10 @@ const PromptCard = ({ post, handleTagClick, handleEdit, handleDelete }) => { setTimeout(() => setCopied(""), 3000); } + const handleClick = () => { + handleTagClick(); + } + return (
diff --git a/public/assets/icons/copy.svg b/public/icons/copy.svg similarity index 100% rename from public/assets/icons/copy.svg rename to public/icons/copy.svg diff --git a/public/assets/icons/link.svg b/public/icons/link.svg similarity index 100% rename from public/assets/icons/link.svg rename to public/icons/link.svg diff --git a/public/assets/icons/loader.svg b/public/icons/loader.svg similarity index 100% rename from public/assets/icons/loader.svg rename to public/icons/loader.svg diff --git a/public/assets/icons/menu.svg b/public/icons/menu.svg similarity index 100% rename from public/assets/icons/menu.svg rename to public/icons/menu.svg diff --git a/public/assets/icons/tick.svg b/public/icons/tick.svg similarity index 100% rename from public/assets/icons/tick.svg rename to public/icons/tick.svg