|
| 1 | +# Solution with Scalable frontend, with Elm or Redux using **Redux Operations** |
| 2 | + |
| 3 | +# redux-operations package |
| 4 | + |
| 5 | +- The original package is at - https://github.com/mattkrick/redux-operations which combines ideas that myself and Matt had |
| 6 | +- General description and idea behind it can be found at - https://medium.com/@matt.krick/solving-redux-s-shortcoming-in-150-locs-540979ce6cf9#.6kl8l9dic |
| 7 | + |
| 8 | +# general ideas - |
| 9 | +- reducers define an API (can be founed at the API section of the state) |
| 10 | +- each reducer can define functions to be called for specific dispatch calls |
| 11 | +- priority decides the order of actions to be called |
| 12 | +- data is past from one reducer to another as they are called for a specific dispatch |
| 13 | +- reducer functionality is not limited to its "standard" location in state and can be changed by defining a location the reducer/component affect - for example the pair of Random Gifs works by using a normal RandomGif component and giving it a different location in state |
| 14 | + |
| 15 | +# Two solutions for the challenge - |
| 16 | + |
| 17 | +# Common to both solutions - |
| 18 | +- counter is used from our redux-operations example at https://github.com/mattkrick/redux-operations-counter-example, so it has more functions than a regular actions |
| 19 | +- components reusability is simple and allow for more flexibility and less code written, also reduces amounts of data needed to be passed from component to child components |
| 20 | +- the pair of random gifs and the two pairs are simply created by using the RandomGif component and pointing at different locations in the state |
| 21 | +- the update button for each RandomGif component dispatches the UPDATE_GIF action, which calls an async fetch and on data received calls NEW_GIF |
| 22 | +- on NEW_GIF the randomGif reducer is called to update the image source |
| 23 | +- **async** - is written as part of the reducer and invoked as part and not in middleware, so that all application logic is in the reducers |
| 24 | + |
| 25 | +# 1. action chaining |
| 26 | +In this solution we use redux-operations ability to call different functions from different reducers to the same action in a specific order passing one function result to the next |
| 27 | +- on NEW_GIF action after the randomGif reducer is called the button reducer is called, making no change to state but is used so that the counter reducer can get access to the button state |
| 28 | +- last the counter reducer is called (on the same NEW_GIF action) and using the button state passed through the action.meta decides by how much to increase the counter value |
| 29 | + |
| 30 | +# Observations for 1st solution |
| 31 | +- RandomGif component is not aware of any of the rest of the implementation |
| 32 | +- Button is only aware of a UPDATE_GIF call, does not know what it does or how it connects to everything |
| 33 | +- Counter listens to the UPDATE_GIF call and has the addition implementation |
| 34 | +- The API section of the state gives a clear view of how the different reducers react to each action, in what order, and even details about expected arguments |
| 35 | + |
| 36 | +# 2. component location |
| 37 | +- Redux-opertaions allows for components and the reducers they call through dispatch to act on specific location in the state, so we create part of the state of the following structure (with given defualts): |
| 38 | +{ |
| 39 | + gifCounter:{ |
| 40 | + counter: 0, |
| 41 | + button: false |
| 42 | + } |
| 43 | +} |
| 44 | +- Than we point a regular counter to use location ["gifCounter","counter"] and a regular button to ["gifCounter","button"] |
| 45 | +- a new reducer is added called gifCounter - this reducer listens to UPDATE_GIF action and when called uses the counter and button values in its state to increase the counter. |
| 46 | + |
| 47 | +# Observations for 2nd solution |
| 48 | +- Both counter and button do not need to know about each other, the gifCounter reducer or the different RandomGif components. |
| 49 | +- Each component can be written by completely different groups, knowledge of how the components/reducers work together is only at the APP level and the gifCounter reducer. |
| 50 | + |
| 51 | + |
0 commit comments