summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/reducers/sort.js
blob: 0d3486f57b213ebbfef0116faf7c193c4cf9ffb5 (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
/* 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 {
  SORT_BY,
  RESET_COLUMNS,
} = require("resource://devtools/client/netmonitor/src/constants.js");

function Sort() {
  return {
    // null means: sort by "waterfall", but don't highlight the table header
    type: null,
    ascending: true,
  };
}

function sortReducer(state = new Sort(), action) {
  switch (action.type) {
    case SORT_BY: {
      state = { ...state };
      if (action.sortType != null && action.sortType == state.type) {
        state.ascending = !state.ascending;
      } else {
        state.type = action.sortType;
        state.ascending = true;
      }
      return state;
    }

    case RESET_COLUMNS: {
      state = { ...state };
      state.type = null;
      state.ascending = true;
      return state;
    }

    default:
      return state;
  }
}

module.exports = {
  Sort,
  sortReducer,
};