diff --git a/server/index.js b/server/index.js index 50c1d439..50ff90b6 100644 --- a/server/index.js +++ b/server/index.js @@ -70,6 +70,7 @@ app.post('/api/portmodify', function (req, res) { sManager.updateLinkSettings(req.body.user); }) +//Get details of a network connection by name app.post('/api/networkIP', (req, res) => { networkManager.getConnectionDetails(req.body.conName, (err, conDetails) => { res.setHeader('Content-Type', 'application/json'); @@ -79,6 +80,14 @@ app.post('/api/networkIP', (req, res) => { }); }); +//User wants to add/edit/delete network +app.post('/api/networkmodify', (req, res) => { + console.log('Editing network:'); + console.log(req.body.conAction); + console.log(req.body.conName); + console.log(req.body.conSettings); +}); + http.listen(3001, () => console.log('Express server is running on localhost:3001') ); diff --git a/src/networkconfig.js b/src/networkconfig.js index 3d57a6c0..6c2f20ce 100644 --- a/src/networkconfig.js +++ b/src/networkconfig.js @@ -13,10 +13,40 @@ class NetworkConfig extends Component { netConnectionFilteredSelected: null, netConnectionDetails: {}, showIP: false, - wpaTypes: ['none', 'ieee8021x', 'wpa-none', 'wpa-psk', 'sae', 'wpa-eap'], - bandTypes: ['a', 'bg'] - }; + wpaTypes: [{value: 'wpa-none', text: 'wpa-none'}, {value: 'wpa-psk', text: 'wpa-psk'}], + bandTypes: [{value: 'a', text: '5 GHz'}, {value: 'bg', text: '2.4 GHz'}], + availChannels: [], + curSettings: { + ipaddresstype: { + value: '' + }, + ipaddress: { + value: '' + }, + subnet: { + value: '' + }, + wpaType: { + value: '' + }, + password: { + value: '' + }, + ssid: { + value: '' + }, + band: { + value: '' + } } + }; + + //bind button events + this.handleNetworkSubmit = this.handleNetworkSubmit.bind(this); + this.deleteConnection = this.deleteConnection.bind(this); + this.addConnection = this.addConnection.bind(this); + //this.handleIPTypeChange = this.handleIPTypeChange.bind(this); + } componentDidMount() { this.handleStart(); @@ -108,22 +138,98 @@ class NetworkConfig extends Component { }) }).then(response => response.json()) .then(state => this.setState(state)) - .then(state => this.setState({netConnectionFilteredSelected: value})); + .then(state => this.setState({netConnectionFilteredSelected: value})) + .then(state => this.setState({ curSettings: { + ipaddresstype: {value: this.state.netConnectionDetails.DHCP}, + ipaddress: {value: this.state.netConnectionDetails.IP}, + subnet: {value: this.state.netConnectionDetails.subnet}, + wpaType: {value: this.state.netConnectionDetails.wpaType}, + password: {value: this.state.netConnectionDetails.password}, + ssid: {value: this.state.netConnectionDetails.ssid}, + band: {value: this.state.netConnectionDetails.band} + }})); } + }; + + sendNetworkEdit = (conName, action, settings) => { + fetch('/api/networkmodify', { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + conName: conName, + conAction: action, + conSettings: settings + }) + }); }; - handleIPTypeChange = (event) => { - //IP type radio buttons changed - this.setState({netConnectionDetails: {...this.state.netConnectionDetails, DHCP: event.target.value}}); + handleNetworkSubmit = (event) =>{ + //save network button clicked + this.sendNetworkEdit(this.state.netConnectionFilteredSelected.value, "edit", this.state.curSettings); + event.preventDefault(); + }; - //this.setState((state) => { - // console.log(event.target.value); - // return {netConnectionDetails: {...state.netConnectionDetails, DHCP: event.target.value}}; - //}); + deleteConnection = (event) =>{ + //delete network button clicked + if (window.confirm('Confirm you wish to delete this network')) + //if it's a new connection, go back to old connection + if (this.state.netConnectionFilteredSelected.value === "new") { + this.handleConnectionChange(this.state.netConnection[0], {action: "select-option"}); + } + else { + this.sendNetworkEdit(this.state.netConnectionFilteredSelected.value, "delete", null); + } + event.preventDefault(); + }; + addConnection = (event) =>{ + //add new network button clicked + const enteredName = prompt('Please enter new connection name'); + if (enteredName !== '' && enteredName !== null) { + this.setState({netConnectionFilteredSelected: {value: 'new', label: enteredName, type: this.state.netDeviceSelected.type, state: ""}}); + //this.setState(); + } + //this.sendNetworkEdit(this.state.netConnectionFilteredSelected.label, "addnew", this.state.curSettings); + event.preventDefault(); }; + changeHandler = event => { + //form change handler + const name = event.target.name; + const value = event.target.value; + + this.setState({ + curSettings: { + ...this.state.curSettings, + [name]: { + ...this.state.curSettings[name], + value + } + } + }); + } + + resetForm = (event) => { + //if it's a new connection, go back to old connection + if (this.state.netConnectionFilteredSelected.value === "new") { + this.handleConnectionChange(this.state.netConnection[0], {action: "select-option"}); + } + //reset the form + this.setState({ curSettings: { + ipaddresstype: {value: this.state.netConnectionDetails.DHCP}, + ipaddress: {value: this.state.netConnectionDetails.IP}, + subnet: {value: this.state.netConnectionDetails.subnet}, + wpaType: {value: this.state.netConnectionDetails.wpaType}, + password: {value: this.state.netConnectionDetails.password}, + ssid: {value: this.state.netConnectionDetails.ssid}, + band: {value: this.state.netConnectionDetails.band} + }}); + } + render() { return (