@magicbell/core
Version:
Official MagicBell API wrapper
46 lines • 1.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const PaginatedStore_js_1 = tslib_1.__importDefault(require("./PaginatedStore.js"));
/**
* A store that keep tracks of pagination as well.
*
* @example
* const store = new RemoteStore();
* store.push(model);
*/
class RemoteStore extends PaginatedStore_js_1.default {
xhrFetchState = 'idle';
/**
* Fetch items from the API server. The pagination data is also
* updated. By default the array of items is not reset.
*
* @param queryParams Parameters to send to the API.
* @param options.reset Reset the store.
*/
async fetch(queryParams, options = { reset: false }) {
const resetStore = options.reset || queryParams?.page === 1;
if (resetStore)
this.xhrFetchState = 'pending';
try {
const json = await this.repo.findBy(queryParams);
if (resetStore)
this.reset();
this.set(json);
this.xhrFetchState = 'success';
}
catch (error) {
this.xhrFetchState = 'failure';
}
}
/**
* Fetch the next page of items.
*
* @param queryParams Parameters to send to the API.
*/
fetchNextPage(queryParams = {}) {
return this.fetch({ ...queryParams, page: this.currentPage + 1 });
}
}
exports.default = RemoteStore;
//# sourceMappingURL=RemoteStore.js.map