Commit 00be8817 by zhaochengxiang

权限

parent 3a605525
......@@ -4,7 +4,6 @@ import produce from "immer"
import { useDebounceEffect } from "ahooks"
import { dispatch } from '../../../../model'
import { UsersItem } from "./update-branch"
const FC = (props) => {
const { item, visible, onCancel } = props
......@@ -103,8 +102,6 @@ const FC = (props) => {
export default FC
export const Basic = React.forwardRef(function ({ cooperators, item }, ref) {
const [loading, setLoading] = React.useState(false)
const [users, setUsers] = React.useState()
const [form] = Form.useForm()
React.useImperativeHandle(ref, () => ({
......@@ -114,33 +111,60 @@ export const Basic = React.forwardRef(function ({ cooperators, item }, ref) {
}), [form])
React.useEffect(() => {
if (item?.id) {
getUsers()
}
}, [item])
React.useEffect(() => {
if ((cooperators??[]).length>0) {
form.setFieldsValue({ cooperators })
}
}, [cooperators])
return (
<Form
form={form}
labelCol={{ span: 3 }}
wrapperCol={{ span: 21 }}
autoComplete="off"
>
<Form.Item
name='cooperators'
label='选择用户'
extra="权限共享后,您与共享用户均可编辑该模型"
>
<UsersItem modelId={item?.id} />
</Form.Item>
</Form>
)
})
//注意 这里使用name作为唯一标识 id有可能为空
const UsersItem = ({ modelId, value, onChange, readonly = false }) => {
const [loading, setLoading] = React.useState(false)
const [searchValue, setSearchValue] = React.useState()
const [options, setOptions] = React.useState()
useDebounceEffect(() => {
if (searchValue) {
getUsers()
} else {
setOptions()
}
}, [searchValue], { wait: 300 })
const getUsers = () => {
setLoading(true)
dispatch({
type: 'datamodel.getCooperatorCandidates',
payload: {
id: item?.id
id: modelId,
match: searchValue,
},
callback: data => {
setLoading(false)
//id int转string
const newData = produce((data??[]).filter(item=>item.cooperatorCandidate), (draft) => {
draft.forEach(item => {
item.id = `${item.id}`
})
})
setUsers(newData)
setOptions(
(data??[]).map(item => ({
label: `${item.dname}(${item.name})`,
value: `${item.dname}(${item.name})`,
...item
}))
)
},
error: () => {
setLoading(false)
......@@ -149,19 +173,28 @@ export const Basic = React.forwardRef(function ({ cooperators, item }, ref) {
}
return (
<Form
form={form}
labelCol={{ span: 3 }}
wrapperCol={{ span: 21 }}
autoComplete="off"
>
<Form.Item
name='cooperators'
label='选择用户'
extra="权限共享后,您与共享用户均可编辑该模型"
>
<UsersItem loading={loading} users={users} />
</Form.Item>
</Form>
<>
{
readonly ? <span>{value?.map(item => `${item.dname}(${item.name})`).toString()}</span> : <Select loading={loading} mode='multiple' allowClear
placeholder='请选择用户'
value={(value??[]).map(item => `${item.dname}(${item.name})`)}
searchValue={searchValue}
onSearch={(val) => {
setSearchValue(val)
}}
onClear={() => {
setSearchValue()
}}
filterOption={false}
options={options}
onChange={(val) => {
onChange?.([
...(value??[]).filter(item => (val??[]).indexOf(`${item.dname}(${item.name})`) !== -1),
...(options??[]).filter(item => (val??[]).indexOf(item.value) !== -1)
])
}}
/>
}
</>
)
})
}
......@@ -102,8 +102,6 @@ const FC = (props) => {
export default FC
export const Basic = React.forwardRef(function ({ owner, item }, ref) {
const [loading, setLoading] = React.useState(false)
const [users, setUsers] = React.useState()
const [form] = Form.useForm()
React.useImperativeHandle(ref, () => ({
......@@ -113,40 +111,11 @@ export const Basic = React.forwardRef(function ({ owner, item }, ref) {
}), [form])
React.useEffect(() => {
if (item?.id) {
getUsers()
}
}, [item])
React.useEffect(() => {
if (owner) {
form.setFieldsValue({ owner })
}
}, [owner])
const getUsers = () => {
setLoading(true)
dispatch({
type: 'datamodel.getCooperatorCandidates',
payload: {
id: item?.id
},
callback: data => {
setLoading(false)
//id int转string
const newData = produce((data??[]).filter(item=>item.cooperatorCandidate), (draft) => {
draft.forEach(item => {
item.id = `${item.id}`
})
})
setUsers(newData)
},
error: () => {
setLoading(false)
}
})
}
return (
<Form
form={form}
......@@ -160,31 +129,53 @@ export const Basic = React.forwardRef(function ({ owner, item }, ref) {
extra="权限转移后,您将无法编辑该模型"
rules={[{ required: true, message: '请选择用户'}]}
>
<UsersItem loading={loading} users={users} />
<UsersItem modelId={item?.id} />
</Form.Item>
</Form>
)
})
const UsersItem = ({ loading, users, value, onChange }) => {
const UsersItem = ({ modelId, value, onChange }) => {
const [loading, setLoading] = React.useState(false)
const [searchValue, setSearchValue] = React.useState()
const [options, setOptions] = React.useState()
useDebounceEffect(() => {
setOptions(
(users??[])
.filter(item => !searchValue || (item.name??'').indexOf(searchValue)!==-1)
.map(item => ({
label: item.name,
value: item.id,
}))
)
}, [searchValue, users], { wait: 300 })
if (modelId && searchValue) {
getUsers()
} else {
setOptions()
}
}, [searchValue], { wait: 300 })
const getUsers = () => {
setLoading(true)
dispatch({
type: 'datamodel.getCooperatorCandidates',
payload: {
id: modelId,
match: searchValue,
},
callback: data => {
setLoading(false)
setOptions(
(data??[]).map(item => ({
label: `${item.dname}(${item.name})`,
value: `${item.dname}(${item.name})`,
...item
}))
)
},
error: () => {
setLoading(false)
}
})
}
return (
<Select loading={loading} showSearch allowClear
placeholder='请选择用户'
value={(options??[]).length>0?value?.id:undefined}
value={value?`${value.dname}(${value.name})`:undefined}
searchValue={searchValue}
onSearch={(val) => {
setSearchValue(val)
......@@ -196,11 +187,9 @@ const UsersItem = ({ loading, users, value, onChange }) => {
options={options}
onChange={(val) => {
if (val) {
const index = (users??[]).findIndex(item => item.id === val)
const index = (options??[]).findIndex(item => item.value === val)
if (index !== -1) {
onChange?.(users[index])
} else {
onChange?.()
onChange?.(options[index])
}
} else {
onChange?.()
......
......@@ -253,7 +253,7 @@ const Basic = React.forwardRef(function ({ type, item, user }, ref) {
})
//注意 这里使用name作为唯一标识 id有可能为空
export const UsersItem = ({ value, onChange, readonly = false }) => {
const UsersItem = ({ value, onChange, readonly = false }) => {
const [loading, setLoading] = React.useState(false)
const [searchValue, setSearchValue] = React.useState()
const [options, setOptions] = React.useState()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment