Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. /*eslint-disable */
  2.  
  3. const PUSH = '@@nav/PUSH';
  4. const POP = '@@nav/POP';
  5.  
  6. const INDEX = '@@articles/INDEX';
  7. const ARTICLE = '@@articles/ARTICLE';
  8. const COMMENTS = '@@articles/COMMENTS';
  9.  
  10.  
  11. const navActions = {
  12. push: (payload) => { type: PUSH, payload },
  13. pop: () => { type: POP }
  14. };
  15.  
  16. const routeMap = [
  17. {
  18. key: ARTICLES,
  19. renderer: (props) => {}
  20. }, {
  21. key: ARTICLE,
  22. renderer: (props) => {}
  23. }, {
  24. key: COMMENTS,
  25. renderer: (props) => {}
  26. }
  27. ];
  28.  
  29. routeMap.prototype.get = function(key) {
  30. const keys = routeMap.map(r => r.key);
  31. const route = keys[key];
  32.  
  33. if (route) {
  34. return route.renderer;
  35. } else {
  36. return null;
  37. }
  38. }
  39.  
  40. const initialState = {
  41. key: 'articles',
  42. index: 0,
  43. history: [
  44. {
  45. key: 'index',
  46. index: 0
  47. }
  48. ]
  49. };
  50.  
  51. const articlesReducer = (state = initialState, action) => {
  52. switch(action.type) {
  53. case PUSH:
  54. return {
  55. ...state,
  56. history: history.push(action.payload)
  57. };
  58. case POP: {
  59. if (history.length > 1) {
  60. return {
  61. ...state,
  62. history: history.pop() // imaginary api
  63. };
  64. } else {
  65. return state;
  66. }
  67. }
  68.  
  69. default:
  70. return state;
  71. }
  72. };
  73.  
  74. const render = (props) => {
  75. const component = routeMap.get(props.history.last.key);
  76.  
  77. if (component) {
  78. return renderer(props);
  79. } else {
  80. return null;
  81. }
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement