summaryrefslogtreecommitdiff
path: root/app/client/queue/queue.reducer.js
blob: 040cc331e53b911e154221c084494aab986b4941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import types from '../types'
import util from '../util'
import moment from 'moment/min/moment.min'

const queueInitialState = {
  loading: false,
  error: null,
  tasks: {},
  queue: [],
  completed: [],
}

const dateSort = util.sort.orderByFn('updated_at desc')
const prioritySort = util.sort.orderByFn('priority asc')

const queueReducer = (state = queueInitialState, action) => {
  switch(action.type) {
    case types.task.index:
      return {
        ...state,
        tasks: action.data.reduce((a,b) => (a[b.id] = b, a), {}),
        queue: action.data
          .filter(a => !a.completed)
          .map(dateSort.mapFn)
          .sort(dateSort.sortFn)
          .map(pair => pair[1].id),
        completed: action.data
          .filter(a => a.completed)
          .map(prioritySort.mapFn)
          .sort(prioritySort.sortFn)
          .map(pair => pair[1].id),
      }
    case types.task.create:
      console.log(action.data)
      return {
        ...state,
        tasks: {
          ...state.tasks,
          [action.data.id]: action.data,
        },
        queue: state.queue.concat([action.data.id]),
      }
    case types.task.update:
      return {
        ...state,
        tasks: {
          ...state.tasks,
          [action.data.id]: action.data,
        },
      }
    case types.task.destroy:
      const {
        [action.data.id]: deletedTask,
        ...taskLookup,
      } = state.tasks
      return {
        ...state,
        queue: state.queue.filter(id => id !== deletedTask.id),
        completed: state.completed.filter(id => id !== deletedTask.id),
        tasks: taskLookup,
      }
    case types.task.task_finish:
      return {
        ...state,
        queue: state.queue.filter(a => a !== action.task.id),
        completed: [action.task.id].concat(state.completed)
      }
    default:
      return state
  }
}

export default queueReducer