Skip to content

Commit

Permalink
Merge branch 'develop' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
Ponchimeow committed Jan 13, 2025
2 parents a3d1123 + 2b40a2e commit 6bf18cc
Show file tree
Hide file tree
Showing 18 changed files with 453 additions and 250 deletions.
4 changes: 2 additions & 2 deletions src/components/member/MemberPermissionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import hasura from '../../hasura'
import { handleError } from '../../helpers'
import { commonMessages, permissionGroupsAdminMessages } from '../../helpers/translation'
import { useDefaultPermissions } from '../../hooks/permission'
import { PermissionGroupProps } from '../../types/general'
import { PermissionGroup } from '../../types/general'
import { MemberAdminProps, UserRole } from '../../types/member'
import PermissionGroupSelector from '../form/PermissionGroupSelector'
import PermissionInput from '../form/PermissionInput'
Expand All @@ -21,7 +21,7 @@ type FieldProps = {
}

const MemberPermissionForm: React.FC<{
memberAdmin: (MemberAdminProps & { permissionGroups: Pick<PermissionGroupProps, 'id' | 'name'>[] }) | null
memberAdmin: (MemberAdminProps & { permissionGroups: Pick<PermissionGroup, 'id' | 'name'>[] }) | null
onRefetch?: () => void
}> = ({ memberAdmin, onRefetch }) => {
const { formatMessage } = useIntl()
Expand Down
22 changes: 14 additions & 8 deletions src/components/permission/PermissionGroupAdminItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { EditOutlined } from '@ant-design/icons'
import { Button } from 'antd'
import { useIntl } from 'react-intl'
import styled from 'styled-components'
import { permissionGroupsAdminMessages } from '../../helpers/translation'
import { PermissionGroupProps } from '../../types/general'
import { PermissionGroup } from '../../types/general'
import PermissionGroupAdminModal from './PermissionGroupAdminModal'
import PermissionGroupDeletionModal from './PermissionGroupDeletionModal'
import permissionMessages from './translation'

const StyledAdminBlock = styled.div`
margin-bottom: 1.25rem;
Expand All @@ -15,11 +15,11 @@ const StyledAdminBlock = styled.div`
box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.06);
`

const PermissionGroupAdminItem: React.VFC<
PermissionGroupProps & {
const PermissionGroupAdminItem: React.FC<
PermissionGroup & {
onRefetch?: () => void
}
> = ({ id, name, permissionGroupPermissions, onRefetch }) => {
} & { existedPermissionGroupNames: string[] }
> = ({ id, name, existedPermissionGroupNames, permissionGroupPermissions, onRefetch }) => {
const { formatMessage } = useIntl()

return (
Expand All @@ -28,16 +28,22 @@ const PermissionGroupAdminItem: React.VFC<
<div>{name}</div>
<div className="d-flex align-items-center">
<PermissionGroupAdminModal
title={formatMessage(permissionGroupsAdminMessages.ui.editPermissionGroup)}
title={formatMessage(permissionMessages['*'].editPermissionGroup)}
renderTrigger={({ setVisible }) => (
<Button type="link" icon={<EditOutlined />} onClick={() => setVisible(true)} />
)}
id={id}
name={name}
existedPermissionGroupNames={existedPermissionGroupNames}
permissionGroupPermissions={permissionGroupPermissions}
onRefetch={onRefetch}
/>
<PermissionGroupDeletionModal
id={id}
name={name}
permissionGroupPermissions={permissionGroupPermissions}
onRefetch={onRefetch}
/>
<PermissionGroupDeletionModal id={id || ''} onRefetch={onRefetch} />
</div>
</div>
</StyledAdminBlock>
Expand Down
164 changes: 86 additions & 78 deletions src/components/permission/PermissionGroupAdminModal.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { FileAddOutlined } from '@ant-design/icons'
import { useMutation } from '@apollo/client'
import { Button, Form, Input, message, Skeleton } from 'antd'
import { useForm } from 'antd/lib/form/Form'
import { gql } from '@apollo/client'
import { useApp } from 'lodestar-app-element/src/contexts/AppContext'
import { useAuth } from 'lodestar-app-element/src/contexts/AuthContext'
import React, { useState } from 'react'
import { useIntl } from 'react-intl'
import hasura from '../../hasura'
import { handleError } from '../../helpers'
import { commonMessages, errorMessages } from '../../helpers/translation'
import { useDefaultPermissions } from '../../hooks/permission'
import { PermissionGroupProps } from '../../types/general'
import {
useDefaultPermissions,
useMutationPermissionGroup,
useMutationPermissionGroupAuditLog,
useMutationPermissionGroupPermission,
} from '../../hooks/permission'
import { PermissionGroup } from '../../types/general'
import AdminModal, { AdminModalProps } from '../admin/AdminModal'
import PermissionInput from '../form/PermissionInput'
import permissionMessages from './translation'

type FieldProps = {
name: string
Expand All @@ -21,73 +24,114 @@ type FieldProps = {

const PermissionGroupAdminModal: React.FC<
AdminModalProps &
Partial<PermissionGroupProps> & {
Partial<PermissionGroup> & {
onRefetch?: () => void
}
> = ({ id, name, permissionGroupPermissions, onRefetch, ...props }) => {
} & { existedPermissionGroupNames: string[] }
> = ({ id, name, permissionGroupPermissions, existedPermissionGroupNames, onRefetch, ...props }) => {
const { formatMessage } = useIntl()
const { id: appId } = useApp()
const { currentMemberId } = useAuth()
const [form] = useForm<FieldProps>()
const [loading, setLoading] = useState(false)
const { loadingPermissions, defaultRolePermissions } = useDefaultPermissions()
const [insertPermissionGroup] = useMutation<hasura.INSERT_PERMISSION_GROUP, hasura.INSERT_PERMISSION_GROUPVariables>(
INSERT_PERMISSION_GROUP,
)
const [updatePermissionGroup] = useMutation<hasura.UPDATE_PERMISSION_GROUP, hasura.UPDATE_PERMISSION_GROUPVariables>(
UPDATE_PERMISSION_GROUP,
)
const [insertPermissionGroupPermission] = useMutation<
hasura.INSERT_PERMISSION_GROUP_PERMISSION,
hasura.INSERT_PERMISSION_GROUP_PERMISSIONVariables
>(INSERT_PERMISSION_GROUP_PERMISSION)
const [updatePermissionGroupPermission] = useMutation<
hasura.UPDATE_PERMISSION_GROUP_PERMISSION,
hasura.UPDATE_PERMISSION_GROUP_PERMISSIONVariables
>(UPDATE_PERMISSION_GROUP_PERMISSION)
const { insertPermissionGroup, updatePermissionGroup } = useMutationPermissionGroup()
const { insertPermissionGroupPermission, updatePermissionGroupPermission } = useMutationPermissionGroupPermission()
const { insertPermissionGroupAuditLog } = useMutationPermissionGroupAuditLog()

const handleSubmit = (onSuccess: () => void) => {
form
.validateFields()
.then(() => {
setLoading(true)
const values = form.getFieldsValue()

if (existedPermissionGroupNames.includes(values.name.trim())) {
return message.error(formatMessage(permissionMessages.PermissionGroupAdminModal.duplicateName))
}
const oldName = name
const oldPermissionGroupPermissions =
permissionGroupPermissions?.map(permissionGroupPermission => ({
permission_group_id: id,
permission_id: permissionGroupPermission.permissionId,
})) || []
const newPermissionGroupPermissions =
values.permissionIds.map(permissionId => ({
permission_group_id: id,
permission_id: permissionId,
})) || []
const newName = values.name

if (id) {
updatePermissionGroup({ variables: { id: id, name: values.name } })
updatePermissionGroup({ variables: { id: id, name: newName } })
insertPermissionGroupAuditLog({
variables: {
permissionGroupAuditLog: {
app_id: appId,
target: id,
member_id: currentMemberId || '',
action: 'UPDATE',
old: {
name: oldName,
permissionsGroupPermissions: oldPermissionGroupPermissions,
},
new: {
name: newName,
permissionsGroupPermissions: newPermissionGroupPermissions,
},
},
},
})
updatePermissionGroupPermission({
variables: {
permissionGroupPermissionId: id,
permissionGroups:
values.permissionIds.map(permissionId => ({
permission_group_id: id,
permission_id: permissionId,
})) || [],
permissionsGroupPermissions: newPermissionGroupPermissions,
},
}).then(() => {
message.success(formatMessage(commonMessages.event.successfullyEdited))
message.success(formatMessage(permissionMessages.PermissionGroupAdminModal.successfullyEdited))
onRefetch?.()
})
} else {
insertPermissionGroup({ variables: { appId: appId, name: values.name } }).then(({ data }) => {
const permissionGroupId = data?.insert_permission_group?.returning[0].id
insertPermissionGroupAuditLog({
variables: {
permissionGroupAuditLog: {
app_id: appId,
target: permissionGroupId,
member_id: currentMemberId || '',
action: 'INSERT',
old: {
name: oldName,
permissionsGroupPermissions: oldPermissionGroupPermissions,
},
new: {
name: newName,
permissionsGroupPermissions: newPermissionGroupPermissions,
},
},
},
})
insertPermissionGroupPermission({
variables: {
permissionGroups: values.permissionIds
permissionsGroupPermissions: values.permissionIds
? values.permissionIds.map(permissionId => ({
permission_group_id: permissionGroupId,
permission_id: permissionId,
}))
: [],
},
}).then(() => {
message.success(formatMessage(commonMessages.event.successfullyCreated))
message.success(formatMessage(permissionMessages.PermissionGroupAdminModal.successfullyCreated))
onRefetch?.()
})
})
}
form.resetFields()
onSuccess()
})
.catch(handleError)
.catch(error => {
handleError(error)
})
.finally(() => setLoading(false))
}

Expand All @@ -103,10 +147,10 @@ const PermissionGroupAdminModal: React.FC<
renderFooter={({ setVisible }) => (
<>
<Button className="mr-2" onClick={() => setVisible(false)}>
{formatMessage(commonMessages.ui.cancel)}
{formatMessage(permissionMessages['*'].cancel)}
</Button>
<Button type="primary" loading={loading} onClick={() => handleSubmit(() => setVisible(false))}>
{formatMessage(commonMessages.ui.save)}
{formatMessage(permissionMessages.PermissionGroupAdminModal.save)}
</Button>
</>
)}
Expand All @@ -125,64 +169,28 @@ const PermissionGroupAdminModal: React.FC<
}}
>
<Form.Item
label={formatMessage(commonMessages.label.name)}
label={formatMessage(permissionMessages.PermissionGroupAdminModal.name)}
name="name"
rules={[
{
required: true,
message: formatMessage(errorMessages.form.isRequired, {
field: formatMessage(commonMessages.label.name),
message: formatMessage(permissionMessages.PermissionGroupAdminModal.isRequired, {
field: formatMessage(permissionMessages.PermissionGroupAdminModal.name),
}),
},
]}
>
<Input />
</Form.Item>
<Form.Item label={formatMessage(commonMessages.label.permissionSettings)} name="permissionIds">
<Form.Item
label={formatMessage(permissionMessages.PermissionGroupAdminModal.permissionSettings)}
name="permissionIds"
>
<PermissionInput />
</Form.Item>
</Form>
</AdminModal>
)
}

const INSERT_PERMISSION_GROUP = gql`
mutation INSERT_PERMISSION_GROUP($appId: String, $name: String) {
insert_permission_group(objects: { app_id: $appId, name: $name }) {
affected_rows
returning {
id
}
}
}
`
const UPDATE_PERMISSION_GROUP = gql`
mutation UPDATE_PERMISSION_GROUP($id: uuid, $name: String) {
update_permission_group(where: { id: { _eq: $id } }, _set: { name: $name }) {
affected_rows
}
}
`

const INSERT_PERMISSION_GROUP_PERMISSION = gql`
mutation INSERT_PERMISSION_GROUP_PERMISSION($permissionGroups: [permission_group_permission_insert_input!]!) {
insert_permission_group_permission(objects: $permissionGroups) {
affected_rows
}
}
`
const UPDATE_PERMISSION_GROUP_PERMISSION = gql`
mutation UPDATE_PERMISSION_GROUP_PERMISSION(
$permissionGroupPermissionId: uuid!
$permissionGroups: [permission_group_permission_insert_input!]!
) {
delete_permission_group_permission(where: { permission_group_id: { _eq: $permissionGroupPermissionId } }) {
affected_rows
}
insert_permission_group_permission(objects: $permissionGroups) {
affected_rows
}
}
`

export default PermissionGroupAdminModal
Loading

0 comments on commit 6bf18cc

Please sign in to comment.