Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

//actions.

js

//Repas

export const ADD_REPAS = 'ADD_REPAS';

export const DELETE_REPAS = 'DELETE_REPAS';

export const RECHERCHE_REPAS = 'RECHERCHE_REPAS';

export const FILTRER_REPAS_TEMPSPREP = 'FILTRER_REPAS_TEMPSPREP';

export const FILTRER_REPAS_TYPECUISINE = 'FILTRER_REPAS_TYPECUISINE';

export const TRIER_REPAS_PRIX = 'TRIER_REPAS_PRIX';

//Commentaires

export const ADD_COMMENT = 'ADD_COMMENT';

export const DELETE_COMMENT = 'DELETE_COMMENT';

//Notes

export const INCREMENTER_NOTE = 'INCREMENTER_NOTE';

export const DECREMENTER_NOTE = 'DECREMENTER_NOTE';

//Ingrédients

export const ADD_INGREDIENT = 'ADD_INGREDIENT';

export const DELETE_INGREDIENT = 'DELETE_INGREDIENT';

//Promotions

export const ADD_PROMOTION = 'ADD_PROMOTION';

//Repas

export const ajouter_repas = (repas) =>({

type : ADD_REPAS,

payload : repas,

});

export const supprimer_repas = (idrepas) =>({

type : DELETE_REPAS,

payload : idrepas,

});

/*export const rechercher_repas = (nom) =>({

type : RECHERCHE_REPAS,

payload : nom,

});

export const filter_temps = (temps) =>({

type : FILTRER_REPAS_TEMPSPREP ,

payload : temps,

});
export const filter_type = (type) =>({

type : FILTRER_REPAS_TYPECUISINE,

payload : type,

});

export const trie_prix = (prix) =>({

type : TRIER_REPAS_PRIX,

payload : prix,

})*/

//Commentaires & Notes

export const ajouter_commentaire = (idr, comment) =>({

type : ADD_COMMENT,

payload : {idr, comment},

});

export const supprimer_commentaire = (doe, idr) =>({

type : DELETE_COMMENT,

payload : {doe, idr},

});

export const incrementer_note= (doe, idr) =>({

type : INCREMENTER_NOTE ,

payload : {doe, idr} ,

});

export const decrementer_note= (doe, idr) =>({

type : DECREMENTER_NOTE ,

payload : {doe, idr} ,

});

//Ingrédients

export const ajouter_ingredient = (idr, ing)=>({

type : ADD_INGREDIENT ,

payload : {idr, ing} ,

});

export const supprimer_ingredient = (idr, ing)=>({

type : DELETE_INGREDIENT ,

payload : {idr, ing} ,

});

//Promotions
export const ajouter_promotions = (idr, prom)=>({

type : ADD_PROMOTION ,

payload : {idr, prom} ,

});

//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:{

listeIngredients:["ingredient 1","ingredient 2","ingredient 3"],

calories:500,

allergenes:["Noix","Gluten"],

tempsPreparation:60,

typeCuisine:"Italienne"

},

commentaires:[
{

auteur:"John Doe 1",

texte:"Repas Délicieux!",

note:5

},

auteur:"John Doe 2",

texte:"Repas Délicieux!",

note:3.5

],

promotions:[

libelle: "Réduction de 10%",

description: "Valable jusqu'au 31 décembre.",

value:-50

},

libelle: "Réduction de 40%",

description: "New Year !",

value:-200

},{

id:2,

nom:"Repas 2",

prix:250,

details:{

listeIngredients:["ingredient 1"],

calories:650,

allergenes:["Gluten"],

tempsPreparation:90,

typeCuisine:"Italienne"

},

commentaires:[

auteur:"John Doe 1",

texte:"Repas Délicieux!",
note:4.5

},

auteur:"John Doe 2",

texte:"Plat Normal",

note:2.5

],

promotions:[

libelle: "Réduction de 10%",

description: "Valable jusqu'au 31 décembre.",

value:-25

},

},{

id:3,

nom:"Repas 3",

prix:200,

details:{

listeIngredients:["ingredient 1","ingredient 2"],

calories:300,

allergenes:["Noix"],

tempsPreparation:35,

typeCuisine:"française"

},

commentaires:[

auteur:"John Doe 1",

texte:"Repas Délicieux!",

note:4

},

auteur:"Jane Doe 1",

texte:"Délice !",

note:5.5

}
],

promotions:[]

const MenuReducer = (state=StateInitiale, action) => {

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

);

export default MenuReducer;

You might also like