forked from cefjoeii/mern-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the add to database form connect
- Loading branch information
Showing
7 changed files
with
211 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import React, { Component } from 'react'; | ||
import { Button, Form, Select } from 'semantic-ui-react'; | ||
import axios from 'axios'; | ||
|
||
const genderOptions = [ | ||
{ key: 'm', text: 'Male', value: 'male' }, | ||
{ key: 'f', text: 'Female', value: 'female' }, | ||
] | ||
|
||
class FormAdd extends Component { | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
name: '', | ||
email: '', | ||
age: '', | ||
gender: '' | ||
} | ||
|
||
this.handleInputChange = this.handleInputChange.bind(this); | ||
this.handleSelectChange = this.handleSelectChange.bind(this); | ||
this.handleSubmit = this.handleSubmit.bind(this); | ||
} | ||
|
||
handleInputChange(e) { | ||
const target = e.target; | ||
const value = target.type === 'checkbox' ? target.checked : target.value; | ||
const name = target.name; | ||
|
||
this.setState({ [name]: value }); | ||
} | ||
|
||
handleSelectChange(e, data) { | ||
this.setState({ gender: data.value }); | ||
} | ||
|
||
handleSubmit(e) { | ||
e.preventDefault(); | ||
|
||
const newUser = { | ||
name: this.state.name, | ||
email: this.state.email, | ||
age: parseInt(this.state.age, 10), | ||
gender: this.state.gender | ||
} | ||
|
||
axios({ | ||
method: 'post', | ||
responseType: 'json', | ||
url: `${this.props.server}/api/users/`, | ||
data: newUser | ||
}) | ||
.then((res) => { | ||
alert('Successfully added!'); | ||
}) | ||
.catch((err) => { | ||
alert(err.response.data.msg); | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<Form warning onSubmit={this.handleSubmit}> | ||
<Form.Input | ||
label='Name' | ||
type='text' | ||
placeholder='Elon Musk' | ||
name='name' | ||
value={this.state.name} | ||
onChange={this.handleInputChange} | ||
/> | ||
<Form.Input | ||
label='Email' | ||
type='email' | ||
placeholder='[email protected]' | ||
name='email' | ||
value={this.state.email} | ||
onChange={this.handleInputChange} | ||
/> | ||
<Form.Group widths='equal'> | ||
<Form.Input | ||
label='Age' | ||
type='number' | ||
placeholder='18' | ||
min={0} | ||
max={120} | ||
name='age' | ||
value={this.state.age} | ||
onChange={this.handleInputChange} | ||
/> | ||
<Form.Field | ||
control={Select} | ||
label='Gender' | ||
options={genderOptions} | ||
placeholder='Gender' | ||
value={this.state.gender} | ||
onChange={this.handleSelectChange} | ||
/> | ||
</Form.Group> | ||
<Button primary floated='right'>Add</Button> | ||
<br /><br /> | ||
</Form> | ||
); | ||
} | ||
} | ||
|
||
export default FormAdd; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, { Component } from 'react'; | ||
import { Button, Modal } from 'semantic-ui-react'; | ||
|
||
import FormAdd from '../FormAdd/FormAdd'; | ||
|
||
class ModalFormAdd extends Component { | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
open: false | ||
}; | ||
} | ||
|
||
show = (size) => () => this.setState({ size, open: true }); | ||
close = () => this.setState({ open: false }); | ||
|
||
render() { | ||
const { open, size } = this.state; | ||
|
||
return ( | ||
<div> | ||
<Button onClick={this.show('tiny')} primary>Add New</Button> | ||
|
||
<Modal size={size} open={open} onClose={this.close}> | ||
<Modal.Header> | ||
Add User | ||
</Modal.Header> | ||
<Modal.Content> | ||
<FormAdd server={this.props.server} /> | ||
</Modal.Content> | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default ModalFormAdd; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import { Button, Table } from 'semantic-ui-react'; | ||
|
||
const ButtonAction = () => ( | ||
<div> | ||
<Button secondary>Edit</Button> | ||
<Button>Delete</Button> | ||
</div> | ||
) | ||
|
||
const TableData = () => { | ||
return ( | ||
<Table singleLine> | ||
<Table.Header> | ||
<Table.Row> | ||
<Table.HeaderCell>Name</Table.HeaderCell> | ||
<Table.HeaderCell>Email</Table.HeaderCell> | ||
<Table.HeaderCell>Age</Table.HeaderCell> | ||
<Table.HeaderCell>Gender</Table.HeaderCell> | ||
<Table.HeaderCell>Actions</Table.HeaderCell> | ||
</Table.Row> | ||
</Table.Header> | ||
|
||
<Table.Body> | ||
<Table.Row> | ||
<Table.Cell>John Lilki</Table.Cell> | ||
<Table.Cell>[email protected]</Table.Cell> | ||
<Table.Cell>21</Table.Cell> | ||
<Table.Cell>M</Table.Cell> | ||
<Table.Cell><ButtonAction /></Table.Cell> | ||
</Table.Row> | ||
<Table.Row> | ||
<Table.Cell>Jamie Harington</Table.Cell> | ||
<Table.Cell>[email protected]</Table.Cell> | ||
<Table.Cell>25</Table.Cell> | ||
<Table.Cell>F</Table.Cell> | ||
<Table.Cell><ButtonAction /></Table.Cell> | ||
</Table.Row> | ||
<Table.Row> | ||
<Table.Cell>Jill Lewis</Table.Cell> | ||
<Table.Cell>[email protected]</Table.Cell> | ||
<Table.Cell>12</Table.Cell> | ||
<Table.Cell>F</Table.Cell> | ||
<Table.Cell><ButtonAction /></Table.Cell> | ||
</Table.Row> | ||
</Table.Body> | ||
</Table> | ||
); | ||
} | ||
|
||
export default TableData; |