Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ Notification search #2164

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions components/NotificationsTable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* globals DOMPurify, marked: false */

import { useState, useEffect } from 'react'
import { useState, useEffect, useCallback } from 'react'
import upperFirst from 'lodash/upperFirst'
import { debounce } from 'lodash'
import Table from './Table'
import Collapse from './Collapse'
import { formatDateTime, prettyId } from '../lib/utils'
Expand All @@ -11,12 +12,37 @@ export default function NotificationsTable({
numUnviewed,
markViewed,
markAllViewed,
setSearchTerm,
searchTerm,
}) {
const [immediateSearchTerm, setImmediateSearchTerm] = useState(null)

const delaySearch = useCallback(
debounce((term) => setSearchTerm(term), 300),
[]
)

useEffect(() => {
if (searchTerm !== null) return
setImmediateSearchTerm(null)
}, [searchTerm])

if (!messages) return null

const headingContent = (
<>
<span className="pull-left">Message Details</span>
<div className="form-inline">
<div className="form-group">
<label htmlFor="message-subject-search">Search Subject:</label>
<input
id="message-subject-search"
className="form-control input-sm ml-2"
value={immediateSearchTerm ?? ''}
onChange={(e) => {
setImmediateSearchTerm(e.target.value)
delaySearch(e.target.value)
}}
/>
</div>
<span className="pull-right">
<button
className="btn btn-xs"
Expand All @@ -27,7 +53,7 @@ export default function NotificationsTable({
Mark All as Read
</button>
</span>
</>
</div>
)

return (
Expand Down
34 changes: 34 additions & 0 deletions pages/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function Notifications({ appContext }) {
const [page, setPage] = useState(1)
const [error, setError] = useState(null)
const [shouldRefresh, setShouldRefresh] = useState(false)
const [searchTerm, setSearchTerm] = useState(null)
const router = useRouter()
const { setUnreadNotificationCount, decrementNotificationCount } = useContext(UserContext)
const { setBannerHidden } = appContext
Expand Down Expand Up @@ -93,6 +94,7 @@ export default function Notifications({ appContext }) {

setToEmail(router.query.email || user.profile.preferredEmail || user.profile.emails[0])
setPage(1)
setSearchTerm(null)
}, [user?.id, router.isReady, router.query.email])

useEffect(() => {
Expand Down Expand Up @@ -147,11 +149,13 @@ export default function Notifications({ appContext }) {
if (!accessToken || !toEmail) return

setError(null)
const cleanSearchTerm = searchTerm?.trim()

api
.get(
'/messages',
{
...(cleanSearchTerm?.length && { subject: `${cleanSearchTerm}.*` }),
to: toEmail,
limit: pageSize,
offset: pageSize * (page - 1),
Expand All @@ -168,6 +172,34 @@ export default function Notifications({ appContext }) {
})
}, [accessToken, toEmail, page, shouldRefresh])

useEffect(() => {
const cleanSearchTerm = searchTerm?.trim()
if (!cleanSearchTerm?.length) {
if (searchTerm === '') {
// search term cleared
setPage(1)
setShouldRefresh((refresh) => !refresh)
}
return
}
api
.get(
'/messages',
{ subject: `${cleanSearchTerm}.*`, to: toEmail, limit: pageSize },

{ accessToken }
)
.then((apiRes) => {
setMessages(apiRes.messages ?? [])
setCount(apiRes.count ?? 0)
setPage(1)
})
.catch((apiError) => {
setError(apiError)
setMessages(null)
})
}, [searchTerm])

return (
<div>
<Head>
Expand Down Expand Up @@ -231,6 +263,8 @@ export default function Notifications({ appContext }) {
numUnviewed={unviewedCounts?.[toEmail] ?? 0}
markViewed={markViewed}
markAllViewed={markAllViewed}
setSearchTerm={setSearchTerm}
searchTerm={searchTerm}
/>

<PaginationLinks
Expand Down