Skip to content

Commit

Permalink
account, users, roles and audits
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Oct 30, 2024
1 parent f8f97ff commit e71c93e
Show file tree
Hide file tree
Showing 14 changed files with 580 additions and 357 deletions.
9 changes: 5 additions & 4 deletions www/src/components/account/CreateRole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { appendConnection, updateCache } from '../../utils/graphql'

import SubscriptionContext from '../../contexts/SubscriptionContext'

import { CREATE_ROLE, ROLES_Q } from './queries'
import { ROLES_Q } from './queries'

import { Actions } from './Actions'
import { sanitize } from './utils'
import { RoleForm } from './RoleForm'
import BillingFeatureBlockModal from './billing/BillingFeatureBlockModal'
import { useCreateRoleMutation } from '../../generated/graphql'

const defaultAttributes = {
name: '',
Expand Down Expand Up @@ -42,15 +43,15 @@ export function CreateRole({ q }: any) {
setCreateModalVisible(false)
}, [])

const [mutation, { loading, error }] = useMutation(CREATE_ROLE, {
const [mutation, { loading, error }] = useCreateRoleMutation({
variables: {
attributes: { ...attributes, roleBindings: roleBindings.map(sanitize) },
},
update: (cache, { data: { createRole } }) =>
update: (cache, { data }) =>
updateCache(cache, {
query: ROLES_Q,
variables: { q },
update: (prev) => appendConnection(prev, createRole, 'roles'),
update: (prev) => appendConnection(prev, data?.createRole, 'roles'),
}),
onCompleted: () => resetAndClose(),
})
Expand Down
14 changes: 6 additions & 8 deletions www/src/components/account/CreateServiceAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ import SubscriptionContext from '../../contexts/SubscriptionContext'

import { Confirm } from '../utils/Confirm'

import {
CREATE_SERVICE_ACCOUNT,
UPDATE_SERVICE_ACCOUNT,
USERS_Q,
} from './queries'
import { UPDATE_SERVICE_ACCOUNT, USERS_Q } from './queries'

import { MoreMenu } from './MoreMenu'

import { BindingInput } from './Typeaheads'
import { sanitize } from './utils'
import BillingFeatureBlockModal from './billing/BillingFeatureBlockModal'
import { useCreateServiceAccountMutation } from '../../generated/graphql'

function ServiceAccountForm({
error,
Expand Down Expand Up @@ -274,18 +271,19 @@ export function CreateServiceAccount({ q }: any) {
setCreateModalVisible(false)
}, [])

const [mutation, { loading, error }] = useMutation(CREATE_SERVICE_ACCOUNT, {
const [mutation, { loading, error }] = useCreateServiceAccountMutation({
variables: {
attributes: {
...attributes,
impersonationPolicy: { bindings: bindings.map(sanitize) },
},
},
update: (cache, { data: { createServiceAccount } }) =>
update: (cache, { data }) =>
updateCache(cache, {
query: USERS_Q,
variables: { q, serviceAccount: true },
update: (prev) => appendConnection(prev, createServiceAccount, 'users'),
update: (prev) =>
appendConnection(prev, data?.createServiceAccount, 'users'),
}),
onCompleted: () => {
resetAndClose()
Expand Down
6 changes: 2 additions & 4 deletions www/src/components/account/EditRole.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { useMutation } from '@apollo/client'
import { Button } from 'honorable'
import { Modal } from '@pluralsh/design-system'
import { useMemo, useState } from 'react'
import uniqWith from 'lodash/uniqWith'
import isEqual from 'lodash/isEqual'

import { UPDATE_ROLE } from './queries'

import { Actions } from './Actions'
import { sanitize } from './utils'
import { RoleForm } from './RoleForm'
import { useUpdateRoleMutation } from '../../generated/graphql'

export function EditRole({ role }: any) {
const [open, setOpen] = useState(false)
Expand All @@ -25,7 +23,7 @@ export function EditRole({ role }: any) {
[roleBindings]
)

const [mutation, { loading, error }] = useMutation(UPDATE_ROLE, {
const [mutation, { loading, error }] = useUpdateRoleMutation({
variables: {
id: role.id,
attributes: { ...attributes, roleBindings: roleBindings.map(sanitize) },
Expand Down
7 changes: 3 additions & 4 deletions www/src/components/account/InviteUser.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMutation } from '@apollo/client'
import {
Codeline,
MailIcon,
Expand All @@ -15,8 +14,8 @@ import { useCurrentUser } from '../../contexts/CurrentUserContext'
import SubscriptionContext from '../../contexts/SubscriptionContext'
import { GqlError } from '../utils/Alert'

import { CREATE_INVITE } from './queries'
import { inviteLink } from './utils'
import { useCreateInviteMutation } from '../../generated/graphql'

const MAX_OPEN_SOURCE_USERS = 5

Expand All @@ -27,10 +26,10 @@ export function InviteUser({ refetch }: { refetch?: (() => void) | null }) {
const [invite, setInvite] = useState<any>(null)
const { account } = useCurrentUser()
const { isGrandfathered, isPaidPlan } = useContext(SubscriptionContext)
const [mutation, { loading, error, reset }] = useMutation(CREATE_INVITE, {
const [mutation, { loading, error, reset }] = useCreateInviteMutation({
variables: { attributes: { email } },
onCompleted: (data) => {
setInvite(data && data.createInvite)
setInvite(data?.createInvite)
refetch?.()
},
})
Expand Down
7 changes: 4 additions & 3 deletions www/src/components/account/Invites.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation, useQuery } from '@apollo/client'
import { useQuery } from '@apollo/client'
import moment from 'moment'
import { useEffect, useState } from 'react'
import { Div, Flex, Text } from 'honorable'
Expand All @@ -23,12 +23,13 @@ import LoadingIndicator from '../utils/LoadingIndicator'

import { Confirm } from '../utils/Confirm'

import { DELETE_INVITE, INVITES_Q } from './queries'
import { INVITES_Q } from './queries'
import { inviteLink } from './utils'
import { useDeleteInviteMutation } from '../../generated/graphql'

function DeleteInvite({ invite }: any) {
const [confirm, setConfirm] = useState(false)
const [mutation, { loading, error }] = useMutation(DELETE_INVITE, {
const [mutation, { loading, error }] = useDeleteInviteMutation({
variables: { id: invite.id },
onCompleted: () => setConfirm(false),
update: (cache, { data: { deleteInvite } }) =>
Expand Down
10 changes: 5 additions & 5 deletions www/src/components/account/Roles.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation, useQuery } from '@apollo/client'
import { useQuery } from '@apollo/client'
import { Box } from 'grommet'
import isEmpty from 'lodash/isEmpty'
import { Flex } from 'honorable'
Expand All @@ -16,7 +16,7 @@ import {
} from '../../utils/graphql'
import { DeleteIconButton } from '../utils/IconButtons'
import { StandardScroller } from '../utils/SmoothScroller'
import { Permission } from '../../generated/graphql'
import { Permission, useDeleteRoleMutation } from '../../generated/graphql'
import { canEdit } from '../../utils/account'
import LoadingIndicator from '../utils/LoadingIndicator'

Expand All @@ -26,7 +26,7 @@ import { Confirm } from '../utils/Confirm'

import BillingTrialBanner from './billing/BillingTrialBanner'

import { DELETE_ROLE, ROLES_Q } from './queries'
import { ROLES_Q } from './queries'
import { hasRbac } from './utils'
import { Info } from './Info'
import { EditRole } from './EditRole'
Expand All @@ -51,13 +51,13 @@ function Role({ role, q }: any) {
const [confirm, setConfirm] = useState(false)
const me = useContext(CurrentUserContext)
const editable = canEdit(me, me.account) || hasRbac(me, Permission.Users)
const [mutation, { loading, error }] = useMutation(DELETE_ROLE, {
const [mutation, { loading, error }] = useDeleteRoleMutation({
variables: { id: role.id },
update: (cache, { data }) =>
updateCache(cache, {
query: ROLES_Q,
variables: { q },
update: (prev) => removeConnection(prev, data.deleteRole, 'roles'),
update: (prev) => removeConnection(prev, data?.deleteRole, 'roles'),
}),
onCompleted: () => setConfirm(false),
})
Expand Down
84 changes: 0 additions & 84 deletions www/src/components/account/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,55 +81,6 @@ export const SEARCH_GROUPS = gql`
${GroupFragment}
`

export const CREATE_INVITE = gql`
mutation CreateInvite($attributes: InviteAttributes!) {
createInvite(attributes: $attributes) {
...InviteFragment
}
}
${InviteFragment}
`

export const CREATE_ROLE = gql`
mutation CreateRole($attributes: RoleAttributes!) {
createRole(attributes: $attributes) {
...RoleFragment
}
}
${RoleFragment}
`

export const UPDATE_ROLE = gql`
mutation UpdateRole($id: ID!, $attributes: RoleAttributes!) {
updateRole(id: $id, attributes: $attributes) {
...RoleFragment
}
}
${RoleFragment}
`

export const DELETE_ROLE = gql`
mutation DeleteRow($id: ID!) {
deleteRole(id: $id) {
...RoleFragment
}
}
${RoleFragment}
`

export const CREATE_SERVICE_ACCOUNT = gql`
mutation Create($attributes: ServiceAccountAttributes!) {
createServiceAccount(attributes: $attributes) {
...UserFragment
impersonationPolicy {
...ImpersonationPolicy
}
}
}
${ImpersonationPolicy}
${UserFragment}
`

export const UPDATE_SERVICE_ACCOUNT = gql`
mutation Create($id: ID!, $attributes: ServiceAccountAttributes!) {
updateServiceAccount(id: $id, attributes: $attributes) {
Expand Down Expand Up @@ -160,23 +111,6 @@ export const ROLES_Q = gql`
${RoleFragment}
`

export const AUDITS_Q = gql`
query Audits($cursor: String) {
audits(first: 50, after: $cursor) {
pageInfo {
...PageInfo
}
edges {
node {
...AuditFragment
}
}
}
}
${PageInfo}
${AuditFragment}
`

export const DNS_DOMAINS = gql`
query Domains($cursor: String) {
dnsDomains(after: $cursor, first: 50) {
Expand Down Expand Up @@ -215,15 +149,6 @@ export const DNS_RECORDS = gql`
${DnsRecordFragment}
`

export const CREATE_DOMAIN = gql`
mutation Create($attributes: DnsDomainAttributes!) {
createDomain(attributes: $attributes) {
...DnsDomainFragment
}
}
${DnsDomainFragment}
`

export const UPDATE_DOMAIN = gql`
mutation Update($id: ID!, $attributes: DnsDomainAttributes!) {
updateDomain(id: $id, attributes: $attributes) {
Expand Down Expand Up @@ -267,12 +192,3 @@ export const INVITES_Q = gql`
${PageInfo}
${InviteFragment}
`

export const DELETE_INVITE = gql`
mutation Delete($id: ID!) {
deleteInvite(id: $id) {
...InviteFragment
}
}
${InviteFragment}
`
5 changes: 2 additions & 3 deletions www/src/components/audits/Audits.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { memo, useCallback, useMemo } from 'react'
import { Box } from 'grommet'
import { Link } from 'react-router-dom'
import { useQuery } from '@apollo/client'
import { A, Div } from 'honorable'
import { Date, PageTitle, Table } from '@pluralsh/design-system'
import { createColumnHelper } from '@tanstack/react-table'
import isEmpty from 'lodash/isEmpty'

import { extendConnection } from '../../utils/graphql'
import { AUDITS_Q } from '../account/queries'
import LoadingIndicator from '../utils/LoadingIndicator'

import { AuditUser } from './AuditUser'
import { Location } from './Location'
import { useAuditsQuery } from '../../generated/graphql'

const FETCH_MARGIN = 30

Expand Down Expand Up @@ -133,7 +132,7 @@ const AuditsTable = memo(({ audits, fetchMoreOnBottomReached }: any) =>
)

export function Audits() {
const { data, loading, fetchMore } = useQuery(AUDITS_Q, {
const { data, loading, fetchMore } = useAuditsQuery({
fetchPolicy: 'cache-and-network',
})
const pageInfo = data?.audits?.pageInfo
Expand Down
Loading

0 comments on commit e71c93e

Please sign in to comment.