UNPKG

@woocommerce/data

Version:
95 lines (94 loc) 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSettingsError = exports.getLastSettingsErrorForGroup = exports.getSetting = exports.isUpdateSettingsRequesting = exports.getSettingsForGroup = exports.getIsDirty = exports.getDirtyKeys = exports.getSettings = exports.getSettingsGroupNames = void 0; /** * Internal dependencies */ const utils_1 = require("../utils"); const getSettingsGroupNames = (state) => { const groupNames = new Set(Object.keys(state).map((resourceName) => { return (0, utils_1.getResourcePrefix)(resourceName); })); return [...groupNames]; }; exports.getSettingsGroupNames = getSettingsGroupNames; const getSettings = (state, group) => { const settings = {}; const settingIds = (state[group] && state[group].data) || []; if (!Array.isArray(settingIds) || settingIds.length === 0) { return settings; } settingIds.forEach((id) => { settings[id] = state[(0, utils_1.getResourceName)(group, id)].data; }); return settings; }; exports.getSettings = getSettings; const getDirtyKeys = (state, group) => { return state[group].dirty || []; }; exports.getDirtyKeys = getDirtyKeys; const getIsDirty = (state, group, keys = []) => { const dirtyMap = (0, exports.getDirtyKeys)(state, group); // if empty array bail if (dirtyMap.length === 0) { return false; } // if at least one of the keys is in the dirty map then the state is dirty // meaning it hasn't been persisted. return keys.some((key) => dirtyMap.includes(key)); }; exports.getIsDirty = getIsDirty; const getSettingsForGroup = (state, group, keys) => { const allSettings = (0, exports.getSettings)(state, group); return keys.reduce((accumulator, key) => { accumulator[key] = allSettings[key] || {}; return accumulator; }, {}); }; exports.getSettingsForGroup = getSettingsForGroup; const isUpdateSettingsRequesting = (state, group) => { return state[group] && Boolean(state[group].isRequesting); }; exports.isUpdateSettingsRequesting = isUpdateSettingsRequesting; /** * Retrieves a setting value from the setting store. * * @param {Object} state State param added by wp.data. * @param {string} group The settings group. * @param {string} name The identifier for the setting. * @param {*} [fallback=false] The value to use as a fallback * if the setting is not in the * state. * @param {Function} [filter=( val ) => val] A callback for filtering the * value before it's returned. * Receives both the found value * (if it exists for the key) and * the provided fallback arg. * * @return {*} The value present in the settings state for the given * name. */ function getSetting(state, group, name, fallback = false, // eslint-disable-next-line @typescript-eslint/no-unused-vars -- _fallback in default filter is unused. filter = (val, _fallback) => val) { const resourceName = (0, utils_1.getResourceName)(group, name); const value = (state[resourceName] && state[resourceName].data) || fallback; return filter(value, fallback); } exports.getSetting = getSetting; const getLastSettingsErrorForGroup = (state, group) => { const settingsIds = state[group].data; if (!Array.isArray(settingsIds) || settingsIds.length === 0) { return state[group].error; } return [...settingsIds].pop().error; }; exports.getLastSettingsErrorForGroup = getLastSettingsErrorForGroup; const getSettingsError = (state, group, id) => { if (!id) { return (state[group] && state[group].error) || false; } return state[(0, utils_1.getResourceName)(group, id)].error || false; }; exports.getSettingsError = getSettingsError;