mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
168 lines (167 loc) • 5.55 kB
JavaScript
;
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.subscription = subscription;
exports.limits = limits;
exports.errors = errors;
const redux_1 = require("redux");
const action_types_1 = require("mattermost-redux/action_types");
function subscription(state = null, action) {
switch (action.type) {
case action_types_1.CloudTypes.RECEIVED_CLOUD_SUBSCRIPTION: {
return action.data;
}
default:
return state;
}
}
function customer(state = null, action) {
switch (action.type) {
case action_types_1.CloudTypes.RECEIVED_CLOUD_CUSTOMER: {
return action.data;
}
default:
return state;
}
}
function products(state = null, action) {
switch (action.type) {
case action_types_1.CloudTypes.RECEIVED_CLOUD_PRODUCTS: {
const productList = action.data;
const productDict = productList.reduce((map, obj) => {
map[obj.id] = obj;
return map;
}, {});
return {
...state,
...productDict,
};
}
default:
return state;
}
}
function invoices(state = null, action) {
switch (action.type) {
case action_types_1.CloudTypes.RECEIVED_CLOUD_INVOICES: {
const invoiceList = action.data;
const invoiceDict = invoiceList.reduce((map, obj) => {
map[obj.id] = obj;
return map;
}, {});
return {
...state,
...invoiceDict,
};
}
default:
return state;
}
}
const emptyLimits = {
limits: {},
limitsLoaded: false,
};
function limits(state = emptyLimits, action) {
switch (action.type) {
case action_types_1.CloudTypes.RECEIVED_CLOUD_LIMITS: {
return {
limits: action.data,
limitsLoaded: true,
};
}
case action_types_1.CloudTypes.RECEIVED_CLOUD_SUBSCRIPTION: {
return emptyLimits;
}
default:
return state;
}
}
const emptyErrors = {};
function errors(state = emptyErrors, action) {
switch (action.type) {
case action_types_1.CloudTypes.CLOUD_SUBSCRIPTION_FAILED: {
return { ...state, subscription: true };
}
case action_types_1.CloudTypes.CLOUD_PRODUCTS_FAILED: {
return { ...state, products: true };
}
case action_types_1.CloudTypes.CLOUD_CUSTOMER_FAILED: {
return { ...state, customer: true };
}
case action_types_1.CloudTypes.CLOUD_INVOICES_FAILED: {
return { ...state, invoices: true };
}
case action_types_1.CloudTypes.CLOUD_LIMITS_FAILED: {
return { ...state, limits: true };
}
case action_types_1.CloudTypes.RECEIVED_CLOUD_SUBSCRIPTION: {
const newState = Object.assign({}, state);
delete newState.subscription;
return newState;
}
case action_types_1.CloudTypes.RECEIVED_CLOUD_PRODUCTS: {
const newState = Object.assign({}, state);
delete newState.products;
return newState;
}
case action_types_1.CloudTypes.RECEIVED_CLOUD_CUSTOMER: {
const newState = Object.assign({}, state);
delete newState.customer;
return newState;
}
case action_types_1.CloudTypes.RECEIVED_CLOUD_INVOICES: {
const newState = Object.assign({}, state);
delete newState.invoices;
return newState;
}
case action_types_1.CloudTypes.RECEIVED_CLOUD_LIMITS: {
const newState = Object.assign({}, state);
delete newState.limits;
return newState;
}
case action_types_1.CloudTypes.CLOUD_SUBSCRIPTION_REQUEST: {
const newState = Object.assign({}, state);
delete newState.subscription;
return newState;
}
case action_types_1.CloudTypes.CLOUD_PRODUCTS_REQUEST: {
const newState = Object.assign({}, state);
delete newState.products;
return newState;
}
case action_types_1.CloudTypes.CLOUD_CUSTOMER_REQUEST: {
const newState = Object.assign({}, state);
delete newState.customer;
return newState;
}
case action_types_1.CloudTypes.CLOUD_INVOICES_REQUEST: {
const newState = Object.assign({}, state);
delete newState.invoices;
return newState;
}
case action_types_1.CloudTypes.CLOUD_LIMITS_REQUEST: {
const newState = Object.assign({}, state);
delete newState.limits;
return newState;
}
default: {
return state;
}
}
}
exports.default = (0, redux_1.combineReducers)({
// represents the current cloud customer
customer,
// represents the current cloud subscription
subscription,
// represents the cloud products offered
products,
// represents the invoices tied to the current subscription
invoices,
// represents the usage limits associated with this workspace
limits,
// network errors, used to show errors in ui instead of blowing up and showing nothing
errors,
});