@magicbell/core
Version:
Official MagicBell API wrapper
42 lines • 1.29 kB
JavaScript
import PaginatedStore from './PaginatedStore.js';
/**
* A store that keep tracks of pagination as well.
*
* @example
* const store = new RemoteStore();
* store.push(model);
*/
export default class RemoteStore extends PaginatedStore {
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 });
}
}
//# sourceMappingURL=RemoteStore.js.map