-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* couple of icons * pre-mature working version, it updates, but also reloads? * no more reload but semi-proper update * Search results are from MongoDB now; added a simple ArticleView for testing purposes; issue: clicking a search result redirects to home page (??) * formatted search results * Woof-16: Removed sample search test dependency after connection to MongoDB * edit full text box * working delete functionality * fixed minor errors --------- Co-authored-by: Parsa Hajipour <[email protected]> Co-authored-by: samanthaj9 <[email protected]> Co-authored-by: laurenrm3 <[email protected]> Co-authored-by: Nathan Zhang <[email protected]>
- Loading branch information
1 parent
0e7a39e
commit def81c9
Showing
20 changed files
with
1,039 additions
and
291 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// apiUtils.ts (or any other utility file) | ||
import { mutate } from 'swr'; | ||
|
||
export interface FormData { | ||
title: string; | ||
content: string; | ||
// created_date: Date; | ||
// updated_date: Date; | ||
sections: string[]; | ||
pinned_sections: string[]; | ||
quick_link: boolean; | ||
image_url: string; | ||
} | ||
|
||
export const putData = async ( | ||
form: FormData, | ||
id: string | string[] | undefined, | ||
contentType: string, | ||
setMessage: React.Dispatch<React.SetStateAction<string>>, | ||
router: any | ||
) => { | ||
try { | ||
const res = await fetch(`/api/articles/${id}`, { | ||
method: 'PUT', | ||
headers: { | ||
Accept: contentType, | ||
'Content-Type': contentType, | ||
}, | ||
body: JSON.stringify(form), | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error(res.status.toString()); | ||
} | ||
|
||
const { data } = await res.json(); | ||
|
||
mutate(`/api/articles/${id}`, data, false); // Update the local data without revalidation | ||
|
||
} catch (error) { | ||
setMessage('Failed to update article'); | ||
} | ||
}; | ||
|
||
|
||
|
||
export const postData = async ( | ||
form: FormData, | ||
id: string, | ||
contentType: string, | ||
setMessage: React.Dispatch<React.SetStateAction<string>>, | ||
router: any | ||
) => { | ||
|
||
try { | ||
const res = await fetch('/api/articles', { | ||
method: 'POST', | ||
headers: { | ||
Accept: contentType, | ||
'Content-Type': contentType, | ||
}, | ||
body: JSON.stringify(form), | ||
}); | ||
|
||
// Throw error with status code in case Fetch API req failed | ||
if (!res.ok) { | ||
throw new Error(res.status.toString()); | ||
} | ||
|
||
router.push('/'); | ||
} catch (error) { | ||
setMessage('Failed to add article'); | ||
} | ||
}; | ||
|
||
|
||
export const deleteData = async ( | ||
id: string, | ||
setMessage: React.Dispatch<React.SetStateAction<string>>, | ||
router: any | ||
) => { | ||
try { | ||
const res = await fetch(`/api/articles/${id}`, { | ||
method: 'DELETE', | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error(res.status.toString()); | ||
} | ||
|
||
// Remove the deleted item from SWR cache | ||
mutate(`/api/articles/${id}`, null, false); | ||
|
||
setMessage('Article deleted successfully'); | ||
|
||
} catch (error) { | ||
setMessage('Failed to delete article'); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,56 +1,61 @@ | ||
import Link from "next/link"; | ||
import Link from 'next/link'; | ||
import { useSearch } from '../context/SearchContext'; | ||
|
||
type Props = { | ||
section?: string; | ||
color?: string; | ||
section?: string; | ||
color?: string; | ||
}; | ||
|
||
export const SearchResults = ({ | ||
section = '', | ||
color = 'accent-purple', | ||
}: Props) => { | ||
const { searchTerm, filteredArticles } = useSearch(); | ||
section = '', | ||
color = 'accent-purple', | ||
}: Props) => { | ||
const { searchTerm, filteredArticles } = useSearch(); | ||
|
||
let sectionArticles = filteredArticles; | ||
if (section) { | ||
sectionArticles = filteredArticles?.filter((article) => article.sections?.includes(section)) || []; | ||
} | ||
let sectionArticles = filteredArticles; | ||
if (section) { | ||
sectionArticles = | ||
filteredArticles?.filter((article) => | ||
article.sections?.includes(section), | ||
) || []; | ||
} | ||
|
||
return ( | ||
<> | ||
{searchTerm && ( | ||
<div className="rounded-2xl border-black border-t-[0.5vmin] border-l-[0.5vmin] border-b-[0.5vmin] border-r-[0.5vmin] mb-[24px] w-fill"> | ||
<div className={`border-black border-b-[0.5vmin] bg-${color} h-[6vmin] w-full rounded-t-lg items-center pl-[3vmin] flex p-[1vmin]`}> | ||
<p className="font-semibold text-center text-[3vmin] justify-center text-white bg-transparent quick-links pt-[1vmin]"> | ||
Search results for "{ searchTerm }" in {section || 'All'} | ||
</p> | ||
</div> | ||
{sectionArticles.length > 0 && ( | ||
<div className="py-[1.2vmin] px-[3.7vmin] rounded-b-lg bg-white max-h-[400px] overflow-auto"> | ||
{sectionArticles.map((article) => ( | ||
<div key={article._id.$oid} className="search-results"> | ||
<Link | ||
href={{ | ||
pathname: '/[id]', | ||
query: { id: article._id.$oid }, | ||
}} | ||
> | ||
{article.title} | ||
</Link> | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
{sectionArticles.length == 0 && ( | ||
<div className="py-[1.2vmin] px-[3.7vmin] rounded-b-lg bg-white"> | ||
<div className="no-results"> | ||
No articles found. | ||
</div> | ||
</div> | ||
)} | ||
return ( | ||
<> | ||
{searchTerm && ( | ||
<div className="rounded-2xl border-black border-t-[0.5vmin] border-l-[0.5vmin] border-b-[0.5vmin] border-r-[0.5vmin] mb-[24px] w-fill"> | ||
<div | ||
className={`border-black border-b-[0.5vmin] bg-${color} h-[6vmin] w-full rounded-t-lg items-center pl-[3vmin] flex p-[1vmin]`} | ||
> | ||
<p className="font-semibold text-center text-[3vmin] justify-center text-white bg-transparent quick-links pt-[1vmin]"> | ||
Search results for "{searchTerm}" in {section || 'All'} | ||
</p> | ||
</div> | ||
{sectionArticles.length > 0 && ( | ||
<div className="py-[1.2vmin] px-[3.7vmin] rounded-b-lg bg-white max-h-[400px] overflow-auto"> | ||
{sectionArticles.map((article) => ( | ||
<div key={article._id.$oid} className="search-results"> | ||
<Link | ||
href={{ | ||
pathname: '/[id]', | ||
query: { id: article._id.$oid }, | ||
}} | ||
style={{ color: 'blue', fontWeight: 'bold' }} | ||
> | ||
{article.title} | ||
</Link> | ||
<div>{article.content}</div> | ||
</div> | ||
)} | ||
</> | ||
) | ||
} | ||
))} | ||
</div> | ||
)} | ||
{sectionArticles.length == 0 && ( | ||
<div className="py-[1.2vmin] px-[3.7vmin] rounded-b-lg bg-white"> | ||
<div className="no-results">No articles found.</div> | ||
</div> | ||
)} | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; |
Oops, something went wrong.