-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6200a9
commit 0ce8b52
Showing
8 changed files
with
300 additions
and
20 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
74 changes: 74 additions & 0 deletions
74
frontend/components/admin/mentions-overview/MentionsOverview.tsx
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,74 @@ | ||
// SPDX-FileCopyrightText: 2024 Ewan Cahen (Netherlands eScience Center) <e.cahen@esciencecenter.nl> | ||
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {useEffect, useState} from 'react' | ||
import MentionsOverviewList from '~/components/admin/mentions-overview/MentionsOverviewList' | ||
import {extractSearchTerm, SearchTermInfo} from '~/components/software/edit/mentions/utils' | ||
import Searchbox from '~/components/search/Searchbox' | ||
import Pagination from '~/components/pagination/Pagination' | ||
import usePaginationWithSearch from '~/utils/usePaginationWithSearch' | ||
import {extractCountFromHeader} from '~/utils/extractCountFromHeader' | ||
import {paginationUrlParams} from '~/utils/postgrestUrl' | ||
|
||
const uuidRegex = /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/ | ||
|
||
export default function MentionsOverview() { | ||
const [mentionList, setMentionList] = useState([]) | ||
const {searchFor, page, rows, setCount} = usePaginationWithSearch('Find mentions') | ||
|
||
const sanitisedSearch = sanitiseSearch(searchFor) | ||
useEffect(() => { | ||
fetchAndSetMentionList(sanitisedSearch) | ||
}, [sanitisedSearch, page, rows]) | ||
|
||
function fetchAndSetMentionList(sanitisedSearch: undefined | string): void { | ||
fetch(`/api/v1/mention?${createSearchQueryParameters(sanitisedSearch)}&${paginationUrlParams({rows, page})}`, { | ||
headers: { | ||
'Prefer': 'count=exact' | ||
} | ||
}) | ||
.then(res => { | ||
setCount(extractCountFromHeader(res.headers) ?? 0) | ||
return res.json() | ||
}) | ||
.then(arr => setMentionList(arr)) | ||
} | ||
|
||
function createSearchQueryParameters(sanitisedSearch: undefined | string): string { | ||
if (sanitisedSearch === undefined) { | ||
return '' | ||
} | ||
|
||
if (uuidRegex.test(sanitisedSearch.trim())) { | ||
return `id=eq.${sanitisedSearch.trim().toLowerCase()}` | ||
} | ||
|
||
const searchTypeTerm: SearchTermInfo = extractSearchTerm(sanitisedSearch) | ||
const termEscaped = encodeURIComponent(sanitisedSearch) | ||
if (searchTypeTerm.type === 'doi') { | ||
return `doi=eq.${termEscaped}` | ||
} | ||
return `or=(title.ilike.*${termEscaped}*,authors.ilike.*${termEscaped}*,journal.ilike.*${termEscaped}*,url.ilike.*${termEscaped}*,note.ilike.*${termEscaped}*,external_id.ilike.*${termEscaped}*)` | ||
} | ||
|
||
function sanitiseSearch(search: string): string | undefined { | ||
if (!search || search.length < 3 ) { | ||
return undefined | ||
} | ||
return search | ||
} | ||
|
||
return ( | ||
<section className="flex-1"> | ||
<div className="flex flex-wrap items-center justify-end"> | ||
<Searchbox/> | ||
<Pagination/> | ||
</div> | ||
<div> | ||
<MentionsOverviewList list={mentionList} onUpdate={() => fetchAndSetMentionList(sanitisedSearch)}></MentionsOverviewList> | ||
</div> | ||
</section> | ||
) | ||
} |
79 changes: 79 additions & 0 deletions
79
frontend/components/admin/mentions-overview/MentionsOverviewList.tsx
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,79 @@ | ||
// SPDX-FileCopyrightText: 2024 Ewan Cahen (Netherlands eScience Center) <e.cahen@esciencecenter.nl> | ||
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {useState} from 'react' | ||
import List from '@mui/material/List' | ||
import ListItem from '@mui/material/ListItem' | ||
import EditIcon from '@mui/icons-material/Edit' | ||
import IconButton from '@mui/material/IconButton' | ||
import {MentionItemProps} from '~/types/Mention' | ||
import MentionViewItem from '~/components/mention/MentionViewItem' | ||
import EditMentionModal from '~/components/mention/EditMentionModal' | ||
import {createJsonHeaders} from '~/utils/fetchHelpers' | ||
import {useSession} from '~/auth' | ||
import useSnackbar from '~/components/snackbar/useSnackbar' | ||
import usePaginationWithSearch from '~/utils/usePaginationWithSearch' | ||
|
||
function leaveOutSomeFieldsReplacer(key: string, value: any) { | ||
if (key === 'id' || key === 'doi_registration_date' || key === 'created_at' || key === 'updated_at') { | ||
return undefined | ||
} else { | ||
return value | ||
} | ||
} | ||
|
||
export default function MentionsOverviewList({list, onUpdate}: { list: MentionItemProps[], onUpdate: Function }) { | ||
const [modalOpen, setModalOpen] = useState<boolean>(false) | ||
const [mentionToEdit, setMentionToEdit] = useState<MentionItemProps | undefined>(undefined) | ||
const {token} = useSession() | ||
const {showErrorMessage} = useSnackbar() | ||
const {page, rows} = usePaginationWithSearch('Find mentions') | ||
|
||
async function updateMention(data: MentionItemProps) { | ||
const id = data.id as string | ||
const body = JSON.stringify(data, leaveOutSomeFieldsReplacer) | ||
const resp = await fetch(`/api/v1/mention?id=eq.${id}`, { | ||
method: 'PATCH', | ||
body: body, | ||
headers: createJsonHeaders(token) | ||
}) | ||
if (resp.ok) { | ||
setModalOpen(false) | ||
onUpdate() | ||
} else { | ||
showErrorMessage(`got status ${resp.status}:${await resp.text()}`) | ||
} | ||
} | ||
|
||
if (list.length === 0) { | ||
return 'No mentions to show' | ||
|
||
} | ||
|
||
return ( | ||
<> | ||
<List> | ||
{list.map((mention, idx) => { | ||
return ( | ||
<ListItem key={mention.id} secondaryAction={ | ||
<IconButton onClick={() => {setModalOpen(true); setMentionToEdit(mention)}} ><EditIcon></EditIcon></IconButton> | ||
}> | ||
<MentionViewItem item={mention} pos={page * rows + idx + 1}></MentionViewItem> | ||
</ListItem> | ||
) | ||
})} | ||
</List> | ||
<EditMentionModal | ||
title={mentionToEdit?.id as string ?? 'undefined'} | ||
open={modalOpen} | ||
pos={undefined} //why does this exist? | ||
item={mentionToEdit} | ||
onCancel={() => setModalOpen(false)} | ||
onSubmit={({data}) => {updateMention(data)}} | ||
/> | ||
</> | ||
) | ||
}; | ||
|
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
Oops, something went wrong.