From 9963724a6a116301c6bbaf22b464e01a82be79c0 Mon Sep 17 00:00:00 2001 From: Tuan Dang Date: Thu, 10 Aug 2023 12:18:17 +0700 Subject: [PATCH] Continue removing unused frontend components/logic, improve querying in select pages --- .../basic/dialog/AddIncidentContactDialog.tsx | 99 ------ .../basic/table/ProjectUsersTable.tsx | 53 +-- .../integrations/CloudIntegration.tsx | 122 ------- .../integrations/CloudIntegrationSection.tsx | 63 ---- .../integrations/FrameworkIntegration.tsx | 36 -- .../FrameworkIntegrationSection.tsx | 38 --- .../components/integrations/Integration.tsx | 313 ------------------ .../integrations/IntegrationSection.tsx | 63 ---- frontend/src/helpers/project.ts | 100 +++--- .../hooks/api/incidentContacts/queries.tsx | 16 +- .../src/hooks/api/integrations/queries.tsx | 2 +- .../src/hooks/api/organization/queries.tsx | 6 +- frontend/src/hooks/api/tags/queries.tsx | 8 +- frontend/src/hooks/api/users/index.tsx | 2 - frontend/src/hooks/api/users/queries.tsx | 12 +- frontend/src/hooks/api/workspace/index.tsx | 7 +- frontend/src/hooks/api/workspace/queries.tsx | 78 ++++- .../api/integrations/DeleteIntegration.ts | 25 -- .../api/integrations/DeleteIntegrationAuth.ts | 26 -- .../api/integrations/GetIntegrationApps.ts | 21 -- .../api/integrations/GetIntegrationOptions.ts | 17 - .../api/integrations/StartIntegration.ts | 35 -- .../getWorkspaceAuthorizations.ts | 26 -- .../integrations/getWorkspaceIntegrations.ts | 26 -- .../api/integrations/updateIntegration.ts | 55 --- frontend/src/pages/api/organization/GetOrg.ts | 22 -- .../organization/GetOrgProjectMemberships.ts | 24 -- .../pages/api/organization/GetOrgProjects.ts | 25 -- .../pages/api/organization/StripeRedirect.ts | 23 -- .../api/organization/addIncidentContact.ts | 25 -- .../changeUserRoleInOrganization.ts | 27 -- .../api/organization/deleteIncidentContact.ts | 25 -- .../deleteUserFromOrganization.ts | 22 -- .../api/organization/getIncidentContacts.ts | 27 -- .../src/pages/api/organization/renameOrg.ts | 26 -- .../pages/api/workspace/addUserToWorkspace.ts | 26 -- .../workspace/changeUserRoleInWorkspace.ts | 26 -- .../api/workspace/deleteUserFromWorkspace.ts | 22 -- .../pages/api/workspace/deleteWorkspace.ts | 22 -- .../pages/api/workspace/getWorkspaceKeys.ts | 22 -- .../pages/api/workspace/getWorkspaceTags.ts | 22 -- .../pages/api/workspace/getWorkspaceUsers.ts | 22 -- .../src/pages/api/workspace/getWorkspaces.ts | 31 -- .../src/pages/project/[id]/members/index.tsx | 29 +- 44 files changed, 181 insertions(+), 1536 deletions(-) delete mode 100644 frontend/src/components/basic/dialog/AddIncidentContactDialog.tsx delete mode 100644 frontend/src/components/integrations/CloudIntegration.tsx delete mode 100644 frontend/src/components/integrations/CloudIntegrationSection.tsx delete mode 100644 frontend/src/components/integrations/FrameworkIntegration.tsx delete mode 100644 frontend/src/components/integrations/FrameworkIntegrationSection.tsx delete mode 100644 frontend/src/components/integrations/Integration.tsx delete mode 100644 frontend/src/components/integrations/IntegrationSection.tsx delete mode 100644 frontend/src/pages/api/integrations/DeleteIntegration.ts delete mode 100644 frontend/src/pages/api/integrations/DeleteIntegrationAuth.ts delete mode 100644 frontend/src/pages/api/integrations/GetIntegrationApps.ts delete mode 100644 frontend/src/pages/api/integrations/GetIntegrationOptions.ts delete mode 100644 frontend/src/pages/api/integrations/StartIntegration.ts delete mode 100644 frontend/src/pages/api/integrations/getWorkspaceAuthorizations.ts delete mode 100644 frontend/src/pages/api/integrations/getWorkspaceIntegrations.ts delete mode 100644 frontend/src/pages/api/integrations/updateIntegration.ts delete mode 100644 frontend/src/pages/api/organization/GetOrg.ts delete mode 100644 frontend/src/pages/api/organization/GetOrgProjectMemberships.ts delete mode 100644 frontend/src/pages/api/organization/GetOrgProjects.ts delete mode 100644 frontend/src/pages/api/organization/StripeRedirect.ts delete mode 100644 frontend/src/pages/api/organization/addIncidentContact.ts delete mode 100644 frontend/src/pages/api/organization/changeUserRoleInOrganization.ts delete mode 100644 frontend/src/pages/api/organization/deleteIncidentContact.ts delete mode 100644 frontend/src/pages/api/organization/deleteUserFromOrganization.ts delete mode 100644 frontend/src/pages/api/organization/getIncidentContacts.ts delete mode 100644 frontend/src/pages/api/organization/renameOrg.ts delete mode 100644 frontend/src/pages/api/workspace/addUserToWorkspace.ts delete mode 100644 frontend/src/pages/api/workspace/changeUserRoleInWorkspace.ts delete mode 100644 frontend/src/pages/api/workspace/deleteUserFromWorkspace.ts delete mode 100644 frontend/src/pages/api/workspace/deleteWorkspace.ts delete mode 100644 frontend/src/pages/api/workspace/getWorkspaceKeys.ts delete mode 100644 frontend/src/pages/api/workspace/getWorkspaceTags.ts delete mode 100644 frontend/src/pages/api/workspace/getWorkspaceUsers.ts delete mode 100644 frontend/src/pages/api/workspace/getWorkspaces.ts diff --git a/frontend/src/components/basic/dialog/AddIncidentContactDialog.tsx b/frontend/src/components/basic/dialog/AddIncidentContactDialog.tsx deleted file mode 100644 index e3e8acaa1..000000000 --- a/frontend/src/components/basic/dialog/AddIncidentContactDialog.tsx +++ /dev/null @@ -1,99 +0,0 @@ -import { Fragment, useState } from "react"; -import { useTranslation } from "react-i18next"; -import { Dialog, Transition } from "@headlessui/react"; - -import addIncidentContact from "@app/pages/api/organization/addIncidentContact"; - -import Button from "../buttons/Button"; -import InputField from "../InputField"; - -type Props = { - isOpen: boolean; - closeModal: () => void; - incidentContacts: string[]; - setIncidentContacts: (arg: string[]) => void; -}; - -const AddIncidentContactDialog = ({ - isOpen, - closeModal, - incidentContacts, - setIncidentContacts -}: Props) => { - const [incidentContactEmail, setIncidentContactEmail] = useState(""); - const { t } = useTranslation(); - - const submit = () => { - setIncidentContacts( - incidentContacts?.length > 0 - ? incidentContacts.concat([incidentContactEmail]) - : [incidentContactEmail] - ); - addIncidentContact(localStorage.getItem("orgData.id") as string, incidentContactEmail); - closeModal(); - }; - return ( -
- - - -
- - -
-
- - - - {t("section.incident.add-dialog.title")} - -
-

- {t("section.incident.add-dialog.description")} -

-
-
- -
-
-
-
-
-
-
-
-
-
- ); -}; - -export default AddIncidentContactDialog; diff --git a/frontend/src/components/basic/table/ProjectUsersTable.tsx b/frontend/src/components/basic/table/ProjectUsersTable.tsx index 5a9795cff..53a0e86f7 100644 --- a/frontend/src/components/basic/table/ProjectUsersTable.tsx +++ b/frontend/src/components/basic/table/ProjectUsersTable.tsx @@ -6,8 +6,10 @@ import { useNotificationContext } from "@app/components/context/Notifications/No import { Select, SelectItem } from "@app/components/v2"; import { useSubscription, useWorkspace } from "@app/context"; import updateUserProjectPermission from "@app/ee/api/memberships/UpdateUserProjectPermission"; -import changeUserRoleInWorkspace from "@app/pages/api/workspace/changeUserRoleInWorkspace"; -import deleteUserFromWorkspace from "@app/pages/api/workspace/deleteUserFromWorkspace"; +import { + useDeleteUserFromWorkspace, + useUpdateUserWorkspaceRole +} from "@app/hooks/api"; import getLatestFileKey from "@app/pages/api/workspace/getLatestFileKey"; import uploadKeys from "@app/pages/api/workspace/uploadKeys"; @@ -40,9 +42,11 @@ type EnvironmentProps = { const ProjectUsersTable = ({ userData, changeData, myUser, filter, isUserListLoading }: Props) => { const { currentWorkspace } = useWorkspace(); const { subscription } = useSubscription(); - const [roleSelected, setRoleSelected] = useState( - Array(userData?.length).fill(userData.map((user) => user.role)) - ); + const { mutateAsync: deleteUserFromWorkspaceMutateAsync } = useDeleteUserFromWorkspace(); + const { mutateAsync: updateUserWorkspaceRoleMutateAsync } = useUpdateUserWorkspaceRole(); + // const [roleSelected, setRoleSelected] = useState( + // Array(userData?.length).fill(userData.map((user) => user.role)) + // ); const router = useRouter(); const [myRole, setMyRole] = useState("member"); const [workspaceEnvs, setWorkspaceEnvs] = useState([]); @@ -52,38 +56,15 @@ const ProjectUsersTable = ({ userData, changeData, myUser, filter, isUserListLoa const workspaceId = router.query.id as string; // Delete the row in the table (e.g. a user) // #TODO: Add a pop-up that warns you that the user is going to be deleted. - const handleDelete = (membershipId: string, index: number) => { - // setUserIdToBeDeleted(userId); - // onClick(); - deleteUserFromWorkspace(membershipId); - changeData(userData.filter((v, i) => i !== index)); - setRoleSelected([ - ...roleSelected.slice(0, index), - ...roleSelected.slice(index + 1, userData?.length) - ]); + const handleDelete = async (membershipId: string) => { + await deleteUserFromWorkspaceMutateAsync(membershipId); }; - // Update the rold of a certain user - const handleRoleUpdate = (index: number, e: string) => { - changeUserRoleInWorkspace(userData[index].membershipId, e.toLowerCase()); - changeData([ - ...userData.slice(0, index), - ...[ - { - key: userData[index].key, - firstName: userData[index].firstName, - lastName: userData[index].lastName, - email: userData[index].email, - role: e.toLocaleLowerCase(), - status: userData[index].status, - userId: userData[index].userId, - membershipId: userData[index].membershipId, - publicKey: userData[index].publicKey, - deniedPermissions: userData[index].deniedPermissions - } - ], - ...userData.slice(index + 1, userData?.length) - ]); + const handleRoleUpdate = async (index: number, e: string) => { + await updateUserWorkspaceRoleMutateAsync({ + membershipId: userData[index].membershipId, + role: e.toLowerCase() + }); createNotification({ text: "Successfully changed user role.", type: "success" @@ -373,7 +354,7 @@ const ProjectUsersTable = ({ userData, changeData, myUser, filter, isUserListLoa myRole !== "member" ? (
- - - ); -}; - -export default IntegrationTile; diff --git a/frontend/src/components/integrations/IntegrationSection.tsx b/frontend/src/components/integrations/IntegrationSection.tsx deleted file mode 100644 index 8ff114e35..000000000 --- a/frontend/src/components/integrations/IntegrationSection.tsx +++ /dev/null @@ -1,63 +0,0 @@ -import IntegrationTile from "./Integration"; - -interface Props { - integrations: any; - setIntegrations: any; - bot: any; - setBot: any; - environments: Array<{ name: string; slug: string }>; - handleDeleteIntegration: (args: { integration: Integration }) => void; -} - -interface Integration { - _id: string; - isActive: boolean; - app: string | null; - appId: string | null; - path: string | null; - region: string | null; - createdAt: string; - updatedAt: string; - environment: string; - integration: string; - targetEnvironment: string; - workspace: string; - integrationAuth: string; - secretPath: string; -} - -const ProjectIntegrationSection = ({ - integrations, - setIntegrations, - bot, - setBot, - environments = [], - handleDeleteIntegration -}: Props) => { - return integrations.length > 0 ? ( -
-
-

Current Integrations

-

Manage integrations with third-party services.

-
- {integrations.map((integration: Integration) => { - return ( - - ); - })} -
- ) : ( -
- ); -}; - -export default ProjectIntegrationSection; diff --git a/frontend/src/helpers/project.ts b/frontend/src/helpers/project.ts index c15baf1ef..c24d3b9cf 100644 --- a/frontend/src/helpers/project.ts +++ b/frontend/src/helpers/project.ts @@ -91,65 +91,55 @@ const initProjectHelper = async ({ organizationId: string; projectName: string; }) => { - let project; - try { + // create new project + const { data: { workspace } } = await createWorkspace({ + workspaceName: projectName, + organizationId + }); + + // create and upload new (encrypted) project key + const randomBytes = crypto.randomBytes(16).toString("hex"); + const PRIVATE_KEY = localStorage.getItem("PRIVATE_KEY"); + + if (!PRIVATE_KEY) throw new Error("Failed to find private key"); - // create new project - const { data: { workspace } } = await createWorkspace({ - workspaceName: projectName, - organizationId - }); - - project = workspace; + const user = await fetchUserDetails(); - // create and upload new (encrypted) project key - const randomBytes = crypto.randomBytes(16).toString("hex"); - const PRIVATE_KEY = localStorage.getItem("PRIVATE_KEY"); - - if (!PRIVATE_KEY) throw new Error("Failed to find private key"); + const { ciphertext, nonce } = encryptAssymmetric({ + plaintext: randomBytes, + publicKey: user.publicKey, + privateKey: PRIVATE_KEY + }); - const user = await fetchUserDetails(); + await uploadKeys(workspace._id, user._id, ciphertext, nonce); - const { ciphertext, nonce } = encryptAssymmetric({ - plaintext: randomBytes, - publicKey: user.publicKey, - privateKey: PRIVATE_KEY - }); - - await uploadKeys(project._id, user._id, ciphertext, nonce); - - const workspaceId = project._id; - - // encrypt and upload secrets to new project - const secrets = await encryptSecrets({ - secretsToEncrypt: secretsToBeAdded, - workspaceId, - env: "dev" - }); - - secrets?.forEach((secret) => { - createSecret({ - workspaceId, - environment: secret.environment, - type: secret.type, - secretKey: secret.secretName, - secretKeyCiphertext: secret.secretKeyCiphertext, - secretKeyIV: secret.secretKeyIV, - secretKeyTag: secret.secretKeyTag, - secretValueCiphertext: secret.secretValueCiphertext, - secretValueIV: secret.secretValueIV, - secretValueTag: secret.secretValueTag, - secretCommentCiphertext: secret.secretCommentCiphertext, - secretCommentIV: secret.secretCommentIV, - secretCommentTag: secret.secretCommentTag, - secretPath: "/" - }); - }); - } catch (err) { - console.error("Failed to init project in organization", err); - } - - return project; + // encrypt and upload secrets to new project + const secrets = await encryptSecrets({ + secretsToEncrypt: secretsToBeAdded, + workspaceId: workspace._id, + env: "dev" + }); + + secrets?.forEach((secret) => { + createSecret({ + workspaceId: workspace._id, + environment: secret.environment, + type: secret.type, + secretKey: secret.secretName, + secretKeyCiphertext: secret.secretKeyCiphertext, + secretKeyIV: secret.secretKeyIV, + secretKeyTag: secret.secretKeyTag, + secretValueCiphertext: secret.secretValueCiphertext, + secretValueIV: secret.secretValueIV, + secretValueTag: secret.secretValueTag, + secretCommentCiphertext: secret.secretCommentCiphertext, + secretCommentIV: secret.secretCommentIV, + secretCommentTag: secret.secretCommentTag, + secretPath: "/" + }); + }); + + return workspace; } export { diff --git a/frontend/src/hooks/api/incidentContacts/queries.tsx b/frontend/src/hooks/api/incidentContacts/queries.tsx index 708aaaf78..aecf56a3e 100644 --- a/frontend/src/hooks/api/incidentContacts/queries.tsx +++ b/frontend/src/hooks/api/incidentContacts/queries.tsx @@ -8,18 +8,16 @@ 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), + queryFn: async () => { + const { data } = await apiRequest.get<{ incidentContactsOrg: IncidentContact[] }>( + `/api/v1/organization/${orgId}/incidentContactOrg` + ); + + return data.incidentContactsOrg; + }, enabled: Boolean(orgId) }); diff --git a/frontend/src/hooks/api/integrations/queries.tsx b/frontend/src/hooks/api/integrations/queries.tsx index c1353b6c6..c982659cb 100644 --- a/frontend/src/hooks/api/integrations/queries.tsx +++ b/frontend/src/hooks/api/integrations/queries.tsx @@ -32,4 +32,4 @@ export const useDeleteIntegration = () => { queryClient.invalidateQueries(workspaceKeys.getWorkspaceIntegrations(workspaceId)); } }); -}; +}; \ No newline at end of file diff --git a/frontend/src/hooks/api/organization/queries.tsx b/frontend/src/hooks/api/organization/queries.tsx index f7bd3e973..13810febd 100644 --- a/frontend/src/hooks/api/organization/queries.tsx +++ b/frontend/src/hooks/api/organization/queries.tsx @@ -41,8 +41,10 @@ export const useRenameOrg = () => { const queryClient = useQueryClient(); return useMutation<{}, {}, RenameOrgDTO>({ - mutationFn: ({ newOrgName, orgId }) => - apiRequest.patch(`/api/v1/organization/${orgId}/name`, { name: newOrgName }), + mutationFn: ({ newOrgName, orgId }) => { + console.log("useRenameOrg"); + return apiRequest.patch(`/api/v1/organization/${orgId}/name`, { name: newOrgName }); + }, onSuccess: () => { queryClient.invalidateQueries(organizationKeys.getUserOrganizations); } diff --git a/frontend/src/hooks/api/tags/queries.tsx b/frontend/src/hooks/api/tags/queries.tsx index 3134884b8..74900da0e 100644 --- a/frontend/src/hooks/api/tags/queries.tsx +++ b/frontend/src/hooks/api/tags/queries.tsx @@ -10,7 +10,6 @@ import { UserWsTags } from "./types"; - const workspaceTags = { getWsTags: (workspaceID: string) => ["workspace-tags", { workspaceID }] as const }; @@ -23,12 +22,13 @@ const fetchWsTag = async (workspaceID: string) => { return data.workspaceTags; }; -export const useGetWsTags = (workspaceID: string) => - useQuery({ +export const useGetWsTags = (workspaceID: string) => { + return useQuery({ queryKey: workspaceTags.getWsTags(workspaceID), queryFn: () => fetchWsTag(workspaceID), enabled: Boolean(workspaceID) }); +} export const useCreateWsTag = () => { const queryClient = useQueryClient(); @@ -59,4 +59,4 @@ export const useDeleteWsTag = () => { queryClient.invalidateQueries(workspaceTags.getWsTags(tagData?.workspace)); } }); -}; +}; \ No newline at end of file diff --git a/frontend/src/hooks/api/users/index.tsx b/frontend/src/hooks/api/users/index.tsx index e1b9fe402..cb421c633 100644 --- a/frontend/src/hooks/api/users/index.tsx +++ b/frontend/src/hooks/api/users/index.tsx @@ -3,10 +3,8 @@ export { useAddUserToOrg, useAddUserToWs, useCreateAPIKey, - useCreateMyAction, useDeleteAPIKey, useDeleteOrgMembership, - useGetMyActions, useGetMyAPIKeys, useGetMyIp, useGetMySessions, diff --git a/frontend/src/hooks/api/users/queries.tsx b/frontend/src/hooks/api/users/queries.tsx index 938abf3aa..48dfdf564 100644 --- a/frontend/src/hooks/api/users/queries.tsx +++ b/frontend/src/hooks/api/users/queries.tsx @@ -158,8 +158,9 @@ export const useDeleteOrgMembership = () => { const queryClient = useQueryClient(); return useMutation<{}, {}, DeletOrgMembershipDTO>({ - mutationFn: ({ membershipId, orgId }) => - apiRequest.delete(`/api/v2/organizations/${orgId}/memberships/${membershipId}`), + mutationFn: ({ membershipId, orgId }) => { + return apiRequest.delete(`/api/v2/organizations/${orgId}/memberships/${membershipId}`) + }, onSuccess: (_, { orgId }) => { queryClient.invalidateQueries(userKeys.getOrgUsers(orgId)); } @@ -170,10 +171,11 @@ export const useUpdateOrgUserRole = () => { const queryClient = useQueryClient(); return useMutation<{}, {}, UpdateOrgUserRoleDTO>({ - mutationFn: ({ organizationId, membershipId, role }) => - apiRequest.patch(`/api/v2/organizations/${organizationId}/memberships/${membershipId}`, { + mutationFn: ({ organizationId, membershipId, role }) => { + return apiRequest.patch(`/api/v2/organizations/${organizationId}/memberships/${membershipId}`, { role - }), + }); + }, onSuccess: (_, { organizationId }) => { queryClient.invalidateQueries(userKeys.getOrgUsers(organizationId)); }, diff --git a/frontend/src/hooks/api/workspace/index.tsx b/frontend/src/hooks/api/workspace/index.tsx index bd06fedd3..42d3467d1 100644 --- a/frontend/src/hooks/api/workspace/index.tsx +++ b/frontend/src/hooks/api/workspace/index.tsx @@ -1,6 +1,8 @@ export { + useAddUserToWorkspace, useCreateWorkspace, useCreateWsEnvironment, + useDeleteUserFromWorkspace, useDeleteWorkspace, useDeleteWsEnvironment, useGetUserWorkspaceMemberships, @@ -11,8 +13,9 @@ export { useGetWorkspaceIndexStatus, useGetWorkspaceIntegrations, useGetWorkspaceSecrets, + useGetWorkspaceUsers, useNameWorkspaceSecrets, useRenameWorkspace, useToggleAutoCapitalization, - useUpdateWsEnvironment -} from "./queries"; + useUpdateUserWorkspaceRole, + useUpdateWsEnvironment} from "./queries"; diff --git a/frontend/src/hooks/api/workspace/queries.tsx b/frontend/src/hooks/api/workspace/queries.tsx index 860ee7757..e5c2d6c96 100644 --- a/frontend/src/hooks/api/workspace/queries.tsx +++ b/frontend/src/hooks/api/workspace/queries.tsx @@ -29,7 +29,8 @@ export const workspaceKeys = { getWorkspaceIntegrations: (workspaceId: string) => [{ workspaceId }, "workspace-integrations"], getAllUserWorkspace: ["workspaces"] as const, getUserWsEnvironments: (workspaceId: string) => ["workspace-env", { workspaceId }] as const, - getWorkspaceAuditLogs: (workspaceId: string) => [{ workspaceId }] as const + getWorkspaceAuditLogs: (workspaceId: string) => [{ workspaceId }] as const, + getWorkspaceUsers: (workspaceId: string) => [{ workspaceId }] as const }; const fetchWorkspaceById = async (workspaceId: string) => { @@ -218,7 +219,9 @@ export const useDeleteWorkspace = () => { const queryClient = useQueryClient(); return useMutation<{}, {}, DeleteWorkspaceDTO>({ - mutationFn: ({ workspaceID }) => apiRequest.delete(`/api/v1/workspace/${workspaceID}`), + mutationFn: ({ workspaceID }) => { + return apiRequest.delete(`/api/v1/workspace/${workspaceID}`); + }, onSuccess: () => { queryClient.invalidateQueries(workspaceKeys.getAllUserWorkspace); } @@ -273,3 +276,74 @@ export const useDeleteWsEnvironment = () => { }); }; +export const useGetWorkspaceUsers = (workspaceId: string) => { + return useQuery({ + queryKey: workspaceKeys.getWorkspaceUsers(workspaceId), + queryFn: async () => { + const { data: { users } } = await apiRequest.get( + `/api/v1/workspace/${workspaceId}/users` + ); + return users; + }, + enabled: true + }); +} + +export const useAddUserToWorkspace = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async ({ + email, + workspaceId + }: { + email: string; + workspaceId: string; + }) => { + const { data: { invitee, latestKey } } = await apiRequest.post(`/api/v1/workspace/${workspaceId}/invite-signup`, { email }); + + return ({ + invitee, + latestKey + }); + }, + onSuccess: (_, dto) => { + queryClient.invalidateQueries(workspaceKeys.getWorkspaceUsers(dto.workspaceId)); + } + }); +}; + +export const useDeleteUserFromWorkspace = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async (membershipId: string) => { + const { data: { deletedMembership } } = await apiRequest.delete(`/api/v1/membership/${membershipId}`); + return deletedMembership; + }, + onSuccess: (res) => { + queryClient.invalidateQueries(workspaceKeys.getWorkspaceUsers(res.workspace)); + } + }); +}; + +export const useUpdateUserWorkspaceRole = () => { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: async ({ + membershipId, + role + }: { + membershipId: string; + role: string; + }) => { + const { data: { membership } } = await apiRequest.post(`/api/v1/membership/${membershipId}/change-role`, { + role + }); + return membership; + }, + onSuccess: (res) => { + queryClient.invalidateQueries(workspaceKeys.getWorkspaceUsers(res.workspace)); + } + }); +}; diff --git a/frontend/src/pages/api/integrations/DeleteIntegration.ts b/frontend/src/pages/api/integrations/DeleteIntegration.ts deleted file mode 100644 index b9253869a..000000000 --- a/frontend/src/pages/api/integrations/DeleteIntegration.ts +++ /dev/null @@ -1,25 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -interface Props { - integrationId: string; -} - -/** - * This route deletes an integration from a certain project - * @param {*} integrationId - * @returns - */ -const deleteIntegration = ({ integrationId }: Props) => - SecurityClient.fetchCall(`/api/v1/integration/${integrationId}`, { - method: "DELETE", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return (await res.json()).integration; - } - return undefined; - }); - -export default deleteIntegration; diff --git a/frontend/src/pages/api/integrations/DeleteIntegrationAuth.ts b/frontend/src/pages/api/integrations/DeleteIntegrationAuth.ts deleted file mode 100644 index c43de0b3a..000000000 --- a/frontend/src/pages/api/integrations/DeleteIntegrationAuth.ts +++ /dev/null @@ -1,26 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -interface Props { - integrationAuthId: string; -} - -/** - * This route deletes an integration authorization from a certain project - * @param {*} integrationAuthId - * @returns - */ -const deleteIntegrationAuth = ({ integrationAuthId }: Props) => - SecurityClient.fetchCall(`/api/v1/integration-auth/${integrationAuthId}`, { - method: "DELETE", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return (await res.json()).integrationAuth; - } - console.log("Failed to delete an integration authorization"); - return undefined; - }); - -export default deleteIntegrationAuth; diff --git a/frontend/src/pages/api/integrations/GetIntegrationApps.ts b/frontend/src/pages/api/integrations/GetIntegrationApps.ts deleted file mode 100644 index c1784cf9c..000000000 --- a/frontend/src/pages/api/integrations/GetIntegrationApps.ts +++ /dev/null @@ -1,21 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -interface Props { - integrationAuthId: string; -} - -const getIntegrationApps = ({ integrationAuthId }: Props) => - SecurityClient.fetchCall(`/api/v1/integration-auth/${integrationAuthId}/apps`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return (await res.json()).apps; - } - console.log("Failed to get available apps for an integration"); - return undefined; - }); - -export default getIntegrationApps; diff --git a/frontend/src/pages/api/integrations/GetIntegrationOptions.ts b/frontend/src/pages/api/integrations/GetIntegrationOptions.ts deleted file mode 100644 index eacdb9e9d..000000000 --- a/frontend/src/pages/api/integrations/GetIntegrationOptions.ts +++ /dev/null @@ -1,17 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -const getIntegrationOptions = () => - SecurityClient.fetchCall("/api/v1/integration-auth/integration-options", { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return (await res.json()).integrationOptions; - } - console.log("Failed to get (cloud) integration options"); - return undefined; - }); - -export default getIntegrationOptions; diff --git a/frontend/src/pages/api/integrations/StartIntegration.ts b/frontend/src/pages/api/integrations/StartIntegration.ts deleted file mode 100644 index 9172b378b..000000000 --- a/frontend/src/pages/api/integrations/StartIntegration.ts +++ /dev/null @@ -1,35 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -interface Props { - integrationId: string; - appName: string; - environment: string; -} - -/** - * This route starts the integration after teh default one if gonna set up. - * @param {*} integrationId - * @returns - */ -const startIntegration = ({ integrationId, appName, environment }: Props) => - SecurityClient.fetchCall(`/api/v1/integration/${integrationId}`, { - method: "PATCH", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - update: { - app: appName, - environment, - isActive: true - } - }) - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log("Failed to start an integration"); - return undefined; - }); - -export default startIntegration; diff --git a/frontend/src/pages/api/integrations/getWorkspaceAuthorizations.ts b/frontend/src/pages/api/integrations/getWorkspaceAuthorizations.ts deleted file mode 100644 index 2c5822049..000000000 --- a/frontend/src/pages/api/integrations/getWorkspaceAuthorizations.ts +++ /dev/null @@ -1,26 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -interface Props { - workspaceId: string; -} - -/** - * This route gets authorizations of a certain project (Heroku, etc.) - * @param {*} workspaceId - * @returns - */ -const getWorkspaceAuthorizations = ({ workspaceId }: Props) => - SecurityClient.fetchCall(`/api/v1/workspace/${workspaceId}/authorizations`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return (await res.json()).authorizations; - } - console.log("Failed to get project authorizations"); - return undefined; - }); - -export default getWorkspaceAuthorizations; diff --git a/frontend/src/pages/api/integrations/getWorkspaceIntegrations.ts b/frontend/src/pages/api/integrations/getWorkspaceIntegrations.ts deleted file mode 100644 index 19a4b78c9..000000000 --- a/frontend/src/pages/api/integrations/getWorkspaceIntegrations.ts +++ /dev/null @@ -1,26 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -interface Props { - workspaceId: string; -} - -/** - * This route gets integrations of a certain project (Heroku, etc.) - * @param {*} workspaceId - * @returns - */ -const getWorkspaceIntegrations = ({ workspaceId }: Props) => - SecurityClient.fetchCall(`/api/v1/workspace/${workspaceId}/integrations`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return (await res.json()).integrations; - } - console.log("Failed to get the project integrations"); - return undefined; - }); - -export default getWorkspaceIntegrations; diff --git a/frontend/src/pages/api/integrations/updateIntegration.ts b/frontend/src/pages/api/integrations/updateIntegration.ts deleted file mode 100644 index dbcdea899..000000000 --- a/frontend/src/pages/api/integrations/updateIntegration.ts +++ /dev/null @@ -1,55 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route starts the integration after teh default one if gonna set up. - * Update integration with id [integrationId] to sync envars from the project's - * [environment] to the integration [app] with active state [isActive] - * @param {Object} obj - * @param {String} obj.integrationId - id of integration - * @param {Boolean} obj.isActive - active state - * @param {String} obj.environment - project environment to push secrets from - * @param {String} obj.app - name of app - * @param {String} obj.appId - (optional) app ID for integration - * @param {String} obj.targetEnvironment - target environment for integration - * @param {String} obj.owner - (optional) owner login of repo for GitHub integration - * @returns - */ -const updateIntegration = ({ - integrationId, - isActive, - environment, - app, - appId, - targetEnvironment, - owner -}: { - integrationId: string; - isActive: boolean; - environment: string; - app: string; - appId: string | null; - targetEnvironment: string | null; - owner: string | null; -}) => - SecurityClient.fetchCall(`/api/v1/integration/${integrationId}`, { - method: "PATCH", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - app, - environment, - isActive, - appId, - targetEnvironment, - owner - }) - }).then(async (res) => { - if (res && res.status === 200) { - return (await res.json()).integration; - } - console.log("Failed to start an integration"); - return undefined; - }); - -export default updateIntegration; diff --git a/frontend/src/pages/api/organization/GetOrg.ts b/frontend/src/pages/api/organization/GetOrg.ts deleted file mode 100644 index 8009a4b3b..000000000 --- a/frontend/src/pages/api/organization/GetOrg.ts +++ /dev/null @@ -1,22 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route lets us get info about a certain org - * @param {string} orgId - the organization ID - * @returns - */ -const getOrganization = ({ orgId }: { orgId: string }) => - SecurityClient.fetchCall(`/api/v1/organization/${orgId}`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res?.status === 200) { - return (await res.json()).organization; - } - console.log("Failed to get org info"); - return undefined; - }); - -export default getOrganization; diff --git a/frontend/src/pages/api/organization/GetOrgProjectMemberships.ts b/frontend/src/pages/api/organization/GetOrgProjectMemberships.ts deleted file mode 100644 index 72c871c21..000000000 --- a/frontend/src/pages/api/organization/GetOrgProjectMemberships.ts +++ /dev/null @@ -1,24 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route lets us get all the project memebrships of users in an org. - * @param {*} req - * @param {*} res - * @returns - */ - -const getOrganizationProjectMemberships = (req: { orgId: string }) => - SecurityClient.fetchCall(`/api/v1/organization/${req.orgId}/workspace-memberships`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return res.json(); - } - console.log("Failed to get project memberships for users in an org"); - return undefined; - }); - -export default getOrganizationProjectMemberships; diff --git a/frontend/src/pages/api/organization/GetOrgProjects.ts b/frontend/src/pages/api/organization/GetOrgProjects.ts deleted file mode 100644 index 2e374e031..000000000 --- a/frontend/src/pages/api/organization/GetOrgProjects.ts +++ /dev/null @@ -1,25 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route lets us get all the users in an org. - * @param {*} req - * @param {*} res - * @returns - */ - -// TODO: this file is not used anywhere -const getOrganizationProjects = (req: { orgId: string }) => - SecurityClient.fetchCall(`/api/organization/${req.orgId}/workspaces`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return (await res.json()).workspaces; - } - console.log("Failed to get projects for an org"); - return undefined; - }); - -export default getOrganizationProjects; diff --git a/frontend/src/pages/api/organization/StripeRedirect.ts b/frontend/src/pages/api/organization/StripeRedirect.ts deleted file mode 100644 index 50a359b58..000000000 --- a/frontend/src/pages/api/organization/StripeRedirect.ts +++ /dev/null @@ -1,23 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route redirects the user to the right stripe billing page. - * @param {*} req - * @param {*} res - * @returns - */ -const StripeRedirect = ({ orgId }: { orgId: string }) => - SecurityClient.fetchCall(`/api/v1/organization/${orgId}/customer-portal-session`, { - method: "POST", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - window.location.href = (await res.json()).url; - return; - } - console.log("Failed to redirect to Stripe"); - }); - -export default StripeRedirect; diff --git a/frontend/src/pages/api/organization/addIncidentContact.ts b/frontend/src/pages/api/organization/addIncidentContact.ts deleted file mode 100644 index fe3407025..000000000 --- a/frontend/src/pages/api/organization/addIncidentContact.ts +++ /dev/null @@ -1,25 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route add an incident contact email to a certain organization - * @param {*} param0 - * @returns - */ -const addIncidentContact = (organizationId: string, email: string) => - SecurityClient.fetchCall(`/api/v1/organization/${organizationId}/incidentContactOrg`, { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - email - }) - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log("Failed to add an incident contact"); - return undefined; - }); - -export default addIncidentContact; diff --git a/frontend/src/pages/api/organization/changeUserRoleInOrganization.ts b/frontend/src/pages/api/organization/changeUserRoleInOrganization.ts deleted file mode 100644 index da9d20e0f..000000000 --- a/frontend/src/pages/api/organization/changeUserRoleInOrganization.ts +++ /dev/null @@ -1,27 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This function change the access of a user in a certain organization - * @param {string} organizationId - * @param {string} membershipId - * @param {string} role - * @returns - */ -const changeUserRoleInOrganization = (organizationId: string, membershipId: string, role: string) => - SecurityClient.fetchCall(`/api/v2/organizations/${organizationId}/memberships/${membershipId}`, { - method: "PATCH", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - role - }) - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log("Failed to change the user role in an org"); - return undefined; - }); - -export default changeUserRoleInOrganization; diff --git a/frontend/src/pages/api/organization/deleteIncidentContact.ts b/frontend/src/pages/api/organization/deleteIncidentContact.ts deleted file mode 100644 index 5375384c6..000000000 --- a/frontend/src/pages/api/organization/deleteIncidentContact.ts +++ /dev/null @@ -1,25 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route deletes an incident Contact from a certain organization - * @param {*} param0 - * @returns - */ -const deleteIncidentContact = (organizationId: string, email: string) => - SecurityClient.fetchCall(`/api/v1/organization/${organizationId}/incidentContactOrg`, { - method: "DELETE", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - email - }) - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log("Failed to delete an incident contact"); - return undefined; - }); - -export default deleteIncidentContact; diff --git a/frontend/src/pages/api/organization/deleteUserFromOrganization.ts b/frontend/src/pages/api/organization/deleteUserFromOrganization.ts deleted file mode 100644 index 3041b7b71..000000000 --- a/frontend/src/pages/api/organization/deleteUserFromOrganization.ts +++ /dev/null @@ -1,22 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This function removes a certain member from a certain organization - * @param {*} membershipId - * @returns - */ -const deleteUserFromOrganization = (membershipId: string) => - SecurityClient.fetchCall(`/api/v1/membership-org/${membershipId}`, { - method: "DELETE", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log("Failed to delete a user from an org"); - return undefined; - }); - -export default deleteUserFromOrganization; diff --git a/frontend/src/pages/api/organization/getIncidentContacts.ts b/frontend/src/pages/api/organization/getIncidentContacts.ts deleted file mode 100644 index 5ed7760ac..000000000 --- a/frontend/src/pages/api/organization/getIncidentContacts.ts +++ /dev/null @@ -1,27 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -export interface IIncidentContactOrg { - _id: string; - email: string; - organization: string; -} -/** - * This routes gets all the incident contacts of a certain organization - * @param {*} workspaceId - * @returns - */ -const getIncidentContacts = (organizationId: string): Promise => - SecurityClient.fetchCall(`/api/v1/organization/${organizationId}/incidentContactOrg`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return (await res.json()).incidentContactsOrg; - } - console.log("Failed to get incident contacts"); - return undefined; - }); - -export default getIncidentContacts; diff --git a/frontend/src/pages/api/organization/renameOrg.ts b/frontend/src/pages/api/organization/renameOrg.ts deleted file mode 100644 index 53fb7a51b..000000000 --- a/frontend/src/pages/api/organization/renameOrg.ts +++ /dev/null @@ -1,26 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route lets us rename a certain org. - * @param {*} req - * @param {*} res - * @returns - */ -const renameOrg = (orgId: string, newOrgName: string) => - SecurityClient.fetchCall(`/api/v1/organization/${orgId}/name`, { - method: "PATCH", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - name: newOrgName - }) - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log("Failed to rename an organization"); - return undefined; - }); - -export default renameOrg; diff --git a/frontend/src/pages/api/workspace/addUserToWorkspace.ts b/frontend/src/pages/api/workspace/addUserToWorkspace.ts deleted file mode 100644 index 19612ec1d..000000000 --- a/frontend/src/pages/api/workspace/addUserToWorkspace.ts +++ /dev/null @@ -1,26 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This function adds a user to a project - * @param {*} email - * @param {*} workspaceId - * @returns - */ -const addUserToWorkspace = (email: string, workspaceId: string) => - SecurityClient.fetchCall(`/api/v1/workspace/${workspaceId}/invite-signup`, { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - email - }) - }).then(async (res) => { - if (res && res.status === 200) { - return res.json(); - } - console.log("Failed to add a user to project"); - return undefined; - }); - -export default addUserToWorkspace; diff --git a/frontend/src/pages/api/workspace/changeUserRoleInWorkspace.ts b/frontend/src/pages/api/workspace/changeUserRoleInWorkspace.ts deleted file mode 100644 index 21ea91c45..000000000 --- a/frontend/src/pages/api/workspace/changeUserRoleInWorkspace.ts +++ /dev/null @@ -1,26 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This function change the access of a user in a certain workspace - * @param {*} membershipId - * @param {*} role - * @returns - */ -const changeUserRoleInWorkspace = (membershipId: string, role: string) => - SecurityClient.fetchCall(`/api/v1/membership/${membershipId}/change-role`, { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - role - }) - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log("Failed to change the user role in a project"); - return undefined; - }); - -export default changeUserRoleInWorkspace; diff --git a/frontend/src/pages/api/workspace/deleteUserFromWorkspace.ts b/frontend/src/pages/api/workspace/deleteUserFromWorkspace.ts deleted file mode 100644 index 33927f1c8..000000000 --- a/frontend/src/pages/api/workspace/deleteUserFromWorkspace.ts +++ /dev/null @@ -1,22 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This function removes a certain member from a certain workspace - * @param {*} membershipId - * @returns - */ -const deleteUserFromWorkspace = (membershipId: string) => - SecurityClient.fetchCall(`/api/v1/membership/${membershipId}`, { - method: "DELETE", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log("Failed to delete a user from a project"); - return undefined; - }); - -export default deleteUserFromWorkspace; diff --git a/frontend/src/pages/api/workspace/deleteWorkspace.ts b/frontend/src/pages/api/workspace/deleteWorkspace.ts deleted file mode 100644 index 7e1551613..000000000 --- a/frontend/src/pages/api/workspace/deleteWorkspace.ts +++ /dev/null @@ -1,22 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route deletes a specified workspace. - * @param {*} workspaceId - * @returns - */ -const deleteWorkspace = (workspaceId: string) => - SecurityClient.fetchCall(`/api/v1/workspace/${workspaceId}`, { - method: "DELETE", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res && res.status === 200) { - return res; - } - console.log("Failed to delete a project"); - return undefined; - }); - -export default deleteWorkspace; diff --git a/frontend/src/pages/api/workspace/getWorkspaceKeys.ts b/frontend/src/pages/api/workspace/getWorkspaceKeys.ts deleted file mode 100644 index e4c4475e9..000000000 --- a/frontend/src/pages/api/workspace/getWorkspaceKeys.ts +++ /dev/null @@ -1,22 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route lets us get the public keys of everyone in your workspace. - * @param {string} workspaceId - * @returns - */ -const getWorkspaceKeys = ({ workspaceId }: { workspaceId: string }) => - SecurityClient.fetchCall(`/api/v1/workspace/${workspaceId}/keys`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res?.status === 200) { - return (await res.json()).publicKeys; - } - console.log("Failed to get the public keys of everyone in the workspace"); - return undefined; - }); - -export default getWorkspaceKeys; diff --git a/frontend/src/pages/api/workspace/getWorkspaceTags.ts b/frontend/src/pages/api/workspace/getWorkspaceTags.ts deleted file mode 100644 index 535731bfb..000000000 --- a/frontend/src/pages/api/workspace/getWorkspaceTags.ts +++ /dev/null @@ -1,22 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route lets us get the tags for a certain project - * @param {string} workspaceId - * @returns - */ -const getWorkspaceTags = ({ workspaceId }: { workspaceId: string }) => - SecurityClient.fetchCall(`/api/v2/workspace/${workspaceId}/tags`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res?.status === 200) { - return (await res.json()).workspaceTags; - } - console.log("Failed to get the tags available in a certain project"); - return undefined; - }); - -export default getWorkspaceTags; diff --git a/frontend/src/pages/api/workspace/getWorkspaceUsers.ts b/frontend/src/pages/api/workspace/getWorkspaceUsers.ts deleted file mode 100644 index 4ef0a8dbc..000000000 --- a/frontend/src/pages/api/workspace/getWorkspaceUsers.ts +++ /dev/null @@ -1,22 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -/** - * This route lets us get all the users in the workspace. - * @param {string} workspaceId - workspace ID - * @returns - */ -const getWorkspaceUsers = ({ workspaceId }: { workspaceId: string }) => - SecurityClient.fetchCall(`/api/v1/workspace/${workspaceId}/users`, { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res?.status === 200) { - return (await res.json()).users; - } - console.log("Failed to get Project Users"); - return undefined; - }); - -export default getWorkspaceUsers; diff --git a/frontend/src/pages/api/workspace/getWorkspaces.ts b/frontend/src/pages/api/workspace/getWorkspaces.ts deleted file mode 100644 index 4a08c9d83..000000000 --- a/frontend/src/pages/api/workspace/getWorkspaces.ts +++ /dev/null @@ -1,31 +0,0 @@ -import SecurityClient from "@app/components/utilities/SecurityClient"; - -interface Workspace { - __v: number; - _id: string; - name: string; - autoCapitalization: boolean; - organization: string; - environments: Array<{ name: string; slug: string }>; -} - -/** - * This route lets us get the workspaces of a certain user - * @returns - */ -const getWorkspaces = () => - SecurityClient.fetchCall("/api/v1/workspace", { - method: "GET", - headers: { - "Content-Type": "application/json" - } - }).then(async (res) => { - if (res?.status === 200) { - const data = (await res.json()) as unknown as { workspaces: Workspace[] }; - return data.workspaces; - } - - throw new Error("Failed to get projects"); - }); - -export default getWorkspaces; diff --git a/frontend/src/pages/project/[id]/members/index.tsx b/frontend/src/pages/project/[id]/members/index.tsx index cbd13d208..5287c0430 100644 --- a/frontend/src/pages/project/[id]/members/index.tsx +++ b/frontend/src/pages/project/[id]/members/index.tsx @@ -11,15 +11,13 @@ import AddProjectMemberDialog from "@app/components/basic/dialog/AddProjectMembe import ProjectUsersTable from "@app/components/basic/table/ProjectUsersTable"; import guidGenerator from "@app/components/utilities/randomId"; import { Input } from "@app/components/v2"; -import { useGetUser } from "@app/hooks/api"; +import { useAddUserToWorkspace,useGetUser , useGetWorkspaceUsers } from "@app/hooks/api"; import { decryptAssymmetric, encryptAssymmetric } from "../../../../components/utilities/cryptography/crypto"; import getOrganizationUsers from "../../../api/organization/GetOrgUsers"; -import addUserToWorkspace from "../../../api/workspace/addUserToWorkspace"; -import getWorkspaceUsers from "../../../api/workspace/getWorkspaceUsers"; import uploadKeys from "../../../api/workspace/uploadKeys"; interface UserProps { @@ -42,7 +40,13 @@ interface MembershipProps { // #TODO: Update all the workspaceIds export default function Users() { + const router = useRouter(); + const workspaceId = router.query.id as string; + const { data: user } = useGetUser(); + const { data: workspaceUsers } = useGetWorkspaceUsers(workspaceId); + const { mutateAsync: addUserToWorkspaceMutateAsync } = useAddUserToWorkspace(); + const [isAddOpen, setIsAddOpen] = useState(false); // let [isDeleteOpen, setIsDeleteOpen] = useState(false); // let [userIdToBeDeleted, setUserIdToBeDeleted] = useState(false); @@ -52,22 +56,16 @@ export default function Users() { const { t } = useTranslation(); - const router = useRouter(); - const workspaceId = router.query.id as string; const [userList, setUserList] = useState([]); const [isUserListLoading, setIsUserListLoading] = useState(true); const [orgUserList, setOrgUserList] = useState([]); useEffect(() => { - if (user) { + if (user && workspaceUsers) { (async () => { setPersonalEmail(user.email); - - // This part quiries the current users of a project - const workspaceUsers = await getWorkspaceUsers({ - workspaceId - }); + const tempUserList = workspaceUsers.map((membership: MembershipProps) => ({ key: guidGenerator(), firstName: membership.user?.firstName, @@ -100,7 +98,7 @@ export default function Users() { ); })(); } - }, [user]); + }, [user, workspaceUsers]); const closeAddModal = () => { setIsAddOpen(false); @@ -123,7 +121,11 @@ export default function Users() { // } const submitAddModal = async () => { - const result = await addUserToWorkspace(email, workspaceId); + const result = await addUserToWorkspaceMutateAsync({ + email, + workspaceId + }); + if (result?.invitee && result?.latestKey) { const PRIVATE_KEY = localStorage.getItem("PRIVATE_KEY") as string; @@ -145,7 +147,6 @@ export default function Users() { } setEmail(""); setIsAddOpen(false); - router.reload(); }; return userList ? (