feat(ui): api hooks for new org settings page

This commit is contained in:
akhilmhdh
2023-02-17 23:26:29 +05:30
parent e5f6ed3dc7
commit c0d1495761
13 changed files with 207 additions and 15 deletions

View File

@@ -0,0 +1,5 @@
export {
useAddIncidentContact,
useDeleteIncidentContact,
useGetOrgIncidentContact
} from './queries';

View File

@@ -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));
}
});
};

View File

@@ -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;
};

View File

@@ -1,4 +1,5 @@
export * from './auth';
export * from './incidentContacts';
export * from './keys';
export * from './organization';
export * from './serviceTokens';

View File

@@ -1 +1 @@
export { useGetOrganization } from './queries';
export { useGetOrganization, useRenameOrg } from './queries';

View File

@@ -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 { Organization } from './types';
import { Organization, RenameOrgDTO } from './types';
const organizationKeys = {
getUserOrganization: ['organization'] as const
@@ -16,3 +16,16 @@ const fetchUserOrganization = async () => {
export const useGetOrganization = () =>
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);
}
});
};

View File

@@ -4,3 +4,8 @@ export type Organization = {
createAt: string;
updatedAt: string;
};
export type RenameOrgDTO = {
orgId: string;
newOrgName: string;
};

View File

@@ -1,14 +1,17 @@
export type { GetAuthTokenAPI } from './auth/types';
export type { IncidentContact } from './incidentContacts/types';
export type { UserWsKeyPair } from './keys/types';
export type { Organization } from './organization/types';
export type { CreateServiceTokenDTO, ServiceToken } from './serviceTokens/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 {
CreateEnvironmentDTO,
CreateWorkspaceDTO,
DeleteEnvironmentDTO,
DeleteWorkspaceDTO,
RenameWorkspaceDTO,
ToggleAutoCapitalizationDTO,
UpdateEnvironmentDTO,
Workspace,
WorkspaceEnv,

View File

@@ -1,7 +1,10 @@
export {
fetchOrgUsers,
useAddUserToOrg,
useAddUserToWs,
useDeleteOrgMembership,
useGetOrgUsers,
useGetUser,
useLogoutUser
useLogoutUser,
useUpdateOrgUserRole
} from './queries';

View File

@@ -1,4 +1,4 @@
import { useMutation, useQuery } from '@tanstack/react-query';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import {
decryptAssymmetric,
@@ -8,7 +8,15 @@ import { apiRequest } from '@app/config/request';
import { setAuthToken } from '@app/reactQuery';
import { useUploadWsKey } from '../keys/queries';
import { AddUserToWsDTO, AddUserToWsRes, OrgUser, User } from './types';
import {
AddUserToOrgDTO,
AddUserToWsDTO,
AddUserToWsRes,
DeletOrgMembershipDTO,
OrgUser,
UpdateOrgUserRoleDTO,
User
} from './types';
const userKeys = {
getUser: ['user'] as const,
@@ -32,7 +40,11 @@ export const fetchOrgUsers = async (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
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 = () =>
useMutation({
mutationFn: () => apiRequest.post('/api/v1/auth/logout'),

View File

@@ -32,7 +32,7 @@ export type OrgUser = {
inviteEmail: string;
organization: string;
role: 'owner' | 'admin' | 'member';
status: 'invited' | 'accepted';
status: 'invited' | 'accepted' | 'verified' | 'completed';
deniedPermissions: any[];
};
@@ -45,3 +45,19 @@ export type AddUserToWsRes = {
invitee: OrgUser['user'];
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;
};

View File

@@ -3,9 +3,9 @@ export {
useCreateWsEnvironment,
useDeleteWorkspace,
useDeleteWsEnvironment,
useGetUserWorkspaceMemberships,
useGetUserWorkspaces,
useGetWorkspaceById,
useRenameWorkspace,
useToggleAutoCapitalization,
useUpdateWsEnvironment} from './queries';

View File

@@ -13,16 +13,18 @@ import {
Workspace
} from './types';
const workspaceKeys = {
getWorkspaceById: (workspaceId: string) => [{ workspaceId }, 'workspace'] as const,
getWorkspaceMemberships: (orgId: string) => [{ orgId }, 'workspace-memberships'],
getAllUserWorkspace: ['workspaces'] as const
};
const fetchWorkspaceById = async (workspaceId: string) => {
const { data } = await apiRequest.get<{ workspace: Workspace }>(`/api/v1/workspace/${workspaceId}`);
return data.workspace;
}
const { data } = await apiRequest.get<{ workspace: Workspace }>(
`/api/v1/workspace/${workspaceId}`
);
return data.workspace;
};
const fetchUserWorkspaces = async () => {
const { data } = await apiRequest.get<{ workspaces: Workspace[] }>('/api/v1/workspace');
@@ -40,6 +42,21 @@ export const useGetWorkspaceById = (workspaceId: string) => {
export const useGetUserWorkspaces = () =>
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
export const useCreateWorkspace = () => {
const queryClient = useQueryClient();
@@ -70,7 +87,9 @@ export const useToggleAutoCapitalization = () => {
return useMutation<{}, {}, ToggleAutoCapitalizationDTO>({
mutationFn: ({ workspaceID, state }) =>
apiRequest.patch(`/api/v2/workspace/${workspaceID}/auto-capitalization`, { autoCapitalization: state }),
apiRequest.patch(`/api/v2/workspace/${workspaceID}/auto-capitalization`, {
autoCapitalization: state
}),
onSuccess: () => {
queryClient.invalidateQueries(workspaceKeys.getAllUserWorkspace);
}