Skip to content
Snippets Groups Projects
Commit 1d5e5a94 authored by Cody Dumortier's avatar Cody Dumortier
Browse files

fin du tp

parent 910ddf09
Branches
No related tags found
No related merge requests found
import Router from '../Router.js';
import Page from './Page.js';
import PizzaList from './PizzaList.js';
export default class AddPizzaPage extends Page {
render() {
......@@ -8,6 +10,21 @@ export default class AddPizzaPage extends Page {
Nom :
<input type="text" name="name">
</label>
<label>
Image :<br/>
<input type="text" name="image" placeholder="https://source.unsplash.com/xxxxxxx/600x600">
<small>Vous pouvez trouver des images de pizza sur <a href="https://unsplash.com/">https://unsplash.com/</a> puis utiliser l'URL <code>https://source.unsplash.com/xxxxxxx/600x600</code> où <code>xxxxxxx</code> est l'id de la photo choisie (celui dans la barre d'adresse)</small>
</label>
<label>
Prix petit format :
<input type="number" name="price_small" step="0.05">
</label>
<label>
Prix grand format :
<input type="number" name="price_large" step="0.05">
</label>
<button type="submit">Ajouter</button>
</form>`;
}
......@@ -25,17 +42,43 @@ export default class AddPizzaPage extends Page {
// D.4. La validation de la saisie
const nameInput = this.element.querySelector('input[name="name"]'),
name = nameInput.value;
const imageInput = this.element.querySelector('input[name="image"]'),
image = imageInput.value;
const priceSmallInput = this.element.querySelector(
'input[name="price_small"]'
),
priceSmall = priceSmallInput.value;
const priceLargeInput = this.element.querySelector(
'input[name="price_large"]'
),
priceLarge = priceLargeInput.value;
if (name === '') {
alert('Erreur : le champ "Nom" est obligatoire');
return;
}
const pizza = {
name: name,
image: image,
price_small: priceSmall,
price_large: priceLarge,
};
fetch('http://localhost:8080/api/v1/pizzas', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(name),
});
body: JSON.stringify(pizza),
}).then(response => {
if (!response.ok) {
throw new Error(`Erreur : ${response.statusText}`);
} else {
alert(`La pizza ${name} a été ajoutée !`);
Router.navigate('/');
}
});
nameInput.value = '';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment