browser/extensions/activity-stream/common/Reducers.jsm
author Ed Lee <edilee@mozilla.com>
Tue, 25 Apr 2017 13:31:29 -0700
changeset 354818 a3b4b98b525b75f3276f9e226781ee4bcc1a33c0
parent 351931 dfbfa3dd4a26ab341b31e2c77fc5d6d3d4554156
child 357976 13de8636a5deb05a360ead676d96b6ebf352e33f
permissions -rw-r--r--
Bug 1359481 - Add top sites and search to activity-stream system add-on. r=Mardak

/* 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 {actionTypes: at} = Components.utils.import("resource://activity-stream/common/Actions.jsm", {});

const INITIAL_STATE = {
  TopSites: {
    init: false,
    rows: []
  },
  Search: {
    currentEngine: {
      name: "",
      icon: ""
    },
    engines: []
  }
};

// TODO: Handle some real actions here, once we have a TopSites feed working
function TopSites(prevState = INITIAL_STATE.TopSites, action) {
  let hasMatch;
  let newRows;
  switch (action.type) {
    case at.TOP_SITES_UPDATED:
      if (!action.data) {
        return prevState;
      }
      return Object.assign({}, prevState, {init: true, rows: action.data});
    case at.SCREENSHOT_UPDATED:
      newRows = prevState.rows.map(row => {
        if (row.url === action.data.url) {
          hasMatch = true;
          return Object.assign({}, row, {screenshot: action.data.screenshot});
        }
        return row;
      });
      return hasMatch ? Object.assign({}, prevState, {rows: newRows}) : prevState;
    default:
      return prevState;
  }
}

function Search(prevState = INITIAL_STATE.Search, action) {
  switch (action.type) {
    case at.SEARCH_STATE_UPDATED: {
      if (!action.data) {
        return prevState;
      }
      let {currentEngine, engines} = action.data;
      return Object.assign({}, prevState, {
        currentEngine,
        engines
      });
    }
    default:
      return prevState;
  }
}
this.INITIAL_STATE = INITIAL_STATE;
this.reducers = {TopSites, Search};

this.EXPORTED_SYMBOLS = ["reducers", "INITIAL_STATE"];