all files / src/datasource/ datasource.ts

56.16% Statements 41/73
45% Branches 9/20
28.57% Functions 4/14
57.97% Lines 40/69
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                                                                                                                                    43× 43× 43×                       43×                                            
/* 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";
var reducer_js_1 = require("../util/reducer.js");
var ActionNames = require("../actionNames");
var Uuid = require("../util/uuid.js");
var _ = require("lodash");
var ModalIds = require("../modal/modalDialogIds");
var Modal = require("../modal/modalDialog.js");
var initialDatasources = {
    "initial_random_source": {
        id: "initial_random_source",
        type: "random",
        settings: {
            name: "Random",
            min: 10,
            max: 20,
            maxValues: 20
        },
        isLoading: true
    }
};
function createDatasource(type, settings, id) {
    Iif (id === void 0) { id = Uuid.generate(); }
    return addDatasource(type, settings, true, id);
}
exports.createDatasource = createDatasource;
function updateDatasource(id, type, settings) {
    return function (dispatch, getState) {
        var state = getState();
        var dsState = state.datasources[id];
        if (!dsState) {
            throw new Error("Failed to update not existing datasource of type '" + type + "' with id '" + id + "'");
        }
        if (dsState.type !== type) {
            throw new Error("Can not update datasource of type '" + dsState.type + "' with props of type '" + type + "'");
        }
        dispatch(updateDatasourceSettings(id, settings));
    };
}
exports.updateDatasource = updateDatasource;
function finishedLoading(id) {
    return {
        type: ActionNames.DATASOURCE_FINISHED_LOADING,
        id: id
    };
}
exports.finishedLoading = finishedLoading;
function addDatasource(dsType, settings, isLoading, id) {
    Iif (isLoading === void 0) { isLoading = true; }
    Iif (id === void 0) { id = Uuid.generate(); }
    Iif (!dsType) {
        console.warn("dsType: ", dsType);
        console.warn("settings: ", settings);
        throw new Error("Can not add Datasource without Type");
    }
    return {
        type: ActionNames.ADD_DATASOURCE,
        id: id,
        dsType: dsType,
        settings: settings,
        isLoading: isLoading
    };
}
exports.addDatasource = addDatasource;
function updateDatasourceSettings(id, settings) {
    // TODO: Working on that copy does not work yet. We need to notify the Datasource about updated settings!
    //let settingsCopy = {...settings};
    return {
        type: ActionNames.UPDATE_DATASOURCE,
        id: id,
        settings: settings
    };
}
exports.updateDatasourceSettings = updateDatasourceSettings;
function startCreateDatasource() {
    return Modal.showModal(ModalIds.DATASOURCE_CONFIG);
}
exports.startCreateDatasource = startCreateDatasource;
function startEditDatasource(id) {
    return function (dispatch, getState) {
        var state = getState();
        var dsState = state.datasources[id];
        dispatch(Modal.showModal(ModalIds.DATASOURCE_CONFIG, { datasource: dsState }));
    };
}
exports.startEditDatasource = startEditDatasource;
function deleteDatasource(id) {
    return {
        type: ActionNames.DELETE_DATASOURCE,
        id: id
    };
}
exports.deleteDatasource = deleteDatasource;
var datasourceCrudReducer = reducer_js_1.genCrudReducer([ActionNames.ADD_DATASOURCE, ActionNames.DELETE_DATASOURCE], datasource);
function datasources(state, action) {
    if (state === void 0) { state = initialDatasources; }
    state = datasourceCrudReducer(state, action);
    switch (action.type) {
        case ActionNames.DELETE_DATASOURCE_PLUGIN: {
            var toDelete = _.valuesIn(state).filter(function (dsState) {
                return dsState.type === action.id;
            });
            var newState_1 = _.assign({}, state);
            toDelete.forEach(function (dsState) {
                delete newState_1[dsState.id];
            });
            return newState_1;
        }
        default:
            return state;
    }
}
exports.datasources = datasources;
function datasource(state, action) {
    switch (action.type) {
        case ActionNames.ADD_DATASOURCE:
            return {
                id: action.id,
                type: action.dsType,
                settings: action.settings,
                isLoading: true
            };
        case ActionNames.UPDATE_DATASOURCE:
            return _.assign({}, state, {
                settings: action.settings
            });
        case ActionNames.DATASOURCE_FINISHED_LOADING: {
            var newState = _.assign({}, state);
            newState.isLoading = false;
            return newState;
        }
        default:
            return state;
    }
}