@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
101 lines (100 loc) • 5.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stateWidgetInitializer = exports.dataWidgetReducer = exports.actionWidgetTypes = void 0;
const utils_1 = require("@selfcommunity/utils");
/**
* @hidden
* We have complex state logic that involves multiple sub-values,
* so useReducer is preferable to useState.
* Define all possible auth action types label
* Use this to export actions and dispatch an action
*/
exports.actionWidgetTypes = {
LOADING_NEXT: '_loading_next',
LOADING_PREVIOUS: '_loading_previous',
LOAD_NEXT_SUCCESS: '_load_next_success',
LOAD_PREVIOUS_SUCCESS: '_load_previous_success',
LOAD_NEXT_FAILURE: '_load_next_failure',
LOAD_PREVIOUS_FAILURE: '_load_previous_failure',
SET_RESULTS: '_set_results',
SET_VISIBLE_ITEMS: '_set_visible_items',
INITIALIZE: '_initialize',
RESET: '_reset'
};
/**
* Manage tools state reducer
* @param state
* @param action
*/
function dataWidgetReducer(state, action) {
var _a;
let _state = Object.assign({}, state);
switch (action.type) {
case exports.actionWidgetTypes.LOADING_NEXT:
_state = Object.assign(Object.assign({}, state), { isLoadingNext: true, errorLoadNext: null });
break;
case exports.actionWidgetTypes.LOADING_PREVIOUS:
_state = Object.assign(Object.assign({}, state), { isLoadingPrevious: true, errorLoadPrevious: null });
break;
case exports.actionWidgetTypes.LOAD_NEXT_SUCCESS:
_state = Object.assign(Object.assign({}, state), { isLoadingNext: false, count: action.payload.count, next: action.payload.next ? action.payload.next : null, results: [...state.results, ...action.payload.results], errorLoadNext: null, initialized: ((_a = action.payload) === null || _a === void 0 ? void 0 : _a.initialized) || true });
break;
case exports.actionWidgetTypes.LOAD_PREVIOUS_SUCCESS:
_state = Object.assign(Object.assign({}, state), { isLoadingPrevious: false, count: action.payload.count, previous: action.payload.previous ? action.payload.previous : null, results: [...action.payload.results, ...state.results], errorPreviousNext: null });
break;
case exports.actionWidgetTypes.SET_RESULTS:
_state = Object.assign(Object.assign(Object.assign({}, state), { results: [...action.payload.results] }), (action.payload.count ? { count: action.payload.count } : {}));
break;
case exports.actionWidgetTypes.SET_VISIBLE_ITEMS:
_state = Object.assign(Object.assign({}, state), { visibleItems: action.payload.visibleItems });
break;
case exports.actionWidgetTypes.LOAD_NEXT_FAILURE:
_state = Object.assign(Object.assign({}, state), { isLoadingNext: false, errorLoadNext: action.payload.error });
break;
case exports.actionWidgetTypes.LOAD_PREVIOUS_FAILURE:
_state = Object.assign(Object.assign({}, state), { isLoadingNext: false, errorLoadPrevious: action.payload.error });
break;
case exports.actionWidgetTypes.RESET:
_state = { isLoadingNext: false, next: null, results: [], count: 0, errorLoadNext: null, initialized: false };
break;
case exports.actionWidgetTypes.INITIALIZE:
_state = {
cacheKey: action.payload.cacheKey ? action.payload.cacheKey : null,
count: action.payload.count ? action.payload.count : 0,
results: action.payload.results ? action.payload.results : [],
next: action.payload.next ? action.payload.next : null,
previous: action.payload.previous ? action.payload.previous : null,
isLoadingNext: action.payload.isLoadingNext ? action.payload.isLoadingNext : false,
isLoadingPrevious: action.payload.isLoadingPrevious ? action.payload.isLoadingPrevious : false,
visibleItems: action.payload.visibleItems ? action.payload.visibleItems : null,
initialized: false
};
break;
}
utils_1.LRUCache.set(_state.cacheKey, _state);
return _state;
}
exports.dataWidgetReducer = dataWidgetReducer;
/**
* Define initial tools state
* @param data
*/
function stateWidgetInitializer(data) {
let _initState = {
cacheKey: data.cacheKey ? data.cacheKey : null,
count: data.count ? data.count : 0,
results: data.results ? data.results : [],
next: data.next ? data.next : null,
previous: data.previous ? data.previous : null,
isLoadingNext: data.isLoadingNext ? data.isLoadingNext : false,
isLoadingPrevious: data.isLoadingPrevious ? data.isLoadingPrevious : false,
visibleItems: data.visibleItems ? data.visibleItems : null,
initialized: false
};
if (_initState.cacheKey && utils_1.LRUCache.hasKey(_initState.cacheKey) && data.cacheStrategy !== utils_1.CacheStrategies.NETWORK_ONLY) {
const _cachedStateData = utils_1.LRUCache.get(_initState.cacheKey);
return Object.assign(Object.assign({}, _initState), _cachedStateData);
}
return _initState;
}
exports.stateWidgetInitializer = stateWidgetInitializer;