mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 15:27:46 +00:00
feat(ui): api hooks for new org settings page
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
export {
|
||||||
|
useAddIncidentContact,
|
||||||
|
useDeleteIncidentContact,
|
||||||
|
useGetOrgIncidentContact
|
||||||
|
} from './queries';
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import { apiRequest } from '@app/config/request';
|
||||||
|
|
||||||
|
import { AddIncidentContactDTO, DeleteIncidentContactDTO, IncidentContact } from './types';
|
||||||
|
|
||||||
|
const incidentContactKeys = {
|
||||||
|
getAllContact: (orgId: string) => ['org-incident-contacts', { orgId }] as const
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchOrgIncidentContacts = async (orgId: string) => {
|
||||||
|
const { data } = await apiRequest.get<{ incidentContactsOrg: IncidentContact[] }>(
|
||||||
|
`/api/v1/organization/${orgId}/incidentContactOrg`
|
||||||
|
);
|
||||||
|
|
||||||
|
return data.incidentContactsOrg;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useGetOrgIncidentContact = (orgId: string) =>
|
||||||
|
useQuery({
|
||||||
|
queryKey: incidentContactKeys.getAllContact(orgId),
|
||||||
|
queryFn: () => fetchOrgIncidentContacts(orgId),
|
||||||
|
enabled: Boolean(orgId)
|
||||||
|
});
|
||||||
|
|
||||||
|
// mutation
|
||||||
|
export const useAddIncidentContact = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
return useMutation<{}, {}, AddIncidentContactDTO>({
|
||||||
|
mutationFn: async ({ orgId, email }) => {
|
||||||
|
const { data } = await apiRequest.post(`/api/v1/organization/${orgId}/incidentContactOrg`, {
|
||||||
|
email
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
onSuccess: (_, { orgId }) => {
|
||||||
|
queryClient.invalidateQueries(incidentContactKeys.getAllContact(orgId));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useDeleteIncidentContact = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
return useMutation<{}, {}, DeleteIncidentContactDTO>({
|
||||||
|
mutationFn: async ({ orgId, email }) => {
|
||||||
|
const { data } = await apiRequest.delete(`/api/v1/organization/${orgId}/incidentContactOrg`, {
|
||||||
|
data: { email }
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
onSuccess: (_, { orgId }) => {
|
||||||
|
queryClient.invalidateQueries(incidentContactKeys.getAllContact(orgId));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
export type IncidentContact = {
|
||||||
|
_id: string;
|
||||||
|
email: string;
|
||||||
|
organization: string;
|
||||||
|
__v: number;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type DeleteIncidentContactDTO = {
|
||||||
|
orgId: string;
|
||||||
|
email: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AddIncidentContactDTO = {
|
||||||
|
orgId: string;
|
||||||
|
email: string;
|
||||||
|
};
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
export * from './auth';
|
export * from './auth';
|
||||||
|
export * from './incidentContacts';
|
||||||
export * from './keys';
|
export * from './keys';
|
||||||
export * from './organization';
|
export * from './organization';
|
||||||
export * from './serviceTokens';
|
export * from './serviceTokens';
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
export { useGetOrganization } from './queries';
|
export { useGetOrganization, useRenameOrg } from './queries';
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { useQuery } from '@tanstack/react-query';
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||||
|
|
||||||
import { apiRequest } from '@app/config/request';
|
import { apiRequest } from '@app/config/request';
|
||||||
|
|
||||||
import { Organization } from './types';
|
import { Organization, RenameOrgDTO } from './types';
|
||||||
|
|
||||||
const organizationKeys = {
|
const organizationKeys = {
|
||||||
getUserOrganization: ['organization'] as const
|
getUserOrganization: ['organization'] as const
|
||||||
@@ -16,3 +16,16 @@ const fetchUserOrganization = async () => {
|
|||||||
|
|
||||||
export const useGetOrganization = () =>
|
export const useGetOrganization = () =>
|
||||||
useQuery({ queryKey: organizationKeys.getUserOrganization, queryFn: fetchUserOrganization });
|
useQuery({ queryKey: organizationKeys.getUserOrganization, queryFn: fetchUserOrganization });
|
||||||
|
|
||||||
|
// mutation
|
||||||
|
export const useRenameOrg = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
return useMutation<{}, {}, RenameOrgDTO>({
|
||||||
|
mutationFn: ({ newOrgName, orgId }) =>
|
||||||
|
apiRequest.patch(`/api/v1/organization/${orgId}/name`, { name: newOrgName }),
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries(organizationKeys.getUserOrganization);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -4,3 +4,8 @@ export type Organization = {
|
|||||||
createAt: string;
|
createAt: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type RenameOrgDTO = {
|
||||||
|
orgId: string;
|
||||||
|
newOrgName: string;
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
export type { GetAuthTokenAPI } from './auth/types';
|
export type { GetAuthTokenAPI } from './auth/types';
|
||||||
|
export type { IncidentContact } from './incidentContacts/types';
|
||||||
export type { UserWsKeyPair } from './keys/types';
|
export type { UserWsKeyPair } from './keys/types';
|
||||||
export type { Organization } from './organization/types';
|
export type { Organization } from './organization/types';
|
||||||
export type { CreateServiceTokenDTO, ServiceToken } from './serviceTokens/types';
|
export type { CreateServiceTokenDTO, ServiceToken } from './serviceTokens/types';
|
||||||
export type { GetSubscriptionPlan, SubscriptionPlan } from './subscriptions/types';
|
export type { GetSubscriptionPlan, SubscriptionPlan } from './subscriptions/types';
|
||||||
export type { User } from './users/types';
|
export type { AddUserToWsDTO, AddUserToWsRes, OrgUser, User } from './users/types';
|
||||||
export type {
|
export type {
|
||||||
CreateEnvironmentDTO,
|
CreateEnvironmentDTO,
|
||||||
|
CreateWorkspaceDTO,
|
||||||
DeleteEnvironmentDTO,
|
DeleteEnvironmentDTO,
|
||||||
DeleteWorkspaceDTO,
|
DeleteWorkspaceDTO,
|
||||||
RenameWorkspaceDTO,
|
RenameWorkspaceDTO,
|
||||||
|
ToggleAutoCapitalizationDTO,
|
||||||
UpdateEnvironmentDTO,
|
UpdateEnvironmentDTO,
|
||||||
Workspace,
|
Workspace,
|
||||||
WorkspaceEnv,
|
WorkspaceEnv,
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
export {
|
export {
|
||||||
fetchOrgUsers,
|
fetchOrgUsers,
|
||||||
|
useAddUserToOrg,
|
||||||
useAddUserToWs,
|
useAddUserToWs,
|
||||||
|
useDeleteOrgMembership,
|
||||||
useGetOrgUsers,
|
useGetOrgUsers,
|
||||||
useGetUser,
|
useGetUser,
|
||||||
useLogoutUser
|
useLogoutUser,
|
||||||
|
useUpdateOrgUserRole
|
||||||
} from './queries';
|
} from './queries';
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
decryptAssymmetric,
|
decryptAssymmetric,
|
||||||
@@ -8,7 +8,15 @@ import { apiRequest } from '@app/config/request';
|
|||||||
import { setAuthToken } from '@app/reactQuery';
|
import { setAuthToken } from '@app/reactQuery';
|
||||||
|
|
||||||
import { useUploadWsKey } from '../keys/queries';
|
import { useUploadWsKey } from '../keys/queries';
|
||||||
import { AddUserToWsDTO, AddUserToWsRes, OrgUser, User } from './types';
|
import {
|
||||||
|
AddUserToOrgDTO,
|
||||||
|
AddUserToWsDTO,
|
||||||
|
AddUserToWsRes,
|
||||||
|
DeletOrgMembershipDTO,
|
||||||
|
OrgUser,
|
||||||
|
UpdateOrgUserRoleDTO,
|
||||||
|
User
|
||||||
|
} from './types';
|
||||||
|
|
||||||
const userKeys = {
|
const userKeys = {
|
||||||
getUser: ['user'] as const,
|
getUser: ['user'] as const,
|
||||||
@@ -32,7 +40,11 @@ export const fetchOrgUsers = async (orgId: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useGetOrgUsers = (orgId: string) =>
|
export const useGetOrgUsers = (orgId: string) =>
|
||||||
useQuery(userKeys.getOrgUsers(orgId), () => fetchOrgUsers(orgId));
|
useQuery({
|
||||||
|
queryKey: userKeys.getOrgUsers(orgId),
|
||||||
|
queryFn: () => fetchOrgUsers(orgId),
|
||||||
|
enabled: Boolean(orgId)
|
||||||
|
});
|
||||||
|
|
||||||
// mutation
|
// mutation
|
||||||
export const useAddUserToWs = () => {
|
export const useAddUserToWs = () => {
|
||||||
@@ -69,6 +81,46 @@ export const useAddUserToWs = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useAddUserToOrg = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
return useMutation<{}, {}, AddUserToOrgDTO>({
|
||||||
|
mutationFn: (dto) => apiRequest.post(`/api/v1/invite-org/signup`, dto),
|
||||||
|
onSuccess: (_, { organizationId }) => {
|
||||||
|
queryClient.invalidateQueries(userKeys.getOrgUsers(organizationId));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useDeleteOrgMembership = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
return useMutation<{}, {}, DeletOrgMembershipDTO>({
|
||||||
|
mutationFn: ({ membershipId }) => apiRequest.delete(`/api/v1/membership-org/${membershipId}`),
|
||||||
|
onSuccess: (_, { orgId }) => {
|
||||||
|
queryClient.invalidateQueries(userKeys.getOrgUsers(orgId));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useUpdateOrgUserRole = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
return useMutation<{}, {}, UpdateOrgUserRoleDTO>({
|
||||||
|
mutationFn: ({ organizationId, membershipId, role }) =>
|
||||||
|
apiRequest.patch(`/api/v2/organizations/${organizationId}/memberships/${membershipId}`, {
|
||||||
|
role
|
||||||
|
}),
|
||||||
|
onSuccess: (_, { organizationId }) => {
|
||||||
|
queryClient.invalidateQueries(userKeys.getOrgUsers(organizationId));
|
||||||
|
},
|
||||||
|
// to remove old states
|
||||||
|
onError: (_, { organizationId }) => {
|
||||||
|
queryClient.invalidateQueries(userKeys.getOrgUsers(organizationId));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export const useLogoutUser = () =>
|
export const useLogoutUser = () =>
|
||||||
useMutation({
|
useMutation({
|
||||||
mutationFn: () => apiRequest.post('/api/v1/auth/logout'),
|
mutationFn: () => apiRequest.post('/api/v1/auth/logout'),
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export type OrgUser = {
|
|||||||
inviteEmail: string;
|
inviteEmail: string;
|
||||||
organization: string;
|
organization: string;
|
||||||
role: 'owner' | 'admin' | 'member';
|
role: 'owner' | 'admin' | 'member';
|
||||||
status: 'invited' | 'accepted';
|
status: 'invited' | 'accepted' | 'verified' | 'completed';
|
||||||
deniedPermissions: any[];
|
deniedPermissions: any[];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -45,3 +45,19 @@ export type AddUserToWsRes = {
|
|||||||
invitee: OrgUser['user'];
|
invitee: OrgUser['user'];
|
||||||
latestKey: UserWsKeyPair;
|
latestKey: UserWsKeyPair;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type UpdateOrgUserRoleDTO = {
|
||||||
|
organizationId: string;
|
||||||
|
membershipId: string;
|
||||||
|
role: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type DeletOrgMembershipDTO = {
|
||||||
|
membershipId: string;
|
||||||
|
orgId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AddUserToOrgDTO = {
|
||||||
|
inviteeEmail: string;
|
||||||
|
organizationId: string;
|
||||||
|
};
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ export {
|
|||||||
useCreateWsEnvironment,
|
useCreateWsEnvironment,
|
||||||
useDeleteWorkspace,
|
useDeleteWorkspace,
|
||||||
useDeleteWsEnvironment,
|
useDeleteWsEnvironment,
|
||||||
|
useGetUserWorkspaceMemberships,
|
||||||
useGetUserWorkspaces,
|
useGetUserWorkspaces,
|
||||||
useGetWorkspaceById,
|
useGetWorkspaceById,
|
||||||
useRenameWorkspace,
|
useRenameWorkspace,
|
||||||
useToggleAutoCapitalization,
|
useToggleAutoCapitalization,
|
||||||
useUpdateWsEnvironment} from './queries';
|
useUpdateWsEnvironment} from './queries';
|
||||||
|
|
||||||
@@ -13,16 +13,18 @@ import {
|
|||||||
Workspace
|
Workspace
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
|
|
||||||
const workspaceKeys = {
|
const workspaceKeys = {
|
||||||
getWorkspaceById: (workspaceId: string) => [{ workspaceId }, 'workspace'] as const,
|
getWorkspaceById: (workspaceId: string) => [{ workspaceId }, 'workspace'] as const,
|
||||||
|
getWorkspaceMemberships: (orgId: string) => [{ orgId }, 'workspace-memberships'],
|
||||||
getAllUserWorkspace: ['workspaces'] as const
|
getAllUserWorkspace: ['workspaces'] as const
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchWorkspaceById = async (workspaceId: string) => {
|
const fetchWorkspaceById = async (workspaceId: string) => {
|
||||||
const { data } = await apiRequest.get<{ workspace: Workspace }>(`/api/v1/workspace/${workspaceId}`);
|
const { data } = await apiRequest.get<{ workspace: Workspace }>(
|
||||||
|
`/api/v1/workspace/${workspaceId}`
|
||||||
|
);
|
||||||
return data.workspace;
|
return data.workspace;
|
||||||
}
|
};
|
||||||
|
|
||||||
const fetchUserWorkspaces = async () => {
|
const fetchUserWorkspaces = async () => {
|
||||||
const { data } = await apiRequest.get<{ workspaces: Workspace[] }>('/api/v1/workspace');
|
const { data } = await apiRequest.get<{ workspaces: Workspace[] }>('/api/v1/workspace');
|
||||||
@@ -40,6 +42,21 @@ export const useGetWorkspaceById = (workspaceId: string) => {
|
|||||||
export const useGetUserWorkspaces = () =>
|
export const useGetUserWorkspaces = () =>
|
||||||
useQuery(workspaceKeys.getAllUserWorkspace, fetchUserWorkspaces);
|
useQuery(workspaceKeys.getAllUserWorkspace, fetchUserWorkspaces);
|
||||||
|
|
||||||
|
const fetchUserWorkspaceMemberships = async (orgId: string) => {
|
||||||
|
const { data } = await apiRequest.get<Record<string, Workspace[]>>(
|
||||||
|
`/api/v1/organization/${orgId}/workspace-memberships`
|
||||||
|
);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
// to get all userids in an org with the workspace they are part of
|
||||||
|
export const useGetUserWorkspaceMemberships = (orgId: string) =>
|
||||||
|
useQuery({
|
||||||
|
queryKey: workspaceKeys.getWorkspaceMemberships(orgId),
|
||||||
|
queryFn: () => fetchUserWorkspaceMemberships(orgId),
|
||||||
|
enabled: Boolean(orgId)
|
||||||
|
});
|
||||||
|
|
||||||
// mutation
|
// mutation
|
||||||
export const useCreateWorkspace = () => {
|
export const useCreateWorkspace = () => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
@@ -70,7 +87,9 @@ export const useToggleAutoCapitalization = () => {
|
|||||||
|
|
||||||
return useMutation<{}, {}, ToggleAutoCapitalizationDTO>({
|
return useMutation<{}, {}, ToggleAutoCapitalizationDTO>({
|
||||||
mutationFn: ({ workspaceID, state }) =>
|
mutationFn: ({ workspaceID, state }) =>
|
||||||
apiRequest.patch(`/api/v2/workspace/${workspaceID}/auto-capitalization`, { autoCapitalization: state }),
|
apiRequest.patch(`/api/v2/workspace/${workspaceID}/auto-capitalization`, {
|
||||||
|
autoCapitalization: state
|
||||||
|
}),
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
queryClient.invalidateQueries(workspaceKeys.getAllUserWorkspace);
|
queryClient.invalidateQueries(workspaceKeys.getAllUserWorkspace);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user