summaryrefslogtreecommitdiff
path: root/scraper/client/search/search.reducer.js
blob: b9de60bd4c7d8277420d54c80262a7755b20a375 (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
74
75
76
77
78
79
80
81
82
83
84
import * as types from '../types'
import session from '../session'

const initialState = () => ({
  query: { reset: true },
  browse: { reset: true },
  options: {
    thumbnailSize: session('thumbnailSize') || 'th',
    perPage: parseInt(session('perPage'), 10) || 50,
    groupByHash: session('groupByHash'),
  }
})

const loadingState = {
  query: {
    query: { loading: true },
    results: []
  },
  loading: {
    loading: true
  }
}

export default function searchReducer(state = initialState(), action) {
  // console.log(action.type, action)
  switch (action.type) {
    case types.search.loading:
      if (action.tag === 'query' && action.offset) {
        return {
          ...state,
          query: {
            ...state.query,
            loadingMore: true,
          }
        }
      }
      return {
        ...state,
        [action.tag]: loadingState[action.tag] || loadingState.loading,
      }

    case types.search.loaded:
      if (action.tag === 'query' && action.offset) {
        return {
          ...state,
          query: {
            query: action.data.query,
            results: [
              ...state.query.results,
              ...action.data.results,
            ],
            loadingMore: false,
          }
        }
      }
      return {
        ...state,
        [action.tag]: action.data,
      }

    case types.search.error:
      return {
        ...state,
        [action.tag]: { error: action.err },
      }

    case types.search.panic:
      return {
        ...initialState(),
      }

    case types.search.update_options:
      session.setAll(action.opt)
      return {
        ...state,
        options: {
          ...action.opt,
        }
      }

    default:
      return state
  }
}