UNPKG

@rest-api/react-models

Version:

[![npm version](https://img.shields.io/npm/v/@rest-api/react-models)](https://www.npmjs.com/package/@rest-api/react-models) [![codecov](https://codecov.io/gh/hector7/rest-api-react-models/branch/master/graph/badge.svg)](https://codecov.io/gh/hector7/rest-

55 lines (54 loc) 2.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); class BasicSearchReducer { constructor(restModel, url) { this.restModel = restModel; this.url = url; } getReducer(state) { const res = Object.assign({ searchs: {}, derived: {}, ids: {} }, state[this.restModel.model.name]); return Object.assign({}, res, { ids: Object.assign({}, res.ids, { [this.restModel._id]: Object.assign({}, res.ids[this.restModel._id]) }) }); } getUrl(queryString) { const gettedUrl = this.url({}); const realUrl = this.restModel.trailingSlash && !gettedUrl.endsWith('/') ? `${gettedUrl}/` : gettedUrl; return queryString ? `${realUrl}?${queryString}` : realUrl; } get(state, queryString = '') { const result = utils_1.getResult(this.getReducer(state).searchs[this.getUrl(queryString)]); return result ? result.ids.map(id => this.restModel._reducer.getById(state, id)).filter(f => f !== null) : []; } get getById() { return this.restModel._reducer.getById; } getPopulated(state, queryString = '') { const result = utils_1.getResult(this.getReducer(state).searchs[this.getUrl(queryString)]); return result ? Object.assign([], result.ids).map(id => this.restModel._reducer.getByIdPopulated(state, id)).filter(f => f !== null) : []; } isPopulated(state, queryString = '') { const items = this.get(state, queryString); return items.map(item => this.restModel.model.schema._isPopulated(state, item)).every(e => e === true); } isInitialized(state, queryString = '') { return utils_1.isInitialized(this.getReducer(state).searchs[this.getUrl(queryString)]); } getError(state, queryString = '') { return utils_1.getError(this.getReducer(state).searchs[this.getUrl(queryString)]); } isFetching(state, queryString = '') { return utils_1.isFetching(this.getReducer(state).searchs[this.getUrl(queryString)]); } isInvalidated(state, queryString = '') { return utils_1.isInvalidated(this.getReducer(state).searchs[this.getUrl(queryString)]); } getMetadata(state, queryString = '') { const result = utils_1.getResult(this.getReducer(state).searchs[this.getUrl(queryString)]); return result && result.metadata ? result.metadata : null; } } exports.default = BasicSearchReducer;