styled-hook-form
Version:
React form library for styled-components based on grommet and react-hook-form
113 lines (112 loc) • 4.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.actionImpl = exports.getSyncKey = void 0;
const immutable_1 = require("immutable");
const DEFAULT_ORDER_DIR = "desc";
const getSyncKey = () => new Date().getTime();
exports.getSyncKey = getSyncKey;
const findOrderedIndex = (list, primaryKey, obj, currentPage, orderDir = "asc", orderParam) => {
let id_prop = orderParam !== null && orderParam !== void 0 ? orderParam : primaryKey;
let sortedList = list.sort((a, b) => {
return typeof a[id_prop] === "number"
? a - b
: orderDir === "asc"
? ("" + a[id_prop]).localeCompare(b[id_prop])
: ("" + b.attr).localeCompare(a.attr);
});
for (let i = 0; i < sortedList.length; i++) {
let match = orderDir === "asc"
? obj[id_prop] < sortedList[i][id_prop]
: obj[id_prop] > sortedList[i][id_prop];
if (match) {
return i > 0 ? i : currentPage === 1 ? 0 : -1;
}
}
return list.length;
};
const removeData = (state, payload) => {
var _a;
let newItems = ((_a = state.data) !== null && _a !== void 0 ? _a : []).filter((item) => {
let key = item[state.primaryKey];
let match = typeof payload === "number" || typeof payload === "string"
? key === payload
: payload(item);
return !match;
});
return Object.assign(Object.assign({}, state), { data: newItems, syncKey: exports.getSyncKey() });
};
const updateData = (state, payload) => {
let itemsToUpdate = Array.isArray(payload) ? payload : [payload];
let newItems = (state.data || []).map((item) => {
let key = item[state.primaryKey];
let match = itemsToUpdate.find((e) => e[state.primaryKey] === key);
return match !== null && match !== void 0 ? match : item;
});
return Object.assign(Object.assign({}, state), { data: newItems });
};
const addData = (state, payload) => {
var _a;
let items = Array.isArray(payload) ? payload : [payload];
let newList = state.data || [];
let prevPageItems = 0;
for (let item of items) {
let idx = findOrderedIndex(newList, state.primaryKey, item, state.currentPage, state.orderDir, state.orderParam);
if (idx >= 0) {
newList.splice(idx, 0, item);
newList = newList.slice(0, state.pageSize);
}
else {
prevPageItems++;
}
}
let result = Object.assign(Object.assign({}, state), { data: [...newList], totalRecords: state.totalRecords + items.length });
if (prevPageItems) {
result.neg_dirty = ((_a = result.neg_dirty) !== null && _a !== void 0 ? _a : 0) + prevPageItems;
}
if (result.neg_dirty === state.pageSize) {
result.neg_dirty = 0;
result.currentPage++;
}
return result;
};
const setData = (state, payload) => {
let items = Array.isArray(payload) ? payload : [payload];
return Object.assign(Object.assign({}, state), { data: items });
};
function updateInPath(obj, path, value, updateFunc) {
let state = immutable_1.Map(obj);
let new_state = state.updateIn(path, (prop) => updateFunc(prop, value));
return new_state.toObject();
}
const mergeValue = (state, payload) => (Object.assign(Object.assign({}, state), payload));
const updateDataInPath = (state, { id, path, value, updaterFunc, }) => state.data
? state.data.map((item) => item[state.primaryKey] === id
? updateInPath(item, path, value, updaterFunc !== null && updaterFunc !== void 0 ? updaterFunc : ((props, v) => {
props = {};
Object.assign(props, v);
}))
: item)
: [];
const setOrder = (state, payload) => (Object.assign(Object.assign({}, state), { hasOrder: (payload.param !== state.orderParam || payload.dir !== state.orderDir), orderParam: payload.param, orderDir: payload.dir }));
const resetOrder = (state) => (Object.assign(Object.assign({}, state), { hasOrder: false, orderParam: state.primaryKey, orderDir: DEFAULT_ORDER_DIR }));
const setPage = (state, payload) => (Object.assign(Object.assign({}, state), { currentPage: payload }));
const setPageSize = (state, payload) => (Object.assign(Object.assign({}, state), { pageSize: payload }));
const actionsMap = {
"set-order": setOrder,
"reset-order": resetOrder,
"remove-data": removeData,
"update-data": updateData,
"add-data": addData,
"set-data": setData,
"set-page": setPage,
"set-page-size": setPageSize,
"merge-value": mergeValue,
"update-data-in-path": updateDataInPath,
};
exports.actionImpl = ((actions) => (state, action) => {
let _action = actions[action.type];
if (_action) {
return _action(state, action.payload);
}
return state;
})(actionsMap);