Skip to content

Commit

Permalink
feature: Sorting & finding by username (ID) admin interface (#1778)
Browse files Browse the repository at this point in the history
Closes #1615
- Auto-focuses on filter tab
- Able to sort / filter by username (ID)
  • Loading branch information
owenduncansnobel committed Feb 21, 2023
1 parent c5a75d7 commit 96feac0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
8 changes: 4 additions & 4 deletions website/src/components/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
} from "@tanstack/react-table";
import { Filter } from "lucide-react";
import { useTranslation } from "next-i18next";
import { ChangeEvent, ReactNode, useState } from "react";
import { ChangeEvent, ReactNode, useRef, useState } from "react";
import { useDebouncedCallback } from "use-debounce";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -198,13 +198,13 @@ const FilterModal = ({
value: string;
}) => {
const { isOpen, onOpen, onClose } = useDisclosure();

const initialFocusRef = useRef();
const handleInputChange = useDebouncedCallback((e: ChangeEvent<HTMLInputElement>) => {
onChange(e.target.value);
}, 500);

return (
<Popover isOpen={isOpen} onOpen={onOpen} onClose={onClose}>
<Popover isOpen={isOpen} onOpen={onOpen} initialFocusRef={initialFocusRef} onClose={onClose}>
<PopoverTrigger>
<Button variant={"unstyled"} ml="2">
<Filter size="1em"></Filter>
Expand All @@ -216,7 +216,7 @@ const FilterModal = ({
<PopoverBody mt="4">
<FormControl>
<FormLabel>{label}</FormLabel>
<Input onChange={handleInputChange} defaultValue={value}></Input>
<Input ref={initialFocusRef} onChange={handleInputChange} autoFocus defaultValue={value}></Input>
</FormControl>
</PopoverBody>
</PopoverContent>
Expand Down
26 changes: 20 additions & 6 deletions website/src/components/UserTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ const columns: DataTableColumnDef<User>[] = [
columnHelper.accessor("user_id", {
header: "ID",
}),
columnHelper.accessor("id", {
header: "Auth ID",
}),
{
...columnHelper.accessor("id", {
header: "Auth ID",
}),
filterable: true,
},
columnHelper.accessor("auth_method", {
header: "Auth Method",
}),
Expand Down Expand Up @@ -47,17 +50,28 @@ const columns: DataTableColumnDef<User>[] = [
export const UserTable = memo(function UserTable() {
const { pagination, resetCursor, toNextPage, toPreviousPage } = useCursorPagination();
const [filterValues, setFilterValues] = useState<FilterItem[]>([]);

const handleFilterValuesChange = (values: FilterItem[]) => {
setFilterValues(values);
const last = values.pop();
if (last) {
setFilterValues([last]);
}
resetCursor();
};

// Fetch and save the users.
// This follows useSWR's recommendation for simple pagination:
// https://swr.vercel.app/docs/pagination#when-to-use-useswr
const display_name = filterValues.find((value) => value.id === "display_name")?.value ?? "";

const filterValue = filterValues.find((value) => value.id === filterValues[filterValues.length - 1]?.id)?.value ?? "";
const { data, error } = useSWR<FetchUsersResponse<User>>(
`/api/admin/users?direction=${pagination.direction}&cursor=${pagination.cursor}&searchDisplayName=${display_name}&sortKey=display_name`,
`/api/admin/users?direction=${pagination.direction}&cursor=${
pagination.cursor
}&searchDisplayName=${filterValue}&sortKey=${
filterValues[filterValues.length - 1]?.id === "id"
? "username"
: filterValues[filterValues.length - 1]?.id || "display_name"
}`,
get,
{
keepPreviousData: true,
Expand Down

0 comments on commit 96feac0

Please sign in to comment.