blob: 0c968d33ae2d2bc32b5a71bbf44925d72d3b7430 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Module of functions which produce a new page state in response
* to an action. Reducers (in the same sense as Array.prototype.reduce) must be
* pure functions: they must not modify existing state objects, or make any API
* calls.
*/
cr.define('app_management', function() {
const AppState = {};
/**
* @param {AppMap} apps
* @param {Object} action
* @return {AppMap}
*/
AppState.addApps = function(apps, action) {
const newAppEntries = {};
for (const app of action.apps) {
newAppEntries[app.id] = app;
}
return Object.assign({}, apps, newAppEntries);
};
/**
* @param {AppMap} apps
* @param {Object} action
* @return {AppMap}
*/
AppState.updateApps = function(apps, action) {
switch (action.name) {
case 'add-apps':
return AppState.addApps(apps, action);
default:
return apps;
}
};
/**
* Root reducer for the App Management page. This is called by the store in
* response to an action, and the return value is used to update the UI.
* @param {!AppManagementPageState} state
* @param {Object} action
* @return {!AppManagementPageState}
*/
function reduceAction(state, action) {
return {
apps: AppState.updateApps(state.apps, action),
};
}
return {
reduceAction: reduceAction,
AppState: AppState,
};
});