Added Properties form

This commit is contained in:
dbroqua 2018-10-02 19:39:45 +02:00
parent a9575b79c1
commit b5303c37d4
4 changed files with 203 additions and 21 deletions

View file

@ -3,6 +3,7 @@ import {
BrowserRouter as Router, Route, BrowserRouter as Router, Route,
Switch, Switch,
} from 'react-router-dom'; } from 'react-router-dom';
import Properties from './Properties';
import Categories from './Categories'; import Categories from './Categories';
import Category from './Category'; import Category from './Category';
import Vegetable from './Vegetable'; import Vegetable from './Vegetable';
@ -21,6 +22,7 @@ class App extends Component {
<Switch> <Switch>
<Route exact path="/" component={requireAuth(Login, false)} /> <Route exact path="/" component={requireAuth(Login, false)} />
<Route exact path="/LogOut" component={Logout} /> <Route exact path="/LogOut" component={Logout} />
<Route exact path="/properties" component={requireAuth(Properties)} />
<Route exact path="/categories" component={requireAuth(Categories)} /> <Route exact path="/categories" component={requireAuth(Categories)} />
<Route exact path="/categories/:categoryId" component={requireAuth(Category)} /> <Route exact path="/categories/:categoryId" component={requireAuth(Category)} />
<Route exact path="/categories/:categoryId/vegetables/:vegetableId" component={requireAuth(Vegetable)} /> <Route exact path="/categories/:categoryId/vegetables/:vegetableId" component={requireAuth(Vegetable)} />

View file

@ -1,5 +1,7 @@
import React from 'react'; import React from 'react';
import { Link } from 'react-router-dom'; import {
Link
} from 'react-router-dom';
import { import {
Collapse, Collapse,
Navbar, Navbar,
@ -9,7 +11,11 @@ import {
NavItem, NavItem,
NavLink, NavLink,
} from 'reactstrap'; } from 'reactstrap';
import { FaSignOutAlt } from 'react-icons/fa'; import {
FaSignOutAlt,
FaLeaf,
FaSlidersH
} from 'react-icons/fa';
export default class Example extends React.Component { export default class Example extends React.Component {
constructor(props) { constructor(props) {
@ -35,11 +41,25 @@ export default class Example extends React.Component {
<NavbarToggler onClick={this.toggle} /> <NavbarToggler onClick={this.toggle} />
<Collapse isOpen={this.state.isOpen} navbar> <Collapse isOpen={this.state.isOpen} navbar>
<Nav className="ml-auto" navbar> <Nav className="ml-auto" navbar>
<NavItem>
<NavLink tag={Link} to="/categories">
<FaLeaf />
{' '}
Catégories
</NavLink>
</NavItem>
<NavItem>
<NavLink tag={Link} to="/properties">
<FaSlidersH />
{' '}
Propriétés
</NavLink>
</NavItem>
<NavItem> <NavItem>
<NavLink tag={Link} to="/LogOut"> <NavLink tag={Link} to="/LogOut">
<FaSignOutAlt /> <FaSignOutAlt />
{' '} {' '}
Déconnexion Déconnexion
</NavLink> </NavLink>
</NavItem> </NavItem>
</Nav> </Nav>

View file

@ -1,6 +1,11 @@
import React from 'react'; import React from 'react';
import { Breadcrumb, BreadcrumbItem } from 'reactstrap'; import {
import { Link } from 'react-router-dom'; Breadcrumb,
BreadcrumbItem
} from 'reactstrap';
import {
Link
} from 'react-router-dom';
import API from './Api'; import API from './Api';
class Navigation extends React.Component { class Navigation extends React.Component {
@ -10,25 +15,33 @@ class Navigation extends React.Component {
const Breadcrumb = []; const Breadcrumb = [];
if (API.defaults.headers.common.Authorization !== undefined) { if (API.defaults.headers.common.Authorization !== undefined) {
Breadcrumb.push({ if (this.props.root && this.props.root === 'Property') {
name: 'Catégories',
path: '/categories',
active: !this.props.categoryId,
});
if (this.props.categoryId) {
Breadcrumb.push({ Breadcrumb.push({
name: this.props.categoryId, name: 'Propriétés',
path: `/categories/${this.props.categoryId}`, path: '/properties',
active: !this.props.vegetableId, active: true,
});
} else {
Breadcrumb.push({
name: 'Catégories',
path: '/categories',
active: !this.props.categoryId,
}); });
if (this.props.vegetableId) { if (this.props.categoryId) {
Breadcrumb.push({ Breadcrumb.push({
name: this.props.vegetableId, name: this.props.categoryId,
path: `/categories/${this.props.categoryId}/vegetal/${this.props.vegetableId}`, path: `/categories/${this.props.categoryId}`,
active: true, active: !this.props.vegetableId,
}); });
if (this.props.vegetableId) {
Breadcrumb.push({
name: this.props.vegetableId,
path: `/categories/${this.props.categoryId}/vegetal/${this.props.vegetableId}`,
active: true,
});
}
} }
} }
} }
@ -42,12 +55,16 @@ class Navigation extends React.Component {
if (props.categoryName) { if (props.categoryName) {
const Breadcrumb = this.state.Breadcrumb; const Breadcrumb = this.state.Breadcrumb;
Breadcrumb[1].name = props.categoryName; Breadcrumb[1].name = props.categoryName;
this.setState({ Breadcrumb }); this.setState({
Breadcrumb
});
} }
if (props.vegetableName) { if (props.vegetableName) {
const Breadcrumb = this.state.Breadcrumb; const Breadcrumb = this.state.Breadcrumb;
Breadcrumb[2].name = props.vegetableName; Breadcrumb[2].name = props.vegetableName;
this.setState({ Breadcrumb }); this.setState({
Breadcrumb
});
} }
} }

143
src/Properties.js Normal file
View file

@ -0,0 +1,143 @@
import React, {
Component
} from 'react';
import {
Container,
Table,
Form,
FormGroup,
Button,
Input,
} from 'reactstrap';
import {
FaTrashAlt,
FaPlus
} from 'react-icons/fa';
import Navigation from './Navigation';
import Header from './Header';
import API from './Api';
class Properties extends Component {
constructor(props) {
super(props);
this.getProperties = this.getProperties.bind(this);
this.addProperty = this.addProperty.bind(this);
this.deleteProperty = this.deleteProperty.bind(this);
this.handleChange = this.handleChange.bind(this);
this.state = {
Properties: [],
name: '',
};
this.getProperties();
}
handleChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value,
});
}
getProperties() {
API.get('properties')
.then((res) => {
if (res.status === 200) {
this.setState({
Properties: res.data.rows
});
} else {
this.setState({
Properties: []
});
}
})
.catch((e) => {
alert('Erreur lors de la récupération des types de propriétés');
});
}
addProperty(event) {
event.preventDefault(); // Let's stop this event.
event.stopPropagation(); // Really this time.
API.post('properties', {
name: this.state.name,
})
.then((res) => {
this.setState(prevState => ({
Properties: [...prevState.Properties, res.data],
name: ''
}));
})
.catch(() => {
alert('Impossile de créer ce type de propriété');
});
}
deleteProperty(property) {
API.delete(`properties/${property.id}`)
.then((res) => {
this.setState({
Properties: this.state.Properties.filter(item => item.id !== property.id),
});
})
.catch((e) => {
alert('Erreur lors de la suppression de ce type de propriété');
});
}
render() {
return (
<div>
<Header />
<Container>
<Navigation root="Property" />
<Table>
<thead>
<tr>
<th>#</th>
<th>Nom</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{this.state.Properties.map((item, key) => (
<tr key={key}>
<th scope="row">{item.id}</th>
<td>{item.name}</td>
<td>
{' '}
<Button color="danger" onClick={() => this.deleteProperty(item)}><FaTrashAlt /></Button>
</td>
</tr>
))}
</tbody>
</Table>
<Form inline onSubmit={this.addProperty}>
<FormGroup className="mb-8 mr-sm-8 mb-sm-0">
<Input
type="text"
name="name"
value={this.state.name}
placeholder="Nouveau type de propriété"
onChange={this.handleChange}
/>
</FormGroup>
<Button color="primary">
<FaPlus />
{' '}
Ajouter
</Button>
</Form>
</Container>
</div>
);
}
}
export default Properties;