1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-30 04:25:47 +00:00
seahub/frontend/src/components/select-editor/share-permission-editor.js

139 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-01-09 06:39:17 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import { Utils } from '../../utils/utils';
import SelectEditor from './select-editor';
import { seafileAPI } from '../../utils/seafile-api';
import CustomPermission from '../../models/custom-permission';
import toaster from '../toast';
import { isPro } from '../../utils/constants';
2019-01-09 06:39:17 +00:00
const propTypes = {
repoID: PropTypes.string,
2019-01-09 06:39:17 +00:00
isTextMode: PropTypes.bool.isRequired,
isEditIconShow: PropTypes.bool.isRequired,
permissions: PropTypes.array.isRequired,
currentPermission: PropTypes.string.isRequired,
onPermissionChanged: PropTypes.func.isRequired,
enableAddCustomPermission: PropTypes.bool,
onAddCustomPermissionToggle: PropTypes.func,
2019-01-09 06:39:17 +00:00
};
2019-01-11 04:41:30 +00:00
class SharePermissionEditor extends React.Component {
2019-01-09 06:39:17 +00:00
constructor(props) {
super(props);
this.state = {
isLoading: true,
customPermissions: []
};
this.customPermissions = null;
const { repoID } = this.props;
this.CACHE_KEY = repoID ? `custom_permissions_${repoID}` : '';
}
componentDidMount() {
if (this.props.repoID && isPro) {
this.listCustomPermissions();
} else {
this.setState({
isLoading: false,
customPermissions: [],
});
}
2019-01-09 06:39:17 +00:00
}
componentWillUnmount() {
isPro && localStorage.removeItem(this.CACHE_KEY);
}
listCustomPermissions = () => {
const { repoID } = this.props;
const cacheData = localStorage.getItem(this.CACHE_KEY);
if (cacheData) {
const { permission_list: permissions } = JSON.parse(cacheData);
const customPermissions = permissions.map(item => new CustomPermission(item));
this.setState({
isLoading: false,
customPermissions: customPermissions
});
return;
}
seafileAPI.listCustomPermissions(repoID).then(res => {
const { permission_list: permissions } = res.data;
localStorage.setItem(this.CACHE_KEY, JSON.stringify(res.data));
const customPermissions = permissions.map(item => new CustomPermission(item));
this.setState({
isLoading: false,
customPermissions: customPermissions
});
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
this.setState({isLoading: false});
});
}
translatePermission = (permission) => {
let value = Utils.sharePerms(permission);
if (!value) {
const { customPermissions } = this.state;
const item = customPermissions.find(item => item.id + '' === permission);
value = item && item.name;
}
return value;
}
2019-01-24 09:15:01 +00:00
translateExplanation = (explanation) => {
let value = Utils.sharePermsExplanation(explanation);
if (!value) {
const { customPermissions } = this.state;
const item = customPermissions.find(item => item.id + '' === explanation);
value = item && item.description;
}
return value;
}
getPermissions = () => {
const { permissions } = this.props;
let newPermissions = permissions.slice();
const { customPermissions } = this.state;
if (!this.customPermissions) {
if (customPermissions.length > 0) {
customPermissions.forEach(item => {
newPermissions.push(item.id + '');
});
}
this.customPermissions = newPermissions;
}
return this.customPermissions;
2019-01-24 09:15:01 +00:00
}
2019-01-09 06:39:17 +00:00
render() {
const { isLoading } = this.state;
if (isLoading) {
return null;
}
2019-01-09 06:39:17 +00:00
return (
<SelectEditor
2019-01-09 06:39:17 +00:00
isTextMode={this.props.isTextMode}
isEditIconShow={this.props.isEditIconShow}
options={this.getPermissions()}
2019-01-09 06:39:17 +00:00
currentOption={this.props.currentPermission}
2019-01-14 05:33:36 +00:00
onOptionChanged={this.props.onPermissionChanged}
2019-01-09 06:39:17 +00:00
translateOption={this.translatePermission}
2019-01-24 09:15:01 +00:00
translateExplanation={this.translateExplanation}
enableAddCustomPermission={this.props.enableAddCustomPermission}
onAddCustomPermissionToggle={this.props.onAddCustomPermissionToggle}
2019-01-09 06:39:17 +00:00
/>
);
}
}
2019-01-11 04:41:30 +00:00
SharePermissionEditor.propTypes = propTypes;
2019-01-09 06:39:17 +00:00
2019-01-11 04:41:30 +00:00
export default SharePermissionEditor;