Professional Documents
Culture Documents
Fonctions React Redux
Fonctions React Redux
js
//Repas
//Commentaires
//Notes
//Ingrédients
//Promotions
//Repas
type : ADD_REPAS,
payload : repas,
});
type : DELETE_REPAS,
payload : idrepas,
});
type : RECHERCHE_REPAS,
payload : nom,
});
type : FILTRER_REPAS_TEMPSPREP ,
payload : temps,
});
export const filter_type = (type) =>({
type : FILTRER_REPAS_TYPECUISINE,
payload : type,
});
type : TRIER_REPAS_PRIX,
payload : prix,
})*/
type : ADD_COMMENT,
});
type : DELETE_COMMENT,
});
type : INCREMENTER_NOTE ,
});
type : DECREMENTER_NOTE ,
});
//Ingrédients
type : ADD_INGREDIENT ,
});
type : DELETE_INGREDIENT ,
});
//Promotions
export const ajouter_promotions = (idr, prom)=>({
type : ADD_PROMOTION ,
});
//reducer.js
import {
ADD_REPAS,
DELETE_REPAS,
RECHERCHE_REPAS,
FILTRER_REPAS_TEMPSPREP,
FILTRER_REPAS_TYPECUISINE,
TRIER_REPAS_PRIX,
ADD_COMMENT,
DELETE_COMMENT,
INCREMENTER_NOTE,
DECREMENTER_NOTE,
ADD_INGREDIENT,
DELETE_INGREDIENT,
ADD_PROMOTION
} from "./actions";
const StateInitiale = {
repas:[
id:1,
nom:"Repas 1",
prix:500,
details:{
calories:500,
allergenes:["Noix","Gluten"],
tempsPreparation:60,
typeCuisine:"Italienne"
},
commentaires:[
{
texte:"Repas Délicieux!",
note:5
},
texte:"Repas Délicieux!",
note:3.5
],
promotions:[
value:-50
},
value:-200
},{
id:2,
nom:"Repas 2",
prix:250,
details:{
listeIngredients:["ingredient 1"],
calories:650,
allergenes:["Gluten"],
tempsPreparation:90,
typeCuisine:"Italienne"
},
commentaires:[
texte:"Repas Délicieux!",
note:4.5
},
texte:"Plat Normal",
note:2.5
],
promotions:[
value:-25
},
},{
id:3,
nom:"Repas 3",
prix:200,
details:{
calories:300,
allergenes:["Noix"],
tempsPreparation:35,
typeCuisine:"française"
},
commentaires:[
texte:"Repas Délicieux!",
note:4
},
texte:"Délice !",
note:5.5
}
],
promotions:[]
switch (action.type){
case ADD_REPAS:
return ({
...state,
repas:[...state.repas, action.payload]
);
case DELETE_REPAS:
return({
...state,
repas:state.repas.filter((r)=>r.id!==action.payload)
);
case ADD_COMMENT:
return({
...state,
repas:state.repas.map((r)=>{
if(r.id===action.payload){
r.commentaires.push(action.payload.comment)
return r
})
);
case DELETE_COMMENT:
return({
...state,
repas:state.repas.map((r)=>{
if(r.id===action.payload.idr) {
r.commentaires=r.commentaires.filter(c=>c.auteur!==action.payload.doe)
//r.commentaires=r.commentaires.pop(c=>c.auteur===action.payload.doe)
return r
})
);
case INCREMENTER_NOTE:
return ({
...state,
repas:state.repas.map((r)=>{
if(r.id===action.payload.idr){
r.commentaires.map((c)=>{
if(c.auteur===action.payload.doe){
c.note=c.note+0.5
})
})
);
case DECREMENTER_NOTE:
return ({
...state,
repas:state.repas.map((r)=>{
if(r.id==action.payload.idr){
r.commentaires.map((c)=>{
if(c.auteur===action.payload.doe){
c.note=c.note-0.5
})
})
);
case ADD_INGREDIENT:
return ({
...state,
repas:state.repas.map((r)=>{
if(r.id===action.payload.idr) {
r.details.listeIngredients.push(action.payload.ing)
})
);
case DELETE_INGREDIENT:
return({
...state,
repas:state.repas.map((r)=>{
if(r.id===action.payload.idr){
r.details.listeIngredients=r.details.listeIngredients.filter(i=>i!==act
ion.payload.ing)
})
);
case ADD_PROMOTION:
return({
...state,
repas:state.repas.map((r)=>{
if(r.id===action.payload.idr){
r.promotions.push(action.payload.prom)
})
);
default :
return (
state
);