Access lists basics
This commit is contained in:
19
frontend/src/api/npm/getAccessLists.ts
Normal file
19
frontend/src/api/npm/getAccessLists.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import * as api from "./base";
|
||||
import { AccessListsResponse } from "./responseTypes";
|
||||
|
||||
export async function getAccessLists(
|
||||
offset = 0,
|
||||
limit = 10,
|
||||
sort?: string,
|
||||
filters?: { [key: string]: string },
|
||||
abortController?: AbortController,
|
||||
): Promise<AccessListsResponse> {
|
||||
const { result } = await api.get(
|
||||
{
|
||||
url: "access-lists",
|
||||
params: { limit, offset, sort, ...filters },
|
||||
},
|
||||
abortController,
|
||||
);
|
||||
return result;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
export * from "./createCertificateAuthority";
|
||||
export * from "./createDNSProvider";
|
||||
export * from "./createUser";
|
||||
export * from "./getAccessLists";
|
||||
export * from "./getCertificateAuthorities";
|
||||
export * from "./getCertificateAuthority";
|
||||
export * from "./getCertificates";
|
||||
|
@ -38,6 +38,15 @@ export interface Setting {
|
||||
value: any;
|
||||
}
|
||||
|
||||
export interface AccessList {
|
||||
id: number;
|
||||
createdOn: number;
|
||||
modifiedOn: number;
|
||||
userId: number;
|
||||
name: string;
|
||||
meta: any;
|
||||
}
|
||||
|
||||
// TODO: copy pasta not right
|
||||
export interface Certificate {
|
||||
id: number;
|
||||
|
@ -1,4 +1,5 @@
|
||||
import {
|
||||
AccessList,
|
||||
Certificate,
|
||||
CertificateAuthority,
|
||||
DNSProvider,
|
||||
@ -34,6 +35,10 @@ export interface SettingsResponse extends BaseResponse {
|
||||
items: Setting[];
|
||||
}
|
||||
|
||||
export interface AccessListsResponse extends BaseResponse {
|
||||
items: AccessList[];
|
||||
}
|
||||
|
||||
export interface CertificatesResponse extends BaseResponse {
|
||||
items: Certificate[];
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
export * from "./useAccessLists";
|
||||
export * from "./useCertificateAuthorities";
|
||||
export * from "./useCertificateAuthority";
|
||||
export * from "./useCertificates";
|
||||
|
41
frontend/src/hooks/useAccessLists.ts
Normal file
41
frontend/src/hooks/useAccessLists.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import {
|
||||
getAccessLists,
|
||||
AccessListsResponse,
|
||||
tableSortToAPI,
|
||||
tableFiltersToAPI,
|
||||
} from "api/npm";
|
||||
import { useQuery } from "react-query";
|
||||
|
||||
const fetchAccessLists = (
|
||||
offset = 0,
|
||||
limit = 10,
|
||||
sortBy?: any,
|
||||
filters?: any,
|
||||
) => {
|
||||
return getAccessLists(
|
||||
offset,
|
||||
limit,
|
||||
tableSortToAPI(sortBy),
|
||||
tableFiltersToAPI(filters),
|
||||
);
|
||||
};
|
||||
|
||||
const useAccessLists = (
|
||||
offset = 0,
|
||||
limit = 10,
|
||||
sortBy?: any,
|
||||
filters?: any,
|
||||
options = {},
|
||||
) => {
|
||||
return useQuery<AccessListsResponse, Error>(
|
||||
["access-lists", { offset, limit, sortBy, filters }],
|
||||
() => fetchAccessLists(offset, limit, sortBy, filters),
|
||||
{
|
||||
keepPreviousData: true,
|
||||
staleTime: 15 * 1000, // 15 seconds
|
||||
...options,
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
export { useAccessLists };
|
137
frontend/src/pages/AccessLists/Table.tsx
Normal file
137
frontend/src/pages/AccessLists/Table.tsx
Normal file
@ -0,0 +1,137 @@
|
||||
import { useEffect, useMemo } from "react";
|
||||
|
||||
import {
|
||||
tableEvents,
|
||||
ActionsFormatter,
|
||||
IDFormatter,
|
||||
TableFilter,
|
||||
TableLayout,
|
||||
TablePagination,
|
||||
TableSortBy,
|
||||
TextFilter,
|
||||
} from "components";
|
||||
import { intl } from "locale";
|
||||
import { FiEdit } from "react-icons/fi";
|
||||
import { useSortBy, useFilters, useTable, usePagination } from "react-table";
|
||||
|
||||
export interface TableProps {
|
||||
data: any;
|
||||
pagination: TablePagination;
|
||||
sortBy: TableSortBy[];
|
||||
filters: TableFilter[];
|
||||
onTableEvent: any;
|
||||
}
|
||||
function Table({
|
||||
data,
|
||||
pagination,
|
||||
onTableEvent,
|
||||
sortBy,
|
||||
filters,
|
||||
}: TableProps) {
|
||||
const [columns, tableData] = useMemo(() => {
|
||||
const columns: any = [
|
||||
{
|
||||
Header: intl.formatMessage({ id: "column.id" }),
|
||||
accessor: "id",
|
||||
Cell: IDFormatter(),
|
||||
sortable: true,
|
||||
},
|
||||
{
|
||||
Header: intl.formatMessage({ id: "column.name" }),
|
||||
accessor: "name",
|
||||
sortable: true,
|
||||
Filter: TextFilter,
|
||||
},
|
||||
{
|
||||
id: "actions",
|
||||
accessor: "id",
|
||||
Cell: ActionsFormatter([
|
||||
{
|
||||
title: intl.formatMessage({ id: "action.edit" }),
|
||||
onClick: (e: any, data: any) => {
|
||||
alert(JSON.stringify(data, null, 2));
|
||||
},
|
||||
icon: <FiEdit />,
|
||||
show: (data: any) => !data.isSystem,
|
||||
},
|
||||
]),
|
||||
className: "w-80",
|
||||
},
|
||||
];
|
||||
return [columns, data];
|
||||
}, [data]);
|
||||
|
||||
const tableInstance = useTable(
|
||||
{
|
||||
columns,
|
||||
data: tableData,
|
||||
initialState: {
|
||||
pageIndex: Math.floor(pagination.offset / pagination.limit),
|
||||
pageSize: pagination.limit,
|
||||
sortBy,
|
||||
filters,
|
||||
},
|
||||
// Tell the usePagination
|
||||
// hook that we'll handle our own data fetching
|
||||
// This means we'll also have to provide our own
|
||||
// pageCount.
|
||||
pageCount: Math.ceil(pagination.total / pagination.limit),
|
||||
manualPagination: true,
|
||||
// Sorting options
|
||||
manualSortBy: true,
|
||||
disableMultiSort: true,
|
||||
disableSortRemove: true,
|
||||
autoResetSortBy: false,
|
||||
// Filter options
|
||||
manualFilters: true,
|
||||
autoResetFilters: false,
|
||||
},
|
||||
useFilters,
|
||||
useSortBy,
|
||||
usePagination,
|
||||
);
|
||||
|
||||
const gotoPage = tableInstance.gotoPage;
|
||||
|
||||
useEffect(() => {
|
||||
onTableEvent({
|
||||
type: tableEvents.PAGE_CHANGED,
|
||||
payload: tableInstance.state.pageIndex,
|
||||
});
|
||||
}, [onTableEvent, tableInstance.state.pageIndex]);
|
||||
|
||||
useEffect(() => {
|
||||
onTableEvent({
|
||||
type: tableEvents.PAGE_SIZE_CHANGED,
|
||||
payload: tableInstance.state.pageSize,
|
||||
});
|
||||
gotoPage(0);
|
||||
}, [gotoPage, onTableEvent, tableInstance.state.pageSize]);
|
||||
|
||||
useEffect(() => {
|
||||
if (pagination.total) {
|
||||
onTableEvent({
|
||||
type: tableEvents.TOTAL_COUNT_CHANGED,
|
||||
payload: pagination.total,
|
||||
});
|
||||
}
|
||||
}, [pagination.total, onTableEvent]);
|
||||
|
||||
useEffect(() => {
|
||||
onTableEvent({
|
||||
type: tableEvents.SORT_CHANGED,
|
||||
payload: tableInstance.state.sortBy,
|
||||
});
|
||||
}, [onTableEvent, tableInstance.state.sortBy]);
|
||||
|
||||
useEffect(() => {
|
||||
onTableEvent({
|
||||
type: tableEvents.FILTERS_CHANGED,
|
||||
payload: tableInstance.state.filters,
|
||||
});
|
||||
}, [onTableEvent, tableInstance.state.filters]);
|
||||
|
||||
return <TableLayout pagination={pagination} {...tableInstance} />;
|
||||
}
|
||||
|
||||
export default Table;
|
87
frontend/src/pages/AccessLists/TableWrapper.tsx
Normal file
87
frontend/src/pages/AccessLists/TableWrapper.tsx
Normal file
@ -0,0 +1,87 @@
|
||||
import { useEffect, useReducer, useState } from "react";
|
||||
|
||||
import { Alert, AlertIcon } from "@chakra-ui/react";
|
||||
import { EmptyList, SpinnerPage, tableEventReducer } from "components";
|
||||
import { useAccessLists } from "hooks";
|
||||
import { intl } from "locale";
|
||||
|
||||
import Table from "./Table";
|
||||
|
||||
const initialState = {
|
||||
offset: 0,
|
||||
limit: 10,
|
||||
sortBy: [
|
||||
{
|
||||
id: "name",
|
||||
desc: false,
|
||||
},
|
||||
],
|
||||
filters: [],
|
||||
};
|
||||
|
||||
interface TableWrapperProps {
|
||||
onCreateClick?: () => void;
|
||||
}
|
||||
function TableWrapper({ onCreateClick }: TableWrapperProps) {
|
||||
const [{ offset, limit, sortBy, filters }, dispatch] = useReducer(
|
||||
tableEventReducer,
|
||||
initialState,
|
||||
);
|
||||
|
||||
const [tableData, setTableData] = useState(null);
|
||||
const { isFetching, isLoading, isError, error, data } = useAccessLists(
|
||||
offset,
|
||||
limit,
|
||||
sortBy,
|
||||
filters,
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setTableData(data as any);
|
||||
}, [data]);
|
||||
|
||||
if (isFetching || isLoading || !tableData) {
|
||||
return <SpinnerPage />;
|
||||
}
|
||||
|
||||
if (isError) {
|
||||
return (
|
||||
<Alert status="error">
|
||||
<AlertIcon />
|
||||
{error?.message || "Unknown error"}
|
||||
</Alert>
|
||||
);
|
||||
}
|
||||
|
||||
if (isFetching || isLoading || !tableData) {
|
||||
return <SpinnerPage />;
|
||||
}
|
||||
|
||||
// When there are no items and no filters active, show the nicer empty view
|
||||
if (data?.total === 0 && filters?.length === 0) {
|
||||
return (
|
||||
<EmptyList
|
||||
title={intl.formatMessage({ id: "create-access-list-title" })}
|
||||
summary={intl.formatMessage({ id: "create-hint" })}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const pagination = {
|
||||
offset: data?.offset || initialState.offset,
|
||||
limit: data?.limit || initialState.limit,
|
||||
total: data?.total || 0,
|
||||
};
|
||||
|
||||
return (
|
||||
<Table
|
||||
data={data?.items || []}
|
||||
pagination={pagination}
|
||||
sortBy={sortBy}
|
||||
filters={filters}
|
||||
onTableEvent={dispatch}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default TableWrapper;
|
@ -1,9 +1,16 @@
|
||||
import { Heading } from "@chakra-ui/react";
|
||||
import { intl } from "locale";
|
||||
|
||||
import TableWrapper from "./TableWrapper";
|
||||
|
||||
function AccessLists() {
|
||||
return (
|
||||
<Heading mb={2}>{intl.formatMessage({ id: "access-lists.title" })}</Heading>
|
||||
<>
|
||||
<Heading mb={2}>
|
||||
{intl.formatMessage({ id: "access-lists.title" })}
|
||||
</Heading>
|
||||
<TableWrapper />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user