From d0b8a09dc13553dc73241d96f271a7fd6ba13658 Mon Sep 17 00:00:00 2001 From: Darien Duong Date: Thu, 11 Jan 2018 13:19:16 -0800 Subject: [PATCH] initial commit --- Unit-01/05-redux/index.html | 1 + Unit-01/05-redux/main.js | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/Unit-01/05-redux/index.html b/Unit-01/05-redux/index.html index 5fa11b4..5ea7330 100644 --- a/Unit-01/05-redux/index.html +++ b/Unit-01/05-redux/index.html @@ -20,6 +20,7 @@

+ diff --git a/Unit-01/05-redux/main.js b/Unit-01/05-redux/main.js index e69de29..7fbc147 100644 --- a/Unit-01/05-redux/main.js +++ b/Unit-01/05-redux/main.js @@ -0,0 +1,56 @@ +const DEFAULT_STATE = { + face: 'click a button' +} + +function rootReducer(state=DEFAULT_STATE, action){ + switch (action.type){ + case 'HAPPY': { + return {...state, face: '=]'} + } + case 'SAD': { + return {...state, face: '=['} + } + case 'ANGRY': { + return {...state, face: '>=['} + } + case 'CONFUSED': { + return {...state, face: '@_@'} + } + default: { + return {...state} + } + } +} + +const store = Redux.createStore( + rootReducer, + window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() +) + +$(function(){ + + var $face = $('#face') + + function render(){ + $face.text(store.getState().face) + } + + render() + store.subscribe(render) + + $("#happy").on("click", function(){ + store.dispatch({type: 'HAPPY'}) + }); + + $("#sad").on("click", function(){ + store.dispatch({type: 'SAD'}) + }); + + $("#angry").on("click", function(){ + store.dispatch({type: 'ANGRY'}) + }); + + $("#confused").on("click", function(){ + store.dispatch({type: 'CONFUSED'}) + }); +}); \ No newline at end of file