Initial commit
This commit is contained in:
35
frontend/src/reducers/chat.js
Normal file
35
frontend/src/reducers/chat.js
Normal file
@ -0,0 +1,35 @@
|
||||
import { LOGIN_SUCCESS, LOGIN_FAILURE } from '../actions/login';
|
||||
import { FETCH_MESSAGES_SUCCESS } from '../actions/chat';
|
||||
|
||||
const defaultState = {
|
||||
messagesByChannel: {},
|
||||
}
|
||||
|
||||
export default function chatReducer(state = defaultState, action) {
|
||||
switch (action.type) {
|
||||
case FETCH_MESSAGES_SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
messagesByChannel: {
|
||||
...state.messagesByChannel,
|
||||
[action.channel]: [...action.data.Messages]
|
||||
}
|
||||
};
|
||||
case 'CHANNEL_EVENT':
|
||||
switch(action.event) {
|
||||
case "message":
|
||||
return {
|
||||
...state,
|
||||
messagesByChannel: {
|
||||
...state.messagesByChannel,
|
||||
[action.data.Channel]: [
|
||||
...state.messagesByChannel[action.data.Channel],
|
||||
action.data.Message
|
||||
]
|
||||
}
|
||||
};
|
||||
}
|
||||
return state;
|
||||
}
|
||||
return state;
|
||||
}
|
16
frontend/src/reducers/login.js
Normal file
16
frontend/src/reducers/login.js
Normal file
@ -0,0 +1,16 @@
|
||||
import { LOGIN_SUCCESS, LOGIN_FAILURE } from '../actions/login';
|
||||
|
||||
const defaultState = {
|
||||
isLoggedIn: false,
|
||||
username: null,
|
||||
}
|
||||
|
||||
export default function loginReducer(state = defaultState, action) {
|
||||
switch (action.type) {
|
||||
case LOGIN_SUCCESS:
|
||||
return { ...state, isLoggedIn: true, username: action.data.Username };
|
||||
case LOGIN_FAILURE:
|
||||
return { ...state, isLoggedIn: false, username: null };
|
||||
}
|
||||
return state;
|
||||
}
|
31
frontend/src/reducers/root.js
Normal file
31
frontend/src/reducers/root.js
Normal file
@ -0,0 +1,31 @@
|
||||
import { ADD_PRODUCT, REMOVE_PRODUCT } from '../actions/products';
|
||||
|
||||
export const rootReducer = (state, action) => {
|
||||
|
||||
console.log(`Action: ${JSON.stringify(action)}`)
|
||||
|
||||
switch (action.type) {
|
||||
|
||||
case ADD_PRODUCT:
|
||||
// L'action est de type ADD_PRODUCT
|
||||
// On ajoute le produit dans la liste et
|
||||
// on retourne un nouvel état modifié
|
||||
return {
|
||||
products: [...state.products, action.product]
|
||||
}
|
||||
|
||||
|
||||
case REMOVE_PRODUCT:
|
||||
// L'action est de type REMOVE_PRODUCT
|
||||
// On filtre la liste des produits et on
|
||||
// retourne un nouvel état modifié
|
||||
return {
|
||||
products: state.products.filter(p => p.name !== action.productName)
|
||||
}
|
||||
}
|
||||
|
||||
// Si l'action n'est pas gérée, on retourne l'état
|
||||
// sans le modifier
|
||||
return state
|
||||
|
||||
}
|
Reference in New Issue
Block a user