lml-main
Version:
This is now a mono repository published into many standalone packages.
85 lines • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var lodash_1 = require("lodash");
var Actions = require("../actions");
var ts_utils_1 = require("@lml/ts-utils");
exports.initialJobStatusState = {
byStatus: {},
search: {},
};
exports.jobStatusReducer = function (state, action) {
if (state === void 0) { state = exports.initialJobStatusState; }
switch (action.type) {
case Actions.SET_JOB_STATUS_BY_ID: {
return setJobStatusById(state, action);
}
case Actions.SET_JOB_STATUSES_BY_ID_BATCH: {
return setJobStatusesByIdBatch(state, action);
}
case Actions.SET_JOB_STATUS_SEARCH: {
return setJobStatusSearch(state, action);
}
// TODO: Remove when we have push
case Actions.CHANGE_JOB_STATUS: {
return changeJobStatus(state, action);
}
default: {
return state;
}
}
};
var setJobStatusById = function (state, action) {
return (tslib_1.__assign({}, state, { byStatus: tslib_1.__assign({}, state.byStatus, (_a = {}, _a[action.jobStatus.refId] = action.jobStatus, _a)) }));
var _a;
};
var setJobStatusesByIdBatch = function (state, action) { return (tslib_1.__assign({}, state, { byStatus: tslib_1.__assign({}, state.byStatus, ts_utils_1.arrayToMap(action.jobStatuses, 'refId')) })); };
var setJobStatusSearch = function (state, action) {
var searchKey = action.searchKey, params = action.params, jobs = action.jobs;
// check for existing search results saved against this searchKey
var existingSearch = state.search[searchKey];
// if there is an existing search result
if (existingSearch) {
// diff the incoming array of refIds against the one saved in the state
// if nothing has changed we don't need to do anything
if (!isDifferent(existingSearch.results, jobs))
return state;
}
// otherwise save the latest version of everything in the state
// ie. jobStatusMap, array of refIds, and query params
// plus add in a timestamp for when these results were saved
return tslib_1.__assign({}, state, { byStatus: tslib_1.__assign({}, state.byStatus, ts_utils_1.arrayToMap(action.jobs, 'refId')), search: tslib_1.__assign({}, state.search, (_a = {}, _a[searchKey] = {
params: params,
results: jobs,
lastUpdate: Date.now(),
}, _a)) });
var _a;
};
var changeJobStatus = function (state, _a) {
var prevStatus = _a.prevStatus, nextStatus = _a.nextStatus, job = _a.job;
var update = lodash_1.cloneDeep(job);
update.status = nextStatus;
// TODO - we also need to remove this job from any search list containing the previous status
// Object.keys().reduce()
return tslib_1.__assign({}, state, { byStatus: tslib_1.__assign({}, state.byStatus, (_b = {}, _b[job.refId] = update, _b)), search: tslib_1.__assign({}, state.search, (_c = {}, _c[prevStatus] = {
params: tslib_1.__assign({}, lodash_1.get(state.search[prevStatus], 'params')),
results: lodash_1.filter(lodash_1.get(state.search[prevStatus], 'results', []), function (j) { return j.refId !== job.refId; }),
lastUpdate: Date.now(),
}, _c)) });
var _b, _c;
};
// simple for loop allows us to break out
// of the diff at the earliest opportunity
var isDifferent = function (a, b) {
if (a.length !== b.length)
return true;
for (var i = 0; i < a.length; i++) {
// check the refids are in the exact same order
// also check that the lastAction hasn't changed
if ((a[i].refId !== b[i].refId) || (a[i].lastAction !== b[i].lastAction)) {
return true;
}
}
return false;
};
//# sourceMappingURL=status.js.map