|
| 1 | +/* |
| 2 | + * |
| 3 | + * PermissionCell |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +import React from 'react' |
| 8 | +import R from 'ramda' |
| 9 | +import PropTypes from 'prop-types' |
| 10 | +import shortid from 'shortid' |
| 11 | + |
| 12 | +import { makeDebugger, isEmptyNil, isObject } from '../../utils' |
| 13 | +import { |
| 14 | + Wrapper, |
| 15 | + Number, |
| 16 | + RootNumber, |
| 17 | + NoneText, |
| 18 | + Label, |
| 19 | + UnitText, |
| 20 | + NumberInfo, |
| 21 | + PermissionWrapper, |
| 22 | +} from './styles' |
| 23 | +/* eslint-disable no-unused-vars */ |
| 24 | +const debug = makeDebugger('c:PermissionCell:index') |
| 25 | +/* eslint-enable no-unused-vars */ |
| 26 | + |
| 27 | +const valueIsObj = v => isObject(v) |
| 28 | +const valueIsNotObj = R.complement(valueIsObj) |
| 29 | + |
| 30 | +const objToArray = input => |
| 31 | + Object.keys(input).map(key => { |
| 32 | + return { [key]: input[key] } |
| 33 | + }) |
| 34 | + |
| 35 | +const key = R.compose(R.head, R.keys) |
| 36 | +const value = R.compose(R.head, R.values) |
| 37 | + |
| 38 | +const CommunityPermissions = ({ data }) => { |
| 39 | + if (!data) return <div /> |
| 40 | + const dataArray = objToArray(data) |
| 41 | + |
| 42 | + return ( |
| 43 | + <React.Fragment> |
| 44 | + {dataArray.map(v => ( |
| 45 | + <PermissionWrapper key={shortid.generate()}> |
| 46 | + <Label>{key(v)}: </Label> |
| 47 | + <NumberInfo> |
| 48 | + <Number>{R.length(R.keys(value(v)))}</Number> |
| 49 | + <UnitText>项</UnitText> |
| 50 | + </NumberInfo> |
| 51 | + </PermissionWrapper> |
| 52 | + ))} |
| 53 | + </React.Fragment> |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +const RootPermissions = ({ data }) => { |
| 58 | + if (isEmptyNil(data)) return <div /> |
| 59 | + const plength = R.keys(data) |
| 60 | + |
| 61 | + return ( |
| 62 | + <PermissionWrapper> |
| 63 | + <Label>system: </Label> |
| 64 | + <NumberInfo> |
| 65 | + <RootNumber>{plength.length}</RootNumber> |
| 66 | + <UnitText>项</UnitText> |
| 67 | + </NumberInfo> |
| 68 | + </PermissionWrapper> |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +const PermissionCell = ({ source, onClick }) => { |
| 73 | + const cmsps = source.cmsPassportString |
| 74 | + if (isEmptyNil(cmsps)) { |
| 75 | + return <NoneText>暂无</NoneText> |
| 76 | + } |
| 77 | + const pjson = JSON.parse(cmsps) |
| 78 | + const cdata = R.pickBy(valueIsObj, pjson) |
| 79 | + const rdata = R.pickBy(valueIsNotObj, pjson) |
| 80 | + |
| 81 | + return ( |
| 82 | + <Wrapper onClick={onClick.bind(this, source)}> |
| 83 | + <CommunityPermissions data={cdata} /> |
| 84 | + <RootPermissions data={rdata} /> |
| 85 | + </Wrapper> |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +PermissionCell.propTypes = { |
| 90 | + // https://www.npmjs.com/package/prop-types |
| 91 | + onClick: PropTypes.func.isRequired, |
| 92 | + source: PropTypes.object.isRequired, |
| 93 | +} |
| 94 | + |
| 95 | +PermissionCell.defaultProps = {} |
| 96 | + |
| 97 | +export default PermissionCell |
0 commit comments