PermSelect.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { useModal } from '@/components/Modal'
  2. import { useModel } from 'umi'
  3. import { Select } from 'antd'
  4. import CustomPerm from './CustomPerm'
  5. import { getAuthCache, setAuthCache } from '@/utils/auth'
  6. import { PERMISSION_SELECT_KEY } from '@/utils/cache/cacheEnum'
  7. import type { SelectProps } from 'antd'
  8. import type { MainMenuKeyEnum } from '@/utils/cache/cacheEnum'
  9. export const PermDataTypeEunm = {
  10. CUSTOMER: 'customer',
  11. PRODUCT: 'product',
  12. HR: 'hr',
  13. WORKBENCH: 'workbench'
  14. }
  15. interface PermSelectProps extends SelectProps {
  16. dataType: MainMenuKeyEnum
  17. onConfirm: () => void
  18. }
  19. const PermSelect: React.FC<PermSelectProps> = ({ dataType, onConfirm, ...resetProps }) => {
  20. const { initialState: { permData } = { permData: {} } } = useModel('@@initialState')
  21. const { toggleModal, setModalProps } = useModal()
  22. let defaultValue = 'oneself'
  23. const options = [...(permData[dataType] || [])]
  24. const permAuthCache = getAuthCache(PERMISSION_SELECT_KEY)
  25. if (permAuthCache && permAuthCache[dataType]) {
  26. defaultValue = permAuthCache[dataType]
  27. }
  28. const handleOnChange = e => {
  29. if (e === 'custom') {
  30. setModalProps({
  31. width: '60vw',
  32. modalRender: () => <CustomPerm onConfirm={onConfirm} />,
  33. onCancel: toggleModal
  34. })
  35. toggleModal()
  36. return
  37. }
  38. onConfirm({ staffIds: null, dataPermission: e })
  39. setAuthCache(PERMISSION_SELECT_KEY, { ...permAuthCache, [dataType]: e })
  40. }
  41. return (
  42. <Select
  43. {...resetProps}
  44. defaultValue={defaultValue}
  45. dropdownMatchSelectWidth={false}
  46. onChange={handleOnChange}
  47. options={options}
  48. />
  49. )
  50. }
  51. export default PermSelect