summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/reducers/search.js
blob: 91e843e18cd797d07104680c40d3dffc429c896b (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  ADD_SEARCH_QUERY,
  ADD_SEARCH_RESULT,
  CLEAR_SEARCH_RESULTS,
  ADD_ONGOING_SEARCH,
  SEARCH_STATUS,
  TOGGLE_SEARCH_CASE_SENSITIVE_SEARCH,
  UPDATE_SEARCH_STATUS,
  SET_TARGET_SEARCH_RESULT,
} = require("resource://devtools/client/netmonitor/src/constants.js");

/**
 * Search reducer stores the following data:
 * - query [String]: the string the user is looking for
 * - results [Object]: the list of search results
 * - ongoingSearch [Object]: the object representing the current search
 * - status [String]: status of the current search (see constants.js)
 */
function Search(overrideParams = {}) {
  return Object.assign(
    {
      query: "",
      results: [],
      ongoingSearch: null,
      status: SEARCH_STATUS.INITIAL,
      caseSensitive: false,
      targetSearchResult: null,
    },
    overrideParams
  );
}

function search(state = new Search(), action) {
  switch (action.type) {
    case ADD_SEARCH_QUERY:
      return onAddSearchQuery(state, action);
    case ADD_SEARCH_RESULT:
      return onAddSearchResult(state, action);
    case CLEAR_SEARCH_RESULTS:
      return onClearSearchResults(state);
    case ADD_ONGOING_SEARCH:
      return onAddOngoingSearch(state, action);
    case TOGGLE_SEARCH_CASE_SENSITIVE_SEARCH:
      return onToggleCaseSensitiveSearch(state);
    case UPDATE_SEARCH_STATUS:
      return onUpdateSearchStatus(state, action);
    case SET_TARGET_SEARCH_RESULT:
      return onSetTargetSearchResult(state, action);
  }
  return state;
}

function onAddSearchQuery(state, action) {
  return {
    ...state,
    query: action.query,
  };
}

function onAddSearchResult(state, action) {
  const { resource } = action;
  const results = state.results.slice();
  results.push({
    resource,
    results: action.result,
  });

  return {
    ...state,
    results,
  };
}

function onClearSearchResults(state) {
  return {
    ...state,
    results: [],
  };
}

function onAddOngoingSearch(state, action) {
  return {
    ...state,
    ongoingSearch: action.ongoingSearch,
  };
}

function onToggleCaseSensitiveSearch(state) {
  return {
    ...state,
    caseSensitive: !state.caseSensitive,
  };
}

function onUpdateSearchStatus(state, action) {
  return {
    ...state,
    status: action.status,
  };
}

function onSetTargetSearchResult(state, action) {
  return {
    ...state,
    targetSearchResult: action.searchResult,
  };
}

module.exports = {
  Search,
  search,
};