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-

53 lines (52 loc) 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); class ComplexSearchReducer { constructor(restModel, reducer, url) { this.restModel = restModel; this.getById = reducer.getById; 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(opts, queryString) { const gettedUrl = this.url(opts); const url = this.restModel.trailingSlash && !gettedUrl.endsWith('/') ? `${gettedUrl}/` : gettedUrl; return queryString ? `${url}?${queryString}` : url; } get(opts, state, queryString = '') { const result = utils_1.getResult(this.getReducer(state).searchs[this.getUrl(opts, queryString)]); return result ? result.ids.map(id => this.getById(state, id)).filter(f => f !== null) : []; } getPopulated(opts, state, queryString = '') { const items = this.get(opts, state, queryString); return items.map(item => this.restModel.model.schema._convertToPopulated(state, item)); } isInitialized(opts, state, queryString = '') { return utils_1.isInitialized(this.getReducer(state).searchs[this.getUrl(opts, queryString)]); } isPopulated(opts, state, queryString = '') { const items = this.get(opts, state, queryString); return items.map(item => this.restModel.model.schema._isPopulated(state, item)).every(e => e === true); } getError(opts, state, queryString = '') { return utils_1.getError(this.getReducer(state).searchs[this.getUrl(opts, queryString)]); } isFetching(opts, state, queryString = '') { return utils_1.isFetching(this.getReducer(state).searchs[this.getUrl(opts, queryString)]); } isInvalidated(opts, state, queryString = '') { return utils_1.isInvalidated(this.getReducer(state).searchs[this.getUrl(opts, queryString)]); } getMetadata(opts, state, queryString = '') { const result = utils_1.getResult(this.getReducer(state).searchs[this.getUrl(opts, queryString)]); return result && result.metadata ? result.metadata : null; } } exports.default = ComplexSearchReducer;