mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 02:26:40 +00:00
feat: adds pagination in organizations table
This commit is contained in:
@@ -230,7 +230,10 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {
|
|||||||
createdAt: z.date()
|
createdAt: z.date()
|
||||||
})
|
})
|
||||||
.array()
|
.array()
|
||||||
}).array()
|
}).array(),
|
||||||
|
meta: z.object({
|
||||||
|
total: z.number()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -243,9 +246,15 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {
|
|||||||
const organizations = await server.services.superAdmin.getOrganizations({
|
const organizations = await server.services.superAdmin.getOrganizations({
|
||||||
...req.query
|
...req.query
|
||||||
});
|
});
|
||||||
|
const count = await server.services.superAdmin.countOrganizations({
|
||||||
|
...req.query
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
organizations
|
organizations,
|
||||||
|
meta: {
|
||||||
|
total: count
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -138,6 +138,27 @@ export const orgDALFactory = (db: TDbClient) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const countOrganizationsByFilter = async ({ searchTerm }: { searchTerm: string }) => {
|
||||||
|
interface CountResult {
|
||||||
|
count: string;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const count = await db
|
||||||
|
.replicaNode()(TableName.Organization)
|
||||||
|
.where((qb) => {
|
||||||
|
if (searchTerm) {
|
||||||
|
void qb.whereILike(`${TableName.Organization}.name`, `%${searchTerm}%`);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.count("*")
|
||||||
|
.first();
|
||||||
|
|
||||||
|
return parseInt((count as unknown as CountResult).count || "0", 10);
|
||||||
|
} catch (error) {
|
||||||
|
throw new DatabaseError({ error, name: "Count organizations by filter" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const findOrgById = async (orgId: string) => {
|
const findOrgById = async (orgId: string) => {
|
||||||
try {
|
try {
|
||||||
const org = (await db
|
const org = (await db
|
||||||
@@ -670,6 +691,7 @@ export const orgDALFactory = (db: TDbClient) => {
|
|||||||
findOrgBySlug,
|
findOrgBySlug,
|
||||||
findAllOrgsByUserId,
|
findAllOrgsByUserId,
|
||||||
findOrganizationsByFilter,
|
findOrganizationsByFilter,
|
||||||
|
countOrganizationsByFilter,
|
||||||
ghostUserExists,
|
ghostUserExists,
|
||||||
findOrgMembersByUsername,
|
findOrgMembersByUsername,
|
||||||
findOrgMembersByRole,
|
findOrgMembersByRole,
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ import {
|
|||||||
TAdminGetUsersDTO,
|
TAdminGetUsersDTO,
|
||||||
TAdminIntegrationConfig,
|
TAdminIntegrationConfig,
|
||||||
TAdminSignUpDTO,
|
TAdminSignUpDTO,
|
||||||
|
TCountOrganizationsDTO,
|
||||||
TCreateOrganizationDTO,
|
TCreateOrganizationDTO,
|
||||||
TGetOrganizationsDTO,
|
TGetOrganizationsDTO,
|
||||||
TResendOrgInviteDTO
|
TResendOrgInviteDTO
|
||||||
@@ -750,6 +751,13 @@ export const superAdminServiceFactory = ({
|
|||||||
return organizations;
|
return organizations;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const countOrganizations = async ({ searchTerm }: TCountOrganizationsDTO) => {
|
||||||
|
const count = await orgDAL.countOrganizationsByFilter({
|
||||||
|
searchTerm
|
||||||
|
});
|
||||||
|
return count;
|
||||||
|
};
|
||||||
|
|
||||||
const createOrganization = async (
|
const createOrganization = async (
|
||||||
{ name, inviteAdminEmails: emails }: TCreateOrganizationDTO,
|
{ name, inviteAdminEmails: emails }: TCreateOrganizationDTO,
|
||||||
actor: OrgServiceActor
|
actor: OrgServiceActor
|
||||||
@@ -1146,6 +1154,7 @@ export const superAdminServiceFactory = ({
|
|||||||
invalidateCache,
|
invalidateCache,
|
||||||
checkIfInvalidatingCache,
|
checkIfInvalidatingCache,
|
||||||
getOrganizations,
|
getOrganizations,
|
||||||
|
countOrganizations,
|
||||||
deleteOrganization,
|
deleteOrganization,
|
||||||
deleteOrganizationMembership,
|
deleteOrganizationMembership,
|
||||||
initializeAdminIntegrationConfigSync,
|
initializeAdminIntegrationConfigSync,
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ export type TGetOrganizationsDTO = {
|
|||||||
searchTerm: string;
|
searchTerm: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type TCountOrganizationsDTO = {
|
||||||
|
searchTerm: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type TCreateOrganizationDTO = {
|
export type TCreateOrganizationDTO = {
|
||||||
name: string;
|
name: string;
|
||||||
inviteAdminEmails: string[];
|
inviteAdminEmails: string[];
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ import {
|
|||||||
AdminGetOrganizationsFilters,
|
AdminGetOrganizationsFilters,
|
||||||
AdminGetUsersFilters,
|
AdminGetUsersFilters,
|
||||||
AdminIntegrationsConfig,
|
AdminIntegrationsConfig,
|
||||||
OrganizationWithProjects,
|
|
||||||
TGetEnvOverrides,
|
TGetEnvOverrides,
|
||||||
TGetInvalidatingCacheStatus,
|
TGetInvalidatingCacheStatus,
|
||||||
|
TGetOrganizationsResponse,
|
||||||
TGetServerRootKmsEncryptionDetails,
|
TGetServerRootKmsEncryptionDetails,
|
||||||
TServerConfig
|
TServerConfig
|
||||||
} from "./types";
|
} from "./types";
|
||||||
@@ -49,24 +49,21 @@ export const fetchServerConfig = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useAdminGetOrganizations = (filters: AdminGetOrganizationsFilters) => {
|
export const useAdminGetOrganizations = (filters: AdminGetOrganizationsFilters) => {
|
||||||
return useInfiniteQuery({
|
return useQuery({
|
||||||
initialPageParam: 0,
|
|
||||||
queryKey: adminQueryKeys.getOrganizations(filters),
|
queryKey: adminQueryKeys.getOrganizations(filters),
|
||||||
queryFn: async ({ pageParam }) => {
|
queryFn: async () => {
|
||||||
const { data } = await apiRequest.get<{ organizations: OrganizationWithProjects[] }>(
|
const { data } = await apiRequest.get<TGetOrganizationsResponse>(
|
||||||
"/api/v1/admin/organization-management/organizations",
|
"/api/v1/admin/organization-management/organizations",
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
...filters,
|
...filters
|
||||||
offset: pageParam
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return data.organizations;
|
return { organizations: data.organizations, totalCount: data.meta.total };
|
||||||
},
|
},
|
||||||
getNextPageParam: (lastPage, pages) =>
|
placeholderData: (previousData) => previousData
|
||||||
lastPage.length !== 0 ? pages.length * filters.limit : undefined
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,13 @@ export type OrganizationWithProjects = Organization & {
|
|||||||
}[];
|
}[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type TGetOrganizationsResponse = {
|
||||||
|
organizations: OrganizationWithProjects[];
|
||||||
|
meta: {
|
||||||
|
total: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export type TServerConfig = {
|
export type TServerConfig = {
|
||||||
initialized: boolean;
|
initialized: boolean;
|
||||||
allowSignUp: boolean;
|
allowSignUp: boolean;
|
||||||
@@ -81,8 +88,9 @@ export type TCreateAdminUserDTO = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type AdminGetOrganizationsFilters = {
|
export type AdminGetOrganizationsFilters = {
|
||||||
limit: number;
|
searchTerm?: string;
|
||||||
searchTerm: string;
|
limit?: number;
|
||||||
|
offset?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type AdminGetUsersFilters = {
|
export type AdminGetUsersFilters = {
|
||||||
@@ -162,3 +170,7 @@ export type TResendOrgInviteDTO = {
|
|||||||
organizationId: string;
|
organizationId: string;
|
||||||
membershipId: string;
|
membershipId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export enum AdminOrganizationOrderBy {
|
||||||
|
Name = "name"
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,6 +48,11 @@ import {
|
|||||||
} from "@app/components/v2";
|
} from "@app/components/v2";
|
||||||
import { useUser } from "@app/context";
|
import { useUser } from "@app/context";
|
||||||
import { OrgMembershipRole } from "@app/helpers/roles";
|
import { OrgMembershipRole } from "@app/helpers/roles";
|
||||||
|
import {
|
||||||
|
getUserTablePreference,
|
||||||
|
PreferenceKey,
|
||||||
|
setUserTablePreference
|
||||||
|
} from "@app/helpers/userTablePreferences";
|
||||||
import { useDebounce, usePagination, usePopUp, useResetPageHelper } from "@app/hooks";
|
import { useDebounce, usePagination, usePopUp, useResetPageHelper } from "@app/hooks";
|
||||||
import {
|
import {
|
||||||
useAdminDeleteOrganization,
|
useAdminDeleteOrganization,
|
||||||
@@ -57,7 +62,7 @@ import {
|
|||||||
useServerAdminAccessOrg,
|
useServerAdminAccessOrg,
|
||||||
useServerAdminResendOrgInvite
|
useServerAdminResendOrgInvite
|
||||||
} from "@app/hooks/api";
|
} from "@app/hooks/api";
|
||||||
import { OrganizationWithProjects } from "@app/hooks/api/admin/types";
|
import { AdminOrganizationOrderBy, OrganizationWithProjects } from "@app/hooks/api/admin/types";
|
||||||
import { OrderByDirection } from "@app/hooks/api/generic/types";
|
import { OrderByDirection } from "@app/hooks/api/generic/types";
|
||||||
import { OrgMembershipStatus } from "@app/hooks/api/organization/types";
|
import { OrgMembershipStatus } from "@app/hooks/api/organization/types";
|
||||||
import { UsePopUpState } from "@app/hooks/usePopUp";
|
import { UsePopUpState } from "@app/hooks/usePopUp";
|
||||||
@@ -443,13 +448,32 @@ const OrganizationsPanelTable = ({
|
|||||||
const { user } = useUser();
|
const { user } = useUser();
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { data, isPending, isFetchingNextPage, hasNextPage, fetchNextPage } =
|
|
||||||
useAdminGetOrganizations({
|
const { offset, limit, setPage, perPage, page, setPerPage } =
|
||||||
limit: 20,
|
usePagination<AdminOrganizationOrderBy>(AdminOrganizationOrderBy.Name, {
|
||||||
|
initPerPage: getUserTablePreference("ResourceOverviewOrgsTable", PreferenceKey.PerPage, 10)
|
||||||
|
});
|
||||||
|
|
||||||
|
const handlePerPageChange = (newPerPage: number) => {
|
||||||
|
setPerPage(newPerPage);
|
||||||
|
setUserTablePreference("ResourceOverviewOrgsTable", PreferenceKey.PerPage, newPerPage);
|
||||||
|
};
|
||||||
|
|
||||||
|
const { data, isPending } = useAdminGetOrganizations({
|
||||||
|
limit,
|
||||||
|
offset,
|
||||||
searchTerm: debouncedSearchTerm
|
searchTerm: debouncedSearchTerm
|
||||||
});
|
});
|
||||||
|
|
||||||
const isEmpty = !isPending && !data?.pages?.[0].length;
|
const { organizations, totalCount = 0 } = data ?? {};
|
||||||
|
|
||||||
|
const isEmpty = !isPending && !totalCount;
|
||||||
|
|
||||||
|
useResetPageHelper({
|
||||||
|
totalCount,
|
||||||
|
offset,
|
||||||
|
setPage
|
||||||
|
});
|
||||||
|
|
||||||
const { mutateAsync: accessOrganization } = useServerAdminAccessOrg();
|
const { mutateAsync: accessOrganization } = useServerAdminAccessOrg();
|
||||||
|
|
||||||
@@ -499,8 +523,7 @@ const OrganizationsPanelTable = ({
|
|||||||
<TBody>
|
<TBody>
|
||||||
{isPending && <TableSkeleton columns={4} innerKey="organizations" />}
|
{isPending && <TableSkeleton columns={4} innerKey="organizations" />}
|
||||||
{!isPending &&
|
{!isPending &&
|
||||||
data?.pages?.map((orgs) =>
|
organizations?.map((org) => {
|
||||||
orgs.map((org) => {
|
|
||||||
const isMember = org.members.find((member) => member.user.id === user.id);
|
const isMember = org.members.find((member) => member.user.id === user.id);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -601,23 +624,19 @@ const OrganizationsPanelTable = ({
|
|||||||
</Td>
|
</Td>
|
||||||
</Tr>
|
</Tr>
|
||||||
);
|
);
|
||||||
})
|
})}
|
||||||
)}
|
|
||||||
</TBody>
|
</TBody>
|
||||||
</Table>
|
</Table>
|
||||||
{!isPending && isEmpty && <EmptyState title="No organizations found" icon={faBuilding} />}
|
{!isPending && isEmpty && <EmptyState title="No organizations found" icon={faBuilding} />}
|
||||||
</TableContainer>
|
</TableContainer>
|
||||||
{!isEmpty && (
|
{!isPending && totalCount > 0 && (
|
||||||
<Button
|
<Pagination
|
||||||
className="mt-4 py-3 text-sm"
|
count={totalCount}
|
||||||
isFullWidth
|
page={page}
|
||||||
variant="outline_bg"
|
perPage={perPage}
|
||||||
isLoading={isFetchingNextPage}
|
onChangePage={(newPage) => setPage(newPage)}
|
||||||
isDisabled={isFetchingNextPage || !hasNextPage}
|
onChangePerPage={handlePerPageChange}
|
||||||
onClick={() => fetchNextPage()}
|
/>
|
||||||
>
|
|
||||||
{hasNextPage ? "Load More" : "End of list"}
|
|
||||||
</Button>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<ViewMembersModal
|
<ViewMembersModal
|
||||||
|
|||||||
Reference in New Issue
Block a user