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-

105 lines (104 loc) 4.61 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = __importDefault(require("react")); const ComplexIdActions_1 = __importDefault(require("../../actions/ComplexIdActions")); const ComplexIdReducer_1 = __importDefault(require("../../reducers/ComplexIdReducer")); const _1 = require("."); const __1 = require("../../../"); const react_redux_1 = require("react-redux"); class ComplexIdRestModel extends _1.RestModel { constructor(model, key, optSchema, url) { super(model.model, key, url, { trailingSlash: model.trailingSlash, headers: model.headers, getItems: model.getItems, getMetaData: model.getMetaData, itemStructure: model.itemStructure }); this._actions = new ComplexIdActions_1.default(this); this._reducer = new ComplexIdReducer_1.default(this); } useInvalidate(opts, id) { return () => { const dispatch = __1.useDispatch(); dispatch(this._actions.invalidateById(opts, id)); }; } useInvalidateAll() { return () => { const dispatch = __1.useDispatch(); dispatch(this._actions.invalidateAll()); }; } useFetchByIdIfNeeded(opts, id) { const dispatch = __1.useDispatch(); react_1.default.useEffect(() => { dispatch(this._actions.fetchByIdIfNeeded(opts, id)); }); } useGetById(opts, id) { const [result, setResult] = react_1.default.useState({ error: null, initialized: false, invalidated: true, loading: false, item: null }); const redirect = this.useInvalidate(opts, id); const state = __1.useSelector(state => { const resultState = { item: this._reducer.getById(state, id), initialized: this._reducer.isIdInitialized(state, id), loading: this._reducer.isIdFetching(state, id), invalidated: this._reducer.isIdInvalidated(state, id), error: this._reducer.getIdError(state, id), }; return resultState; }); this.useFetchByIdIfNeeded(opts, id); react_1.default.useEffect(() => { if (!react_redux_1.shallowEqual(state, result)) setResult(state); }); return Object.assign(Object.assign({}, result), { redirect }); } useFetchByIdPopulatedIfNeeded(opts, id) { const dispatch = __1.useDispatch(); react_1.default.useEffect(() => { dispatch(this._actions.fetchByIdPopulatedIfNeeded(opts, id)); }); } useGetByIdPopulated(opts, id) { const redirect = this.useInvalidate(opts, id); const [result, setResult] = react_1.default.useState({ error: null, populated: false, initialized: false, invalidated: true, loading: false, item: null }); const state = __1.useSelector(state => { const resultState = { item: this._reducer.getByIdPopulated(state, id), loading: this._reducer.isIdFetching(state, id), initialized: this._reducer.isIdInitialized(state, id), populated: this._reducer.isIdPopulated(state, id), invalidated: this._reducer.isIdInvalidated(state, id), error: this._reducer.getIdError(state, id), }; return resultState; }); this.useFetchByIdPopulatedIfNeeded(opts, id); react_1.default.useEffect(() => { const { item: newItem } = state, newOthers = __rest(state, ["item"]); const { item } = state, others = __rest(state, ["item"]); if (!react_redux_1.shallowEqual(newOthers, others) || (newItem && !item) || state.populated !== result.populated) { setResult(state); } }); return Object.assign(Object.assign({}, result), { redirect }); } } exports.default = ComplexIdRestModel;