+
diff --git a/src/Category.js b/src/Category.js
index 9a8c650..f7ee926 100644
--- a/src/Category.js
+++ b/src/Category.js
@@ -1,4 +1,6 @@
-import React, { Component } from 'react';
+import React, {
+ Component
+} from 'react';
import {
Container,
Table,
@@ -7,12 +9,24 @@ import {
Button,
Input,
} from 'reactstrap';
-import { FaTrashAlt, FaPlus, FaEdit } from 'react-icons/fa';
-import { Link } from 'react-router-dom';
+import {
+ FaTrashAlt,
+ FaPlus,
+ FaEdit
+} from 'react-icons/fa';
+import {
+ Link
+} from 'react-router-dom';
+import {
+ NotificationContainer,
+ NotificationManager
+} from 'react-notifications';
import Navigation from './Navigation';
import Header from './Header';
import API from './Api';
+import 'react-notifications/lib/notifications.css';
+
class Category extends Component {
constructor(props) {
super(props);
@@ -48,13 +62,15 @@ class Category extends Component {
event.preventDefault(); // Let's stop this event.
event.stopPropagation(); // Really this time.
API.patch(`types/${this.state.Category.id}`, {
- name: this.state.categoryName,
- })
+ name: this.state.categoryName,
+ })
.then((res) => {
- this.setState({ Category: res.data });
+ this.setState({
+ Category: res.data
+ });
})
.catch(() => {
- alert('Impossile de mettre à jour cette catégorie');
+ NotificationManager.error('Impossile de mettre à jour cette catégorie');
});
}
@@ -66,13 +82,18 @@ class Category extends Component {
res.data.Vegetables = [];
}
- this.setState({ Category: res.data, categoryName: res.data.name });
+ this.setState({
+ Category: res.data,
+ categoryName: res.data.name
+ });
} else {
- this.setState({ Category: {} });
+ this.setState({
+ Category: {}
+ });
}
})
.catch((e) => {
- alert('Erreur lors de la récupération des catégories');
+ NotificationManager.error('Erreur lors de la récupération des catégories');
});
}
@@ -80,19 +101,21 @@ class Category extends Component {
event.preventDefault(); // Let's stop this event.
event.stopPropagation(); // Really this time.
API.post(`types/${this.state.Category.id}/vegetables/`, {
- name: this.state.name,
- lat: 0,
- lng: 0,
- description: '',
- })
+ name: this.state.name,
+ lat: 0,
+ lng: 0,
+ description: '',
+ })
.then((res) => {
const Category = this.state.Category;
Category.Vegetables.push(res.data);
- this.setState({ Category });
+ this.setState({
+ Category
+ });
})
.catch(() => {
- alert('Impossile de créer cette catégorie');
+ NotificationManager.error('Impossile de créer cette catégorie');
});
}
@@ -103,16 +126,19 @@ class Category extends Component {
Category.Vegetables.splice(key, 1);
- this.setState({ Category });
+ this.setState({
+ Category
+ });
})
.catch((e) => {
- alert('Erreur lors de la suppression de ce végétal');
+ NotificationManager.error('Erreur lors de la suppression de ce végétal');
});
}
render() {
return (
+
diff --git a/src/Properties.js b/src/Properties.js
index 5f48552..16d828d 100644
--- a/src/Properties.js
+++ b/src/Properties.js
@@ -13,10 +13,16 @@ import {
FaTrashAlt,
FaPlus
} from 'react-icons/fa';
+import {
+ NotificationContainer,
+ NotificationManager
+} from 'react-notifications';
import Navigation from './Navigation';
import Header from './Header';
import API from './Api';
+import 'react-notifications/lib/notifications.css';
+
class Properties extends Component {
constructor(props) {
super(props);
@@ -58,7 +64,7 @@ class Properties extends Component {
}
})
.catch((e) => {
- alert('Erreur lors de la récupération des types de propriétés');
+ NotificationManager.error('Erreur lors de la récupération des types de propriétés');
});
}
@@ -75,7 +81,7 @@ class Properties extends Component {
}));
})
.catch(() => {
- alert('Impossile de créer ce type de propriété');
+ NotificationManager.error('Impossile de créer ce type de propriété');
});
}
@@ -87,13 +93,14 @@ class Properties extends Component {
});
})
.catch((e) => {
- alert('Erreur lors de la suppression de ce type de propriété');
+ NotificationManager.error('Erreur lors de la suppression de ce type de propriété');
});
}
render() {
return (
+
diff --git a/src/Vegetable.js b/src/Vegetable.js
index d216ebc..b75f9d3 100644
--- a/src/Vegetable.js
+++ b/src/Vegetable.js
@@ -15,6 +15,10 @@ import classnames from 'classnames';
import {
FaEdit,
} from 'react-icons/fa';
+import {
+ NotificationContainer,
+ NotificationManager
+} from 'react-notifications';
import Navigation from './Navigation';
import Header from './Header';
import API from './Api';
@@ -23,6 +27,7 @@ import VegetableCarousel from './Vegetable/Carousel'
import VegetableMap from './Vegetable/Map'
import './Vegetable.css';
+import 'react-notifications/lib/notifications.css';
class Vegetable extends Component {
constructor(props) {
@@ -100,27 +105,66 @@ class Vegetable extends Component {
}
updateVegetable(event) {
+ NotificationManager.info('Sauvegarde en cours');
event.preventDefault(); // Let's stop this event.
event.stopPropagation(); // Really this time.
+ const requests = 1 + this.state.Vegetable.Properties.length;
+ let requestsDone = 0;
+ let isDone = () => {
+ if (requests === requestsDone) {
+ NotificationManager.success('Sauvegarde terminée');
+ this.getVegetable(this.props.match.params.categoryId, this.props.match.params.vegetableId);
+ }
+ }
+
+ // Patch Vegetable
const fd = new FormData();
if (this.state.imagePreviewUrl) {
fd.append('mainPicture', this.state.Vegetable.mainPictureImported);
}
Object.keys(this.state.Vegetable).map((objectKey) => {
- if (objectKey !== 'mainPicture' && objectKey !== 'mainPictureImported') {
+ // if (objectKey === 'Properties') {
+ // fd.append(objectKey, JSON.stringify(this.state.Vegetable[objectKey]));
+ // } else
+ if (objectKey !== 'mainPicture' && objectKey !== 'mainPictureImported' && objectKey !== 'Properties') {
fd.append(objectKey, this.state.Vegetable[objectKey]);
}
return true;
});
API.patch(`types/${this.state.Vegetable.Type.id}/vegetables/${this.state.Vegetable.id}`, fd)
.then((res) => {
- // this.setState({ Category: res.data });
+ requestsDone++;
+ isDone();
})
.catch(() => {
- alert('Impossile de mettre à jour ce végétal');
+ NotificationManager.error('Impossile de mettre à jour ce végétal');
});
+
+ // Patch or create Properties
+ this.state.Vegetable.Properties.forEach(propertyItem => {
+ if (propertyItem.id) {
+ API.patch(`types/${this.state.Vegetable.Type.id}/vegetables/${this.state.Vegetable.id}/properties/${propertyItem.id}`, propertyItem)
+ .then((res) => {
+ requestsDone++;
+ isDone();
+ })
+ .catch(() => {
+ NotificationManager.error(`Impossile de mettre à jour la propriété ${propertyItem.Property.name}`);
+ });
+ } else {
+ API.post(`types/${this.state.Vegetable.Type.id}/vegetables/${this.state.Vegetable.id}/properties`, propertyItem)
+ .then((res) => {
+ requestsDone++;
+ isDone();
+ })
+ .catch(() => {
+ NotificationManager.error(`Impossile de sauvegarder la propriété ayant pour valeur ${propertyItem.value}`);
+ });
+ }
+ })
+
}
postPicture(picture) {
@@ -133,7 +177,7 @@ class Vegetable extends Component {
// this.setState({ Category: res.data });
})
.catch(() => {
- alert('Impossile d\'ajouter cette image');
+ NotificationManager.error(`Impossible d'ajouter cette image`);
});
}
@@ -148,7 +192,7 @@ class Vegetable extends Component {
}
})
.catch(e => {
- alert('Erreur lors de la récupération des types de propriétés');
+ NotificationManager.error('Erreur lors de la récupération des types de propriétés');
});
API.get(`types/${categoryId}/vegetables/${vegetableId}`)
@@ -175,13 +219,14 @@ class Vegetable extends Component {
}
})
.catch((e) => {
- alert('Erreur lors de la récupération de ce végétal');
+ NotificationManager.error('Erreur lors de la récupération de ce végétal');
});
}
render() {
return (