UNPKG

@mongez/react-atom

Version:
111 lines (109 loc) 1.94 kB
import { atom } from "./react-atom.mjs"; //#region ../@mongez/react-atom/src/helpers.ts /** * Create a boolean atom */ function openAtom(key, defaultOpened = false) { return atom({ key, default: defaultOpened, actions: { toggle() { this.update(!this.currentValue); }, open() { this.update(true); }, close() { this.update(false); }, useOpened() { return this.useState()[0]; } } }); } /** * Create a loading atom */ function loadingAtom(key, defaultLoading = false) { return atom({ key, default: defaultLoading, actions: { startLoading() { this.update(true); }, stopLoading() { this.update(false); }, toggleLoading() { this.update(!this.currentValue); } } }); } /** * Create a fetching atom */ function fetchingAtom(key, defaultValue = null, defaultFetching = true) { return atom({ key, actions: { startLoading() { this.change("isLoading", true); }, stopLoading() { this.change("isLoading", false); }, useLoading() { return this.use("isLoading"); }, useData() { return this.use("data"); }, useError() { return this.use("error"); }, usePagination() { return this.use("pagination"); }, success(data, pagination) { this.merge({ isLoading: false, data, pagination }); }, append(data) { const newData = [...this.value.data, ...data]; this.merge({ isLoading: false, data: newData }); }, prepend(data) { const newData = [...data, ...this.value.data]; this.merge({ isLoading: false, data: newData }); }, failed(error) { this.merge({ isLoading: false, error }); } }, default: { isLoading: defaultFetching, data: defaultValue, error: void 0, pagination: void 0 } }); } //#endregion export { fetchingAtom, loadingAtom, openAtom }; //# sourceMappingURL=helpers.mjs.map