Skip to content

Commit

Permalink
deviceForm #11
Browse files Browse the repository at this point in the history
  • Loading branch information
mdslp committed Mar 26, 2020
1 parent c25d221 commit 312da65
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 51 deletions.
59 changes: 8 additions & 51 deletions frontend/src/components/forms/DNSForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,19 @@ import { history } from '../../main';
import _ from 'lodash';
import { Link } from 'react-router-dom';
import DNSApi from './../../api/dnsApi';

// interface CompProps {
// }

// interface CompState {
// confirm_password?: string;
// email?: string;
// name?: string;
// password?: string;
// isLogin?: boolean;
// }


// export default class DNSForm extends DNSBaseComponent<CompProps | any, null> {
// state = {
// };
import DeviceApi from './../../api/deviceApi';

const DNSForm = () => {
const [mac_address, setMacAddress] = useState('test');
const [name, setName] = useState('name');
// const [form, setState] = useState({
// ip: '',
// mac_address: ''
// });


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

// componentWillMount() {
// }

// componentDidMount() {
// }

// componentWillReceiveProps(nextProps: any) {

// }

// componentWillUnmount() {
// this.cancelPromises();
// }

// render() {
const [mac_address, setMacAddress] = useState('');
const [dns_name_manual, setDnsNameManual] = useState('');


async function submit() {
try {
const responseInsertion = await DNSApi.addElement({
const responseInsertion = await DeviceApi.create({
mac_address,
name
dns_name_manual
});
console.log("CREATED???", responseInsertion);
} catch (error) {
console.log("ERROR", error);
}
Expand All @@ -68,9 +28,6 @@ const DNSForm = () => {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
console.log("name", name);
console.log("value", value);
// setState({ ...form });
eval(`set${name}("${value}")`);
}

Expand All @@ -93,8 +50,8 @@ const DNSForm = () => {
</Grid.Row>
<Grid.Row width={16}>
<Input
placeholder="name"
name="Name"
placeholder="dns_name_manual"
name="DnsNameManual"
type="text"
value={name}
onChange={(event: any) => {
Expand Down
166 changes: 166 additions & 0 deletions frontend/src/components/forms/deviceForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import * as React from 'react';
import { connect } from 'react-redux';
import authutils from '../../utils/authutils';
import * as NotificationActions from '../../actions/notificationActions';
import * as UserActions from '../../actions/userActions';
import DeviceApi from './../../api/deviceApi';
import DNSBaseComponent from '../dnsBaseComponent';
import { Grid, Input, Button, Container, Segment, Card, Image, Form, Icon } from 'semantic-ui-react';
import { history } from '../../main';
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;
mac_address: string;
tenant_id: number;
}

class DeviceForm extends DNSBaseComponent<CompProps | any, CompState> {
state = {
dns_name_auto: '',
dns_name_manual: '',
is_gw: false,
gw_id: null,
mac_address: '',
tenant_id: null,
};

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

componentWillMount() {
}

componentDidMount() {
}

componentWillReceiveProps(nextProps: any) {

}

componentWillUnmount() {
this.cancelPromises();
}

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

async createDevice() {
try {
if (this.state.mac_address && this.state.dns_name_manual) {
const tenant: IDevice = {
description: this.state.description,
edge_interface_name: this.state.edge_interface_name
};
const registerPromise = TenantApi.create(tenant);
this.registerPromise(registerPromise);
const responseCreate: any = await registerPromise;
if (responseCreate && responseCreate.status === 200 && responseCreate.data) {
if (responseCreate.data.message === 'Tenant successfully created') {
this.props.dispatchNotification('Creation successfully done', 'success', Math.random());
} else if (responseCreate.data.message === 'Tenant already exists') {
this.props.dispatchNotification('Creation successfully done', 'warning', 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());
}
}

render() {
return (
<>
<Button floated='right' icon primary size='small' className="customButton"
onClick={() => {
history.push(`/home`);
}}>
<Icon name='arrow left' />
</Button>
<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.Row>

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

const mapStateToProps = (state: any) => {
return {
userdata: state.userReducer.userdata,
status: state.userReducer.status
};
};

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

export default connect(
mapStateToProps,
mapDispatchToProps
)(DeviceForm);

0 comments on commit 312da65

Please sign in to comment.