Personal Dashboard

note.js 1.1KB

12345678910111213141516171819202122232425262728
  1. import { _ } from '../../utils'
  2. import { handleActions } from 'redux-actions'
  3. import { combineReducers } from 'redux'
  4. import {
  5. ADD_NOTE, UPDATE_NOTE, UPDATE_SELECTED_NOTE,
  6. REMOVE_NOTE, SELECT_NOTE, UNSELECT_NOTE
  7. } from '../Actions'
  8. const MOCK_NOTES = [
  9. {id: _.uniqueId(), title: 'First note', content: 'This is a little note' },
  10. {id: _.uniqueId(), title: 'Second note', content: '### This is a little md note' },
  11. ]
  12. const selectedNote = handleActions({
  13. [SELECT_NOTE]: (state, action) => state = action.payload,
  14. [UPDATE_SELECTED_NOTE]: (state, action) => ({...state, ...action.payload }),
  15. [UNSELECT_NOTE]: (state, action) => null
  16. }, null)
  17. const notes = handleActions({
  18. [ADD_NOTE]: (state, action) => [...state, { id: _.uniqueId(), title: 'new_note', content: '' }],
  19. [UPDATE_NOTE]: (state, action) => _.updateWhere(state, action.payload, _.idCheck(action.payload.id)),
  20. [REMOVE_NOTE]: (state, action) => _.reject(state, _.idCheck(action.payload.id)),
  21. }, MOCK_NOTES)
  22. const note = combineReducers({ notes, selectedNote })
  23. export default note