Skip to content

Commit

Permalink
gestita modifica device #11
Browse files Browse the repository at this point in the history
  • Loading branch information
mdslp committed Mar 27, 2020
1 parent c625079 commit 8bdb59d
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 62 deletions.
11 changes: 7 additions & 4 deletions backend/src/routes/private/deviceRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ router.get(
res: express.Response,
next: express.NextFunction
) => {
const tenant_id = req.query.id;
const device_id = req.query.id;
try {
const deviceResponse = await deviceStore.findById(tenant_id);
const deviceResponse = await deviceStore.findById(device_id);
if (deviceResponse && deviceResponse.length == 1) {
const device = deviceResponse[0];
const result = factory.generateSuccessResponse(device, null, "Device found");
Expand Down Expand Up @@ -52,7 +52,7 @@ router.get(

if (devicesRes && devicesRes.length > 0) {
const result = factory.generateSuccessResponse(
{devices: devicesRes, options: searchOptions},
{ devices: devicesRes, options: searchOptions },
null,
'Devices found'
);
Expand Down Expand Up @@ -106,6 +106,9 @@ router.post('/create', async (req, res, next) => {
router.put('/update', async (req, res, next) => {
const device = req.body.params;
try {
console.log("device", device);
console.log("req.body", req.body);

if (!device.id) {
const result = factory.generateErrorResponse(null, null, 'Error');
res.status(HttpStatus.BAD_REQUEST).json(result);
Expand All @@ -114,7 +117,7 @@ router.put('/update', async (req, res, next) => {
let message = '';
if (devices.length == 1) {
const resUpdate = await deviceStore.update(device);
if (resUpdate && resUpdate.length == 1) {
if (resUpdate) {
message = 'Device successfully updated'
const result = factory.generateSuccessResponse(null, null, message);
res.status(HttpStatus.OK).json(result);
Expand Down
22 changes: 19 additions & 3 deletions frontend/src/api/deviceApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DevicesApi extends api {
const url = '/api/private/device/update';
let params = device;
this.getClient(axiosClient => {
axiosClient.put(url, params)
axiosClient.put(url, { params })
.then((data) => {
resolve(data);
}).catch((error) => {
Expand Down Expand Up @@ -57,6 +57,22 @@ class DevicesApi extends api {
});
}

getById(device_id: number) {
return new BBPromise((resolve, reject) => {
const url = '/api/private/device';
let params: any = { id: device_id };

this.getClient(axiosClient => {
axiosClient.get(url, { params })
.then((data) => {
resolve(data);
}).catch((error) => {
resolve(error);
});
}, null, false);
});
}

async delete(tenant_id: number): BBPromise<any> {
return new BBPromise((resolve, reject) => {
const url = '/api/private/device/delete';
Expand All @@ -73,5 +89,5 @@ class DevicesApi extends api {
}
}

const treatmentsApi = new DevicesApi();
export default treatmentsApi;
const devicesApi = new DevicesApi();
export default devicesApi;
2 changes: 1 addition & 1 deletion frontend/src/api/tenantApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TenantsApi extends api {
const url = '/api/private/tenant/update';
let params = tenant;
this.getClient(axiosClient => {
axiosClient.put(url, params)
axiosClient.put(url, { params })
.then((data) => {
resolve(data);
}).catch((error) => {
Expand Down
File renamed without changes.
133 changes: 88 additions & 45 deletions frontend/src/components/forms/deviceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ import _ from 'lodash';
import { IDevice } from '../../../interfaces/device';

interface CompProps {
device?: IDevice;
onBack?: (arg?: any) => any;

}

interface CompState {
dns_name_auto: string;
dns_name_manual: string;
is_gw: boolean;
gw_id: number;
gw_id: number | any;
mac_address: string;
tenant_id: number;
tenant_id: number | any;
}

class DeviceForm extends DNSBaseComponent<CompProps | any, CompState> {
Expand All @@ -32,7 +30,7 @@ class DeviceForm extends DNSBaseComponent<CompProps | any, CompState> {
is_gw: false,
gw_id: null,
mac_address: '',
tenant_id: null,
tenant_id: null
};

constructor(props: any) {
Expand All @@ -43,6 +41,7 @@ class DeviceForm extends DNSBaseComponent<CompProps | any, CompState> {
}

componentDidMount() {
this.init();
}

componentWillReceiveProps(nextProps: any) {
Expand All @@ -62,28 +61,72 @@ class DeviceForm extends DNSBaseComponent<CompProps | any, CompState> {
});
}

async createDevice() {
async init() {
console.log("INITTTTTTTTT", this.props);
if (this.props.match.params.device_id) {
let device: IDevice = { id: this.props.match.params.device_id };
console.log("device", device);

if (this.props.location.state && this.props.location.state.device) {
console.log("device", this.props.location.state);
device = this.props.location.state.device;

} else {
const deviceResponsePromise: any = DeviceApi.getById(this.props.match.params.device_id);
this.registerPromise(deviceResponsePromise);
const deviceResponse = await deviceResponsePromise;
console.log("deviceResponse", deviceResponse);
if (deviceResponse && deviceResponse.data && deviceResponse.data.payload) {
device = deviceResponse.data.payload;
}

}
if (device && device.id) {
this.setState({
dns_name_auto: device.dns_name_auto || '',
dns_name_manual: device.dns_name_manual || '',
is_gw: device.is_gw,
gw_id: device.gw_id,
mac_address: device.mac_address || '',
tenant_id: device.tenant_id
});
}
}
}

async submit() {
try {
if (this.state.mac_address && this.state.dns_name_manual) {
const tenant: IDevice = {
const device: IDevice = {
mac_address: this.state.mac_address,
dns_name_manual: this.state.dns_name_manual
};
const registerPromise = DeviceApi.create(tenant);
let registerPromise: any = null;
if (this.props.match && this.props.match.params && this.props.match.params.device_id) {
device.id = this.props.match.params.device_id;
registerPromise = DeviceApi.update(device);
} else {
registerPromise = DeviceApi.create(device);
}
this.registerPromise(registerPromise);
const responseCreate: any = await registerPromise;

if (responseCreate && responseCreate.status === 200 && responseCreate.data) {
if (responseCreate.data.message === 'Tenant successfully created') {
if (responseCreate.data.message === 'Device successfully created') {
this.props.dispatchNotification('Creation successfully done', 'success', Math.random());
} else if (responseCreate.data.message === 'Tenant already exists') {
} else if (responseCreate.data.message === 'Device already exists') {
this.props.dispatchNotification('Creation successfully done', 'warning', Math.random());
} if (responseCreate.data.message === 'Device successfully updated') {
this.props.dispatchNotification('the change was successful', 'success', Math.random());
} else {
this.props.dispatchNotification(`Error creation.`, 'error', Math.random());
}
}

} else {
this.props.dispatchNotification(`Missing Data`, 'warning', Math.random());
}

} catch (error) {
console.log('error: ', error);
this.props.dispatchNotification('Sign Up Error', 'error', Math.random());
Expand All @@ -93,50 +136,50 @@ class DeviceForm extends DNSBaseComponent<CompProps | any, CompState> {
render() {
return (
<>
<Button floated='right' icon primary size='small' className="customButton"
<Button floated='right' icon primary size='small' className="customButton"
onClick={() => {
history.push(`/home`);
}}>
<Icon name='arrow left' />
</Button>
<Segment raised>
<Segment raised>
Creazione Device
<Form size="small">
<Grid textAlign="center" className='loginForm'>
<Grid.Row width={12}>
<Input
placeholder="edge interface name"
name="edge_interface_name"
type="text"
value={this.state.edge_interface_name}
onChange={(event: any) => {
this.handleChange(event);
}}
/>
</Grid.Row>
<Grid.Row width={12}>
<Input
placeholder="description"
name="description"
type="text"
value={this.state.description}
onChange={(event: any) => {
this.handleChange(event);
<Grid textAlign="center" className='loginForm'>
<Grid.Row width={12}>
<Input
placeholder="edge mac_address name"
name="mac_address"
type="text"
value={this.state.mac_address}
onChange={(event: any) => {
this.handleChange(event);
}}
/>
</Grid.Row>
<Grid.Row width={12}>
<Input
placeholder="dns_name_manual"
name="dns_name_manual"
type="text"
value={this.state.dns_name_manual}
onChange={(event: any) => {
this.handleChange(event);
}}
/>
</Grid.Row>

<Button
type='submit'
className="buttonLoginForm"
onClick={(event: any) => {
this.submit();
}}
/>
</Grid.Row>

<Button
type='submit'
className="buttonLoginForm"
onClick={(event: any) => {
this.createDevice();
}}
>Crea
>Crea
</Button>
</Grid>
</Form>
</Segment>
</Grid>
</Form>
</Segment>
</>
);
}
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/components/pages/homePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import DNSBaseComponent from '../dnsBaseComponent';
import { history } from '../../main';
import * as NotificationActions from '../../actions/notificationActions';
import authutils from '../../utils/authutils';
import DNSForm from "../forms/DNSForm";
import DeviceForm from "../forms/deviceForm";
import CreateUserForm from "../forms/createUserForm";
import TenantTable from "../tables/tenantTable";

Expand Down Expand Up @@ -36,8 +36,6 @@ class HomePage extends DNSBaseComponent<any, CompState> {

render() {
return (
// <DNSForm></DNSForm>
// <CreateUserForm></CreateUserForm>
<TenantTable></TenantTable>
);
}
Expand Down
27 changes: 24 additions & 3 deletions frontend/src/components/tables/deviceTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class TenantTable extends DNSBaseComponent<CompProps | any, CompState> {
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) {
if (devicesResponse && devicesResponse.data && devicesResponse.data.payload.devices && devicesResponse.data.payload.options) {
_.defer(() => {
this.setState({
devices: devicesResponse.data.payload.devices,
Expand All @@ -145,7 +145,7 @@ class TenantTable extends DNSBaseComponent<CompProps | any, CompState> {
});
}

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

return _.map(this.state.devices, (device: IDevice, idx) => {
Expand All @@ -160,6 +160,25 @@ class TenantTable extends DNSBaseComponent<CompProps | any, CompState> {
className="truncate cellTable"
>{device.dns_name_manual}
</Table.Cell>
<Table.Cell
className="truncate cellTable"
>


<Icon
className='customIcon'
name='tag'
color='blue'
onClick={() => {
history.push({
pathname: `/device/edit/${device.id}`,
state: { device }
})

}}
></Icon>

</Table.Cell>
</Table.Row>
)
})
Expand Down Expand Up @@ -220,14 +239,16 @@ class TenantTable extends DNSBaseComponent<CompProps | any, CompState> {
<Table.Row>
<Table.HeaderCell>Mac Address</Table.HeaderCell>
<Table.HeaderCell>DNS Name Manual</Table.HeaderCell>
<Table.HeaderCell>Actions</Table.HeaderCell>

</Table.Row>
</Table.Header>
<Table.Body>
{this.renderTenants()}
</Table.Body>
<Table.Footer fullWidth>
<Table.Row>
<Table.HeaderCell colSpan='1'>
<Table.HeaderCell colSpan='12'>
<Pagination
activePage={this.state.paginationOpts.activePage}
boundaryRange={1}
Expand Down
Loading

0 comments on commit 8bdb59d

Please sign in to comment.