Merge branch 'application-mgt-new' into 'application-mgt-new'

Bulk actions UI

See merge request entgra/carbon-device-mgt!299
feature/appm-store/pbac
Dharmakeerthi Lasantha 5 years ago
commit f0d8f4d023

@ -17,7 +17,7 @@
*/ */
import React from "react"; import React from "react";
import {Button, Icon, notification} from "antd"; import {Button, Tooltip, Popconfirm, Divider} from "antd";
class BulkActionBar extends React.Component { class BulkActionBar extends React.Component {
@ -25,58 +25,97 @@ class BulkActionBar extends React.Component {
super(props); super(props);
this.state = { this.state = {
selectedMultiple: false, selectedMultiple: false,
selectedSingle:false selectedSingle: false,
canDelete: true,
} }
} }
//This method is used to trigger delete request on selected devices //This method checks whether NON-REMOVED devices are selected
deleteDevice = () => { onDeleteDeviceCall = () => {
const deviceStatusArray = this.props.selectedRows.map(obj => obj.enrolmentInfo.status); let tempDeleteState;
if(deviceStatusArray.includes("ACTIVE") || deviceStatusArray.includes("INACTIVE")){ for(let i=0; i < this.props.selectedRows.length; i++){
notification["error"]({ if(this.props.selectedRows[i].enrolmentInfo.status != "REMOVED"){
message: "There was a problem", tempDeleteState = false;
duration: 0, break;
description:
"Cannot delete ACTIVE/INACTIVE devices.",
});
}else{
this.props.deleteDevice();
} }
tempDeleteState = true;
} }
this.setState({canDelete:tempDeleteState})
};
componentDidUpdate(prevProps, prevState, snapshot) { onConfirmDelete = () => {
if(prevProps.selectedRows !== this.props.selectedRows){ if (this.state.canDelete) {
if(this.props.selectedRows.length > 1){ this.props.deleteDevice();
this.setState({selectedMultiple:true,selectedSingle:false})
}else if(this.props.selectedRows.length == 1){
this.setState({selectedSingle:true,selectedMultiple:true})
}else{
this.setState({selectedSingle:false,selectedMultiple:false})
}
} }
};
onConfirmDisenroll = () => {
//TODO: Implement disenrollment function
};
onDeviceGroupCall = () => {
this.props.getGroups();
} }
render() { render() {
const isSelected = this.props.selectedRows.length > 0;
const isSelectedSingle = this.props.selectedRows.length == 1;
return ( return (
<div style={{padding:'5px'}}> <div style={{display: isSelected ? "inline" : "none", padding: '11px'}}>
<Tooltip
placement="bottom"
title={"Delete Device"}
autoAdjustOverflow={true}>
<Popconfirm
placement="topLeft"
title={
this.state.canDelete ?
"Are you sure you want to delete?" : "You can only delete disenrolled devices"}
onConfirm={this.onConfirmDelete}
okText="Ok"
cancelText="Cancel">
<Button <Button
type="normal" type="link"
shape="circle"
icon="delete" icon="delete"
size={'default'} size={'default'}
onClick={this.deleteDevice} onClick={this.onDeleteDeviceCall}
style={ disabled={isSelected ? false : true}
{display:this.state.selectedMultiple ? "inline" : "none"} style={{margin: "2px"}}/>
}>Delete </Popconfirm>
</Button> </Tooltip>
<Divider type="vertical"/>
<Tooltip placement="bottom" title={"Disenroll Device"}>
<Popconfirm
placement="topLeft"
title={"Are you sure?"}
onConfirm={this.onConfirmDisenroll}
okText="Ok"
disabled={isSelectedSingle ? false : true}
cancelText="Cancel">
<Button
type="link"
shape="circle"
icon="close"
size={'default'}
style={{display:isSelectedSingle ? "inline" : "none", margin: "2px"}}/>
</Popconfirm>
</Tooltip>
<Divider type="vertical" style={{display:isSelectedSingle ? "inline-block" : "none"}}/>
<Tooltip placement="bottom" title={"Add to group"}>
<Button <Button
type="normal" type="link"
icon="delete" shape="circle"
icon="deployment-unit"
size={'default'} size={'default'}
style={ onClick={this.onDeviceGroupCall}
{display:this.state.selectedSingle ? "inline" : "none"} style={{margin: "2px"}}/>
}>Disenroll
</Button> </Tooltip>
</div> </div>
) )
} }

@ -18,7 +18,7 @@
import React from "react"; import React from "react";
import axios from "axios"; import axios from "axios";
import {Tag, message, notification, Table, Typography, Tooltip, Icon, Divider, Button} from "antd"; import {Tag, message, notification, Table, Typography, Tooltip, Icon, Divider, Button, Modal, Select} from "antd";
import TimeAgo from 'javascript-time-ago' import TimeAgo from 'javascript-time-ago'
// Load locale-specific relative date/time formatting rules. // Load locale-specific relative date/time formatting rules.
@ -112,18 +112,7 @@ const columns = [
return <Tooltip title={new Date(dateOfLastUpdate).toString()}>{timeAgoString}</Tooltip>; return <Tooltip title={new Date(dateOfLastUpdate).toString()}>{timeAgoString}</Tooltip>;
} }
// todo add filtering options // todo add filtering options
}, }
{
title: 'Action',
key: 'action',
render: () => (
<span>
<a><Icon type="edit" /></a>
<Divider type="vertical" />
<a><Text type="danger"><Icon type="delete" /></Text></a>
</span>
),
},
]; ];
const getTimeAgo = (time) => { const getTimeAgo = (time) => {
@ -142,7 +131,9 @@ class DeviceTable extends React.Component {
pagination: {}, pagination: {},
loading: false, loading: false,
selectedRows: [], selectedRows: [],
deviceIds: [] deviceGroups: [],
groupModalVisible: false,
selectedGroupId: []
}; };
} }
@ -151,7 +142,6 @@ class DeviceTable extends React.Component {
this.setState({ this.setState({
selectedRows: selectedRows selectedRows: selectedRows
}); });
this.state.deviceIds = selectedRows.map(obj => obj.deviceIdentifier);
} }
}; };
@ -212,7 +202,7 @@ class DeviceTable extends React.Component {
const config = this.props.context; const config = this.props.context;
this.setState({loading: true}); this.setState({loading: true});
const deviceData = this.state.deviceIds; const deviceData = this.state.selectedRows.map(obj => obj.deviceIdentifier);
//send request to the invoker //send request to the invoker
axios.put( axios.put(
@ -221,15 +211,72 @@ class DeviceTable extends React.Component {
"/admin/devices/permanent-delete", "/admin/devices/permanent-delete",
deviceData, deviceData,
{headers: {'Content-Type': 'application/json'}} {headers: {'Content-Type': 'application/json'}}
).then(res => { ).then(res => {
if (res.status === 200) { if (res.status === 200) {
this.fetch(); this.fetch();
const pagination = {...this.state.pagination}; }
}).catch((error) => {
if (error.hasOwnProperty("response") && error.response.status === 401) {
//todo display a popop with error
message.error('You are not logged in');
window.location.href = window.location.origin + '/entgra/login';
} else {
notification["error"]({
message: "There was a problem",
duration: 0,
description:
"Error occurred while trying to delete devices.",
});
}
this.setState({loading: false});
});
};
addDevicesToGroup = (groupId) => {
const config = this.props.context;
this.setState({loading: true});
let apiUrl;
let deviceData;
if (this.state.selectedRows.length === 1) {
apiUrl = window.location.origin + config.serverConfig.invoker.uri +
config.serverConfig.invoker.deviceMgt +
"/groups/device/assign";
deviceData = {
deviceIdentifier: {
id: this.state.selectedRows[0].deviceIdentifier,
type: this.state.selectedRows[0].type
},
deviceGroupIds: groupId
}
} else if (!groupId[0]){
apiUrl = window.location.origin + config.serverConfig.invoker.uri +
config.serverConfig.invoker.deviceMgt +
"/groups/id/" + groupId + "/devices/add";
deviceData = this.state.selectedRows.map(obj => ({id: obj.deviceIdentifier, type: obj.type}));
} else{
apiUrl = window.location.origin + config.serverConfig.invoker.uri +
config.serverConfig.invoker.deviceMgt +
"/groups/id/" + groupId[0] + "/devices/add";
deviceData = this.state.selectedRows.map(obj => ({id: obj.deviceIdentifier, type: obj.type}));
}
//send request to the invoker
axios.post(
apiUrl,
deviceData,
{headers: {'Content-Type': 'application/json'}}
).then(res => {
if (res.status === 200) {
this.setState({ this.setState({
loading: false, loading: false
data: res.data.data.devices, });
pagination, notification["success"]({
message: "Done",
duration: 4,
description:
"Successfully added to the device group.",
}); });
} }
}).catch((error) => { }).catch((error) => {
@ -242,14 +289,70 @@ class DeviceTable extends React.Component {
message: "There was a problem", message: "There was a problem",
duration: 0, duration: 0,
description: description:
"Error occurred while trying to delete devices.", "Error occurred while adding to the device group.",
}); });
} }
this.setState({loading: false}); this.setState({loading: false});
}); });
};
getGroups = () => {
this.setState({
groupModalVisible: true
});
//send request to the invoker
axios.get(
window.location.origin + config.serverConfig.invoker.uri +
config.serverConfig.invoker.deviceMgt +
"/groups"
).then(res => {
this.setState({deviceGroups: res.data.data.deviceGroups})
}).catch((error) => {
if (error.hasOwnProperty("response") && error.response.status === 401) {
//todo display a popop with error
message.error('You are not logged in');
window.location.href = window.location.origin + '/entgra/login';
} else {
notification["error"]({
message: "There was a problem",
duration: 0,
description:
"Error occurred while retrieving device groups.",
});
} }
this.setState({loading: false});
});
};
handleOk = e => {
if(this.state.selectedGroupId){
this.addDevicesToGroup(this.state.selectedGroupId);
this.setState({
groupModalVisible: false
});
}else{
notification["error"]({
message: "There was a problem",
duration: 0,
description:
"Please select a group.",
});
}
};
handleCancel = e => {
this.setState({
groupModalVisible: false,
});
};
onGroupSelectChange = (value) => {
this.setState({selectedGroupId: value});
};
handleTableChange = (pagination, filters, sorter) => { handleTableChange = (pagination, filters, sorter) => {
const pager = {...this.state.pagination}; const pager = {...this.state.pagination};
pager.current = pagination.current; pager.current = pagination.current;
@ -267,11 +370,22 @@ class DeviceTable extends React.Component {
render() { render() {
const {data, pagination, loading, selectedRows} = this.state; const {data, pagination, loading, selectedRows} = this.state;
const isSelectedSingle = this.state.selectedRows.length == 1;
let item = this.state.deviceGroups.map((data) =>
<Select.Option
value={data.id}
key={data.id}>
{data.name}
</Select.Option>);
return ( return (
<div> <div>
<BulkActionBar <BulkActionBar
deleteDevice={this.deleteDevice} deleteDevice={this.deleteDevice}
getGroups={this.getGroups}
selectedRows={this.state.selectedRows}/> selectedRows={this.state.selectedRows}/>
<div>
<Table <Table
columns={columns} columns={columns}
rowKey={record => (record.deviceIdentifier + record.enrolmentInfo.owner + record.enrolmentInfo.ownership)} rowKey={record => (record.deviceIdentifier + record.enrolmentInfo.owner + record.enrolmentInfo.ownership)}
@ -289,6 +403,26 @@ class DeviceTable extends React.Component {
scroll={{x: 1000}} scroll={{x: 1000}}
/> />
</div> </div>
<Modal
title="Grouping Devices"
width="350px"
visible={this.state.groupModalVisible}
onOk={this.handleOk}
onCancel={this.handleCancel}
>
<Select
mode={isSelectedSingle ? "multiple" : "default"}
showSearch
style={{width: 200}}
placeholder="Select Group"
optionFilterProp="children"
onChange={this.onGroupSelectChange}
>
{item}
</Select>
</Modal>
</div>
); );
} }
} }

Loading…
Cancel
Save