Skip to content

Commit

Permalink
FE: aggiunta tabella devices #11
Browse files Browse the repository at this point in the history
  • Loading branch information
mdslp committed Mar 26, 2020
1 parent c4e6187 commit c25d221
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface IDevice {
is_gw?: boolean;
gw_id?: number;
mac_address?: string;
tenant_id: number;
tenant_id?: number;
updated_at?: string;
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/interfaces/device.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export interface IDevice {
is_gw?: boolean;
gw_id?: number;
mac_address?: string;
tenant_id: number;
tenant_id?: number;
}
59 changes: 59 additions & 0 deletions frontend/src/components/pages/devicePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { Component } from "react";
import { connect } from "react-redux";
import { Container, Button, Card, Image, Segment } from "semantic-ui-react";
import DNSBaseComponent from '../dnsBaseComponent';
import { history } from '../../main';
import * as NotificationActions from '../../actions/notificationActions';
import authutils from '../../utils/authutils';
import DeviceTable from "../tables/deviceTable";

interface CompState {
}

class DevicePage extends DNSBaseComponent<any, CompState> {
componentWillMount() {
const token = authutils.getToken();
if (!token || authutils.isTokenExpired()) {
history.push('/');
}
}

componentDidMount() {
}

componentWillUnmount() {
this.cancelPromises();
}

componentWillReceiveProps() {
const token = authutils.getToken();
if (!token || authutils.isTokenExpired()) {
history.push('/');
}
}

render() {
return (
<DeviceTable></DeviceTable>
);
}
}

const mapStateToProps = (state: any) => {
return {

};
};

const mapDispatchToProps = (dispatch: any) => {
return {
dispatchNotification: (body: string, status: string, id: string) => {
dispatch(NotificationActions.globalStatusChanged({ body, status, id }));
},
};
};

export default connect(
mapStateToProps,
mapDispatchToProps
)(DevicePage);
282 changes: 282 additions & 0 deletions frontend/src/components/tables/deviceTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
import * as React from 'react';
import { connect } from 'react-redux';
import * as Constants from '../../constants';
import DNSBaseComponent from '../dnsBaseComponent';
import DeviceApi from '../../api/deviceApi'
import { history } from '../../main';
import _ from 'lodash';
// import './../../../public/css/device.css';
import { Table, Button, Icon, Container, Pagination, Popup, Input, Loader, Label, Grid } from 'semantic-ui-react';

import TenantForm from '../forms/tenantForm';
import { IDevice } from '../../../interfaces/device';
import { IPaginationOpts } from '../../../interfaces/rest';
import * as NotificationActions from '../../actions/notificationActions';

interface CompProps {
}

interface CompState {
addDevice: boolean;
loading: boolean;
message: string;
paginationOpts: IPaginationOpts;
search: string;
selectedDevice: IDevice;
devices: IDevice[];
updateDevice: boolean;
}

class TenantTable extends DNSBaseComponent<CompProps | any, CompState> {
state = {
addDevice: false,
loading: false,
message: '',
paginationOpts: {
activePage: 1,
totalPages: 1,
itemsPerPage: Constants.pagination.itemsPerPage
},
search: '',
selectedDevice: {},
devices: [],
updateDevice: false,
};

constructor(props: any) {
super(props);
}

componentWillMount() {
}
componentDidMount() {
this.init();
}

componentWillReceiveProps(nextProps: any) {

}

componentWillUnmount() {
this.cancelPromises();
}

init() {
this.getDevices();
}

back() {
this.getDevices();
this.setState({ updateDevice: false, selectedDevice: {}, addDevice: false });
}

handleChange(event: any) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}

handlePaginationChange(e: any, { activePage }) {
let paginationOpts = this.state.paginationOpts;
paginationOpts.activePage = activePage;
this.setState({ paginationOpts });
_.defer(() => {
this.getDevices();
});
}

async handleRemoveTenant(device: IDevice) {
this.setState({ loading: true });
_.defer(async () => {
const removeTenantPromise = DeviceApi.delete(device.id);
this.registerPromise(removeTenantPromise);
const tenantResponse: any = await removeTenantPromise;
if (tenantResponse && tenantResponse.data && tenantResponse.data.status == "success") {
this.getDevices();
} else {
_.defer(() => {
this.setState({
message: "Error...."
});
});
}
});
}

async getDevices(search: boolean = false) {
this.setState({ loading: true });
_.defer(async () => {
const searchValue = search ? this.state.search : undefined;
const options = search ? {
activePage: 1,
totalPages: 1,
itemsPerPage: Constants.pagination.itemsPerPage
} : this.state.paginationOpts;
const getDevicesPromise = DeviceApi.getAll(options, searchValue);
this.registerPromise(getDevicesPromise);
const devicesResponse: any = await getDevicesPromise;
const newSearch = search ? this.state.search : "";
if (devicesResponse && devicesResponse.data && devicesResponse.data.payload.devices && devicesResponse.data.payload.options) {
_.defer(() => {
this.setState({
devices: devicesResponse.data.payload.devices,
paginationOpts: devicesResponse.data.payload.options,
search: newSearch,
loading: false
});
});
} else {
_.defer(() => {
this.setState({
devices: [],
paginationOpts: {
activePage: 1,
totalPages: 1,
itemsPerPage: Constants.pagination.itemsPerPage
},
search: newSearch,
loading: false
});
});
}
});
}

renderTenants() {
console.log("this.state.devices", this.state.devices);

return _.map(this.state.devices, (device: IDevice, idx) => {
console.log("device", device);
return (
<Table.Row key={idx} >
<Table.Cell
className="truncate cellTable"
>{device.mac_address}
</Table.Cell>
<Table.Cell
className="truncate cellTable"
>{device.dns_name_manual}
</Table.Cell>
</Table.Row>
)
})
}

onKeyPress = (e: any) => {
if (e.key === 'Enter') {
this.getDevices(true);
}
}

render() {
return (
<>
{/* {this.state.message && this.state.message != '' ?
<Label>{this.state.message}</Label>
:
null
} */}

{this.state.loading ? (
<Loader
inline="centered"
active={this.state.loading}
disabled={!this.state.loading}
>
Loading
</Loader>
) :
<>

{this.state.addDevice || this.state.updateDevice ?
null
:
<>
<Input
name="search"
icon="search"
iconPosition="left"
placeholder="Search"
value={this.state.search}
className="searchInput"
action={
<Button icon basic onClick={() => {
this.getDevices();
}}>
<Icon name='cancel' color='blue' />
</Button>
}
onChange={(event: any) => {
this.handleChange(event);
}}
onKeyPress={this.onKeyPress}
/>
<h2>Devices</h2>
<Table compact>
<Table.Header>
<Table.Row>
<Table.HeaderCell>Mac Address</Table.HeaderCell>
<Table.HeaderCell>DNS Name Manual</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
{this.renderTenants()}
</Table.Body>
<Table.Footer fullWidth>
<Table.Row>
<Table.HeaderCell colSpan='1'>
<Pagination
activePage={this.state.paginationOpts.activePage}
boundaryRange={1}
size='mini'
onPageChange={(event: any, { activePage }) => {
this.handlePaginationChange(event, { activePage });
}}
totalPages={this.state.paginationOpts.totalPages}
/>
</Table.HeaderCell>

<Table.HeaderCell colSpan='1' className="buttonCell">
<Icon
className='addIcon'
name='plus'
circular
color='blue'
floated='rigth'
onClick={() => {
history.push(`/device/new`)

}}></Icon>
</Table.HeaderCell>
</Table.Row>
</Table.Footer>
</Table>
</>
}
</>
}
</>
);
}
}

const mapStateToProps = (state: any) => {
return {
};
};

const mapDispatchToProps = (dispatch: any) => {
return {
dispatchNotification: (body: string, status: string, id: string) => {
dispatch(NotificationActions.globalStatusChanged({ body, status, id }));
},
};
};

export default connect(
mapStateToProps,
mapDispatchToProps
)(TenantTable);
7 changes: 7 additions & 0 deletions frontend/src/components/toolbars/topBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class TopBar extends Component<any, CompState> {
this.onClick('home');
}}
/>
<Menu.Item
name='device'
active={activeItem === 'devices'}
onClick={() => {
this.onClick('devices');
}}
/>
<Menu.Menu position='right'>
<Input className='loggedUserInput'>{email}</Input>
<Button
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash';

import LoginPage from './components/pages/loginPage';
import DNSForm from './components/forms/DNSForm';
import DevicePage from './components/pages/devicePage';
import HomePage from './components/pages/homePage';
import TenantForm from './components/forms/tenantForm';

Expand Down Expand Up @@ -33,6 +34,15 @@ export const appRoutes: ApplicationRouteGroup[] = [
menuLabel: 'Login',
showTopbar: false
},
{
path: '/devices',
name: 'device',
restricted: false,
exact: true,
component: DevicePage,
menuLabel: 'device',
showTopbar: true
},
{
path: '/home',
name: 'home',
Expand Down

0 comments on commit c25d221

Please sign in to comment.