Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. const EMIT_NOTIFICATION = 'EMIT_NOTIFICATION'
  2. const HIDE_NOTIFICATION = 'HIDE_NOTIFICATION'
  3.  
  4. const initialState = {
  5. maxItems: 4,
  6. dismissTimeout: 10000, // 10 seconds
  7. items: [],
  8. queue: []
  9. }
  10.  
  11. class NotificationItem {
  12. constructor (item) {
  13. this.title = item.title || null
  14. this.message = item.message || null
  15. this.style = item.type || 'info'
  16. this.dismissable = item.dismissable || true
  17. this.visible = (typeof item.visible === 'undefined') ? true : item.visible
  18. }
  19. }
  20.  
  21. // let _timeouts = []
  22. const _emit = (payload = {}) => ({
  23. type: EMIT_NOTIFICATION,
  24. payload
  25. })
  26. const _dismiss = (index = 0) => ({
  27. type: HIDE_NOTIFICATION,
  28. index
  29. })
  30.  
  31. export function emit (payload = {}) {
  32. return (dispatch, getState) => {
  33. dispatch(_emit(payload))
  34. // const state = getState().notification
  35. // const dismissTimeout = payload.timeout || state.dismissTimeout
  36. // const timeoutId = setTimeout(() => {
  37. // dispatch(dismiss())
  38. // }, dismissTimeout)
  39. }
  40. }
  41.  
  42. export function dismiss (index = 0) {
  43. return (dispatch, getState) => {
  44. dispatch(_dismiss(index))
  45. }
  46. }
  47. // ------------------------------------
  48. // Action Handlers
  49. // ------------------------------------
  50. const ACTION_HANDLERS = {
  51. [EMIT_NOTIFICATION] : (state, action) => {
  52. let newState = Object.assign({}, state)
  53. let newNotification = new NotificationItem(action.payload)
  54. if (newState.items.length >= newState.maxItems) {
  55. newState.queue = [
  56. ...newState.queue,
  57. {
  58. ...newNotification
  59. }
  60. ]
  61. } else {
  62. newState.items = [
  63. ...newState.items,
  64. {
  65. ...newNotification
  66. }
  67. ]
  68. }
  69. return newState
  70. },
  71.  
  72. [HIDE_NOTIFICATION]: (state, { index }) => {
  73. let newState = Object.assign({}, state)
  74.  
  75. // clear all notifications by returning initialState
  76. if (index === -1) {
  77. return initialState
  78. } else {
  79. newState.items = [
  80. ...newState.items.slice(0, index),
  81. ...newState.items.slice(index + 1)
  82. ]
  83.  
  84. // since were under 4 items in the items[] array, lets grab from queue
  85. // and push into the end of items[] array
  86. if ((newState.queue.length > 0) && (newState.items.length < newState.maxItems)) {
  87. newState.items = [
  88. ...newState.items.slice(0, index),
  89. ...newState.items.slice(index + 1),
  90. ...newState.queue.slice(0, 1)
  91. ]
  92.  
  93. newState.queue = [
  94. ...newState.queue.slice(0, 1),
  95. ...newState.queue.slice(2)
  96. ]
  97. }
  98. return newState
  99. }
  100. }
  101. }
  102.  
  103. export const actions = {
  104. emit,
  105. dismiss
  106. }
  107.  
  108. // ------------------------------------
  109. // Reducer
  110. // ------------------------------------
  111. export default function notificationReducer (state = initialState, action) {
  112. const handler = ACTION_HANDLERS[action.type]
  113.  
  114. return handler ? handler(state, action) : state
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement