Commit 0722ea04 by Johannes Schill Committed by Daniel Lee

dashfolders: Clean up more variables and move newType, aclTypes and…

dashfolders: Clean up more variables and move newType, aclTypes and permissionOptions to the store #10275
parent 4ea9f302
...@@ -3,6 +3,7 @@ import PermissionsList from './PermissionsList'; ...@@ -3,6 +3,7 @@ import PermissionsList from './PermissionsList';
import { observer } from 'mobx-react'; import { observer } from 'mobx-react';
import UserPicker, { User } from 'app/core/components/Picker/UserPicker'; import UserPicker, { User } from 'app/core/components/Picker/UserPicker';
import TeamPicker, { Team } from 'app/core/components/Picker/TeamPicker'; import TeamPicker, { Team } from 'app/core/components/Picker/TeamPicker';
import { aclTypes } from 'app/stores/PermissionsStore/PermissionsStore';
export interface DashboardAcl { export interface DashboardAcl {
id?: number; id?: number;
...@@ -31,22 +32,6 @@ export interface IProps { ...@@ -31,22 +32,6 @@ export interface IProps {
@observer @observer
class Permissions extends Component<IProps, any> { class Permissions extends Component<IProps, any> {
dashboardId: any;
meta: any;
items: DashboardAcl[];
dummyItems: DashboardAcl[];
permissionOptions = [{ value: 1, text: 'View' }, { value: 2, text: 'Edit' }, { value: 4, text: 'Admin' }];
aclTypes = [
{ value: 'Group', text: 'Team' },
{ value: 'User', text: 'User' },
{ value: 'Viewer', text: 'Everyone With Viewer Role' },
{ value: 'Editor', text: 'Everyone With Editor Role' },
];
newType: string;
canUpdate: boolean;
error: string;
refreshList: any;
constructor(props) { constructor(props) {
super(props); super(props);
const { dashboardId, permissions, isFolder } = this.props; const { dashboardId, permissions, isFolder } = this.props;
...@@ -57,10 +42,6 @@ class Permissions extends Component<IProps, any> { ...@@ -57,10 +42,6 @@ class Permissions extends Component<IProps, any> {
this.userPicked = this.userPicked.bind(this); this.userPicked = this.userPicked.bind(this);
this.teamPicked = this.teamPicked.bind(this); this.teamPicked = this.teamPicked.bind(this);
permissions.load(dashboardId, isFolder); permissions.load(dashboardId, isFolder);
this.state = {
newType: 'Group',
};
} }
permissionChanged(index: number, permission: number, permissionName: string) { permissionChanged(index: number, permission: number, permissionName: string) {
...@@ -79,12 +60,8 @@ class Permissions extends Component<IProps, any> { ...@@ -79,12 +60,8 @@ class Permissions extends Component<IProps, any> {
} }
resetNewType() { resetNewType() {
this.setState(prevState => { const { permissions } = this.props;
return { permissions.resetNewType();
...prevState,
newType: 'Group',
};
});
} }
typeChanged(evt) { typeChanged(evt) {
...@@ -97,12 +74,7 @@ class Permissions extends Component<IProps, any> { ...@@ -97,12 +74,7 @@ class Permissions extends Component<IProps, any> {
return; return;
} }
this.setState(prevState => { permissions.setNewType(value);
return {
...prevState,
newType: value,
};
});
} }
userPicked(user: User) { userPicked(user: User) {
...@@ -116,15 +88,12 @@ class Permissions extends Component<IProps, any> { ...@@ -116,15 +88,12 @@ class Permissions extends Component<IProps, any> {
} }
render() { render() {
console.log('Permissions render');
const { permissions, backendSrv } = this.props; const { permissions, backendSrv } = this.props;
const { newType } = this.state;
return ( return (
<div className="gf-form-group"> <div className="gf-form-group">
<PermissionsList <PermissionsList
permissions={permissions.items} permissions={permissions.items}
permissionsOptions={this.permissionOptions}
removeItem={this.removeItem} removeItem={this.removeItem}
permissionChanged={this.permissionChanged} permissionChanged={this.permissionChanged}
fetching={permissions.fetching} fetching={permissions.fetching}
...@@ -135,8 +104,12 @@ class Permissions extends Component<IProps, any> { ...@@ -135,8 +104,12 @@ class Permissions extends Component<IProps, any> {
<div className="gf-form-inline"> <div className="gf-form-inline">
<div className="gf-form"> <div className="gf-form">
<div className="gf-form-select-wrapper"> <div className="gf-form-select-wrapper">
<select className="gf-form-input gf-size-auto" value={newType} onChange={this.typeChanged}> <select
{this.aclTypes.map((option, idx) => { className="gf-form-input gf-size-auto"
value={permissions.newType}
onChange={this.typeChanged}
>
{aclTypes.map((option, idx) => {
return ( return (
<option key={idx} value={option.value}> <option key={idx} value={option.value}>
{option.text} {option.text}
...@@ -147,13 +120,13 @@ class Permissions extends Component<IProps, any> { ...@@ -147,13 +120,13 @@ class Permissions extends Component<IProps, any> {
</div> </div>
</div> </div>
{newType === 'User' ? ( {permissions.newType === 'User' ? (
<div className="gf-form"> <div className="gf-form">
<UserPicker backendSrv={backendSrv} handlePicked={this.userPicked} /> <UserPicker backendSrv={backendSrv} handlePicked={this.userPicked} />
</div> </div>
) : null} ) : null}
{newType === 'Group' ? ( {permissions.newType === 'Group' ? (
<div className="gf-form"> <div className="gf-form">
<TeamPicker backendSrv={backendSrv} handlePicked={this.teamPicked} /> <TeamPicker backendSrv={backendSrv} handlePicked={this.teamPicked} />
</div> </div>
......
...@@ -4,7 +4,6 @@ import { observer } from 'mobx-react'; ...@@ -4,7 +4,6 @@ import { observer } from 'mobx-react';
export interface IProps { export interface IProps {
permissions: any[]; permissions: any[];
permissionsOptions: any[];
removeItem: any; removeItem: any;
permissionChanged: any; permissionChanged: any;
fetching: boolean; fetching: boolean;
...@@ -13,7 +12,7 @@ export interface IProps { ...@@ -13,7 +12,7 @@ export interface IProps {
@observer @observer
class PermissionsList extends Component<IProps, any> { class PermissionsList extends Component<IProps, any> {
render() { render() {
const { permissions, permissionsOptions, removeItem, permissionChanged, fetching } = this.props; const { permissions, removeItem, permissionChanged, fetching } = this.props;
return ( return (
<table className="filter-table gf-form-group"> <table className="filter-table gf-form-group">
...@@ -24,7 +23,6 @@ class PermissionsList extends Component<IProps, any> { ...@@ -24,7 +23,6 @@ class PermissionsList extends Component<IProps, any> {
key={idx} key={idx}
item={item} item={item}
itemIndex={idx} itemIndex={idx}
permissionsOptions={permissionsOptions}
removeItem={removeItem} removeItem={removeItem}
permissionChanged={permissionChanged} permissionChanged={permissionChanged}
/> />
......
import React from 'react'; import React from 'react';
import { observer } from 'mobx-react'; import { observer } from 'mobx-react';
import { permissionOptions } from 'app/stores/PermissionsStore/PermissionsStore';
const setClassNameHelper = inherited => { const setClassNameHelper = inherited => {
return inherited ? 'gf-form-disabled' : ''; return inherited ? 'gf-form-disabled' : '';
}; };
export default observer(({ item, permissionsOptions, removeItem, permissionChanged, itemIndex }) => { export default observer(({ item, removeItem, permissionChanged, itemIndex }) => {
const handleRemoveItem = evt => { const handleRemoveItem = evt => {
evt.preventDefault(); evt.preventDefault();
removeItem(itemIndex); removeItem(itemIndex);
...@@ -15,7 +16,7 @@ export default observer(({ item, permissionsOptions, removeItem, permissionChang ...@@ -15,7 +16,7 @@ export default observer(({ item, permissionsOptions, removeItem, permissionChang
evt.preventDefault(); evt.preventDefault();
const value = evt.target.value; const value = evt.target.value;
const valueAsInt = parseInt(value, 10); const valueAsInt = parseInt(value, 10);
const newPermission = permissionsOptions.find(opt => opt.value === valueAsInt); const newPermission = permissionOptions.find(opt => opt.value === valueAsInt);
permissionChanged(itemIndex, newPermission.value, newPermission.text); permissionChanged(itemIndex, newPermission.value, newPermission.text);
}; };
...@@ -36,7 +37,7 @@ export default observer(({ item, permissionsOptions, removeItem, permissionChang ...@@ -36,7 +37,7 @@ export default observer(({ item, permissionsOptions, removeItem, permissionChang
onChange={handleChangePermission} onChange={handleChangePermission}
disabled={item.inherited} disabled={item.inherited}
> >
{permissionsOptions.map((option, idx) => { {permissionOptions.map((option, idx) => {
return ( return (
<option key={idx} value={option.value}> <option key={idx} value={option.value}>
{option.text} {option.text}
......
...@@ -3,6 +3,17 @@ import { PermissionsStoreItem } from './PermissionsStoreItem'; ...@@ -3,6 +3,17 @@ import { PermissionsStoreItem } from './PermissionsStoreItem';
const duplicateError = 'This permission exists already.'; const duplicateError = 'This permission exists already.';
export const permissionOptions = [{ value: 1, text: 'View' }, { value: 2, text: 'Edit' }, { value: 4, text: 'Admin' }];
export const aclTypes = [
{ value: 'Group', text: 'Team' },
{ value: 'User', text: 'User' },
{ value: 'Viewer', text: 'Everyone With Viewer Role' },
{ value: 'Editor', text: 'Everyone With Editor Role' },
];
const defaultNewType = aclTypes[0].value;
export const PermissionsStore = types export const PermissionsStore = types
.model('PermissionsStore', { .model('PermissionsStore', {
fetching: types.boolean, fetching: types.boolean,
...@@ -12,6 +23,7 @@ export const PermissionsStore = types ...@@ -12,6 +23,7 @@ export const PermissionsStore = types
items: types.optional(types.array(PermissionsStoreItem), []), items: types.optional(types.array(PermissionsStoreItem), []),
error: types.maybe(types.string), error: types.maybe(types.string),
originalItems: types.optional(types.array(PermissionsStoreItem), []), originalItems: types.optional(types.array(PermissionsStoreItem), []),
newType: types.optional(types.string, defaultNewType),
}) })
.views(self => ({ .views(self => ({
isValid: item => { isValid: item => {
...@@ -58,6 +70,12 @@ export const PermissionsStore = types ...@@ -58,6 +70,12 @@ export const PermissionsStore = types
self.items[idx].updatePermission(permission, permissionName); self.items[idx].updatePermission(permission, permissionName);
self.canUpdate = true; self.canUpdate = true;
}, },
setNewType(newType: string) {
self.newType = newType;
},
resetNewType() {
self.newType = defaultNewType;
},
update: flow(function* update(dashboardId: number) { update: flow(function* update(dashboardId: number) {
self.error = null; self.error = null;
const backendSrv = getEnv(self).backendSrv; const backendSrv = getEnv(self).backendSrv;
......
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