Add ability to search for users

This commit is contained in:
CuriousCorrelation authored and Tom Moor committed 2022-07-17 12:22:25 +01:00
1 parent c01312db24
commit ddab370640
3 files changed
+38 -10

No files matched your search

+16 -8
View File
@@ -22,7 +22,7 @@ type Props = {
selectedPrefix?: string;
className?: string;
onSelect: (key: string | null | undefined) => void;
searchable?: boolean;
search?: (query: string) => Promise<TFilterOption[]>;
paginateFetch?: (
options: PaginatedItem
) => Promise<PaginatedItem[] | undefined>;
@@ -35,7 +35,7 @@ const FilterOptions = ({
selectedPrefix = "",
className,
onSelect,
searchable,
search,
paginateFetch,
}: Props) => {
const menu = useMenuState({
@@ -62,14 +62,22 @@ const FilterOptions = ({
// Simple case-insensitive filter to
// check if text appears in any author's name.
const handleFilter = React.useCallback(
(event) => {
async (event) => {
const { value } = event.target;
if (value) {
const search = options.filter((option) =>
const res = options.filter((option) =>
option.label.toLowerCase().includes(value.toLowerCase())
);
setFilteredOptions(search);
if (search) {
const more = await search(value);
const missing = more.filter(
(item) => !res.map((r) => r.key).includes(item.key)
);
setFilteredOptions([...missing, ...res]);
} else {
setFilteredOptions(res);
}
} else {
// Clears filter options cache.
// This part fires off when search term is "".
@@ -79,7 +87,7 @@ const FilterOptions = ({
clearFilter();
}
},
[clearFilter, options]
[clearFilter, options, search]
);
return (
@@ -92,8 +100,8 @@ const FilterOptions = ({
)}
</MenuButton>
<ContextMenu aria-label={defaultLabel} {...menu}>
{searchable && <StyledInputSearch onChange={handleFilter} />}
{searchable && <br />}
{search && <StyledInputSearch onChange={handleFilter} />}
{search && <br />}
<PaginatedList
items={filteredOptions}
fetch={paginateFetch}
+14 -2
View File
@@ -34,7 +34,19 @@ function UserFilter(props: Props) {
},
...userOptions,
];
}, [users.all, t]);
}, [users.unorderedData, t]);
const search = React.useCallback(
async (query: string) => {
const res = await users.find(query);
return res.map((user) => ({
key: user.id,
id: user.id,
label: user.name,
}));
},
[users]
);
return (
<FilterOptions
@@ -43,7 +55,7 @@ function UserFilter(props: Props) {
onSelect={onSelect}
defaultLabel={t("Any author")}
selectedPrefix={`${t("Author")}:`}
searchable
search={search}
paginateFetch={users.fetchPage}
/>
);
+8
View File
@@ -157,6 +157,14 @@ export default class UsersStore extends BaseStore<User> {
return res.data;
};
@action
async find(name: string): Promise<User[]> {
const res = await client.post(`/users.list`, {
query: name,
});
return res.data;
}
@action
async delete(user: User, options: Record<string, any> = {}) {
super.delete(user, options);