import React, { Component } from 'react';
import moment from 'moment';
import { seafileAPI } from '../../utils/seafile-api';
import { gettext, loginUrl } from '../../utils/constants';
import toaster from '../../components/toast';
import EmptyTip from '../../components/empty-tip';
class Content extends Component {
render() {
const {loading, errorMsg, items} = this.props.data;
if (loading) {
return ;
} else if (errorMsg) {
return
{errorMsg}
;
} else {
const emptyTip = (
{gettext("You do not have connected devices")}
{gettext("Your clients (Desktop/Android/iOS) will be listed here.")}
);
const desktopThead = (
{gettext('Platform')} |
{gettext('Device Name')} |
{gettext('IP')} |
{gettext('Last Access')} |
|
);
const mobileThead = (
{gettext('Platform')} |
{gettext('Device Name')} |
|
);
return items.length ? (
{window.innerWidth >= 768 ? desktopThead : mobileThead}
): emptyTip;
}
}
}
class TableBody extends Component {
constructor(props) {
super(props);
this.state = {
items: this.props.items
};
}
render() {
let listLinkedDevices = this.state.items.map(function(item, index) {
return ;
}, this);
return (
{listLinkedDevices}
);
}
}
class Item extends Component {
constructor(props) {
super(props);
this.state = {
showOpIcon: false,
unlinked: false
};
this.handleMouseOver = this.handleMouseOver.bind(this);
this.handleMouseOut = this.handleMouseOut.bind(this);
this.handleClick = this.handleClick.bind(this);
}
handleMouseOver() {
this.setState({
showOpIcon: true
});
}
handleMouseOut() {
this.setState({
showOpIcon: false
});
}
handleClick(e) {
e.preventDefault();
const data = this.props.data;
seafileAPI.unlinkDevice(data.platform, data.device_id).then((res) => {
this.setState({
unlinked: true
});
let msg_s = gettext('Successfully unlink %(name)s.');
msg_s = msg_s.replace('%(name)s', data.device_name);
toaster.success(msg_s);
}).catch((error) => {
let message = gettext('Failed to unlink %(name)s');
message = message.replace('%(name)s', data.device_name);
toaster.danger(message);
});
}
render() {
if (this.state.unlinked) {
return null;
}
const data = this.props.data;
let opClasses = 'sf2-icon-delete unlink-device action-icon';
opClasses += this.state.showOpIcon ? '' : ' invisible';
const desktopItem = (
{data.platform} |
{data.device_name} |
{data.last_login_ip} |
{moment(data.last_accessed).fromNow()} |
|
);
const mobileItem = (
{data.platform} |
{data.device_name} |
|
);
if (window.innerWidth >= 768) {
return desktopItem;
} else {
return mobileItem;
}
}
}
class LinkedDevices extends Component {
constructor(props) {
super(props);
this.state = {
loading: true,
errorMsg: '',
items: []
};
}
componentDidMount() {
seafileAPI.listLinkedDevices().then((res) => {
//res: {data: Array(2), status: 200, statusText: "OK", headers: {…}, config: {…}, …}
this.setState({
loading: false,
items: res.data
});
}).catch((error) => {
if (error.response) {
if (error.response.status == 403) {
this.setState({
loading: false,
errorMsg: gettext('Permission denied')
});
location.href = `${loginUrl}?next=${encodeURIComponent(location.href)}`;
} else {
this.setState({
loading: false,
errorMsg: gettext('Error')
});
}
} else {
this.setState({
loading: false,
errorMsg: gettext('Please check the network.')
});
}
});
}
render() {
return (
{gettext('Linked Devices')}
);
}
}
export default LinkedDevices;