use-react-native-navigation
Version:
A utility library for a easier use of react-native-navigation library.
223 lines • 8.77 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var react_native_navigation_1 = require("react-native-navigation");
var mobx_1 = require("mobx");
var NavigationStore = /** @class */ (function () {
function NavigationStore() {
var _this = this;
this.status = {
updating: false,
currentComponentId: null,
previousComponentId: null,
previousStackComponentIds: [],
commandType: null,
};
this.handleNavigationError = function (e) {
_this.updateNavigationStatus({
updating: false,
});
throw new Error(e);
};
}
NavigationStore.prototype.updateNavigationStatus = function (status) {
this.status = __assign(__assign({}, this.status), status);
};
/**
* A wrapper for Navigation.setRoot.
*/
NavigationStore.prototype.setRoot = function (layout) {
this.updateNavigationStatus({
updating: true,
commandType: 'SET_ROOT',
});
return react_native_navigation_1.Navigation.setRoot(layout).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.setStackRoot.
*/
NavigationStore.prototype.setStackRoot = function (toId, layout) {
this.updateNavigationStatus({
updating: true,
commandType: 'SET_NEW_STACK_ROOT',
});
return react_native_navigation_1.Navigation.setStackRoot(toId, layout).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.push.
*/
NavigationStore.prototype.push = function (toId, layout) {
this.updateNavigationStatus({
updating: true,
commandType: 'PUSH',
});
return react_native_navigation_1.Navigation.push(toId, layout).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.pop.
*/
NavigationStore.prototype.pop = function (onId, mergeOptions) {
this.updateNavigationStatus({
updating: true,
commandType: 'POP',
});
return react_native_navigation_1.Navigation.pop(onId, mergeOptions).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.popTo.
*/
NavigationStore.prototype.popTo = function (toId, mergeOptions) {
this.updateNavigationStatus({
updating: true,
commandType: 'POP_TO',
});
return react_native_navigation_1.Navigation.popTo(toId, mergeOptions).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.popToRoot.
*/
NavigationStore.prototype.popToRoot = function (onId, mergeOptions) {
this.updateNavigationStatus({
updating: true,
commandType: 'POP_TO_ROOT',
});
return react_native_navigation_1.Navigation.popToRoot(onId, mergeOptions).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.showModal.
*/
NavigationStore.prototype.showModal = function (layout) {
this.updateNavigationStatus({
updating: true,
commandType: 'SHOW_MODAL',
});
return react_native_navigation_1.Navigation.showModal(layout).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.dismissModal.
*/
NavigationStore.prototype.dismissModal = function (onId, mergeOptions) {
var _a = this.onPreviousStackComponentShown(this.status.previousStackComponentIds), lastStackComponentId = _a.lastStackComponentId, updatedStackList = _a.updatedStackList;
this.updateNavigationStatus({
currentComponentId: lastStackComponentId,
previousStackComponentIds: updatedStackList,
previousComponentId: null,
updating: true,
commandType: 'MANUAL_DISMISS_MODAL',
});
return react_native_navigation_1.Navigation.dismissModal(onId, mergeOptions).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.dismissAllModals.
*/
NavigationStore.prototype.dismissAllModals = function (mergeOptions) {
var firstStackComponentId = this.onAllStackComponentsDismissed(this.status.previousStackComponentIds);
this.updateNavigationStatus({
currentComponentId: firstStackComponentId,
previousStackComponentIds: [],
previousComponentId: null,
updating: true,
commandType: 'MANUAL_DISMISS_ALL_MODALS',
});
return react_native_navigation_1.Navigation.dismissAllModals(mergeOptions).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.showOverlay.
*/
NavigationStore.prototype.showOverlay = function (layout) {
this.updateNavigationStatus({
updating: true,
commandType: 'SHOW_OVERLAY',
});
return react_native_navigation_1.Navigation.showOverlay(layout).catch(this.handleNavigationError);
};
/**
* A wrapper for Navigation.dismissOverlay.
*/
NavigationStore.prototype.dismissOverlay = function (onId) {
this.updateNavigationStatus({
updating: true,
commandType: 'DISMISS_OVERLAY',
});
return react_native_navigation_1.Navigation.dismissOverlay(onId).catch(this.handleNavigationError);
};
NavigationStore.prototype.onPreviousStackComponentShown = function (stackList) {
if (stackList.length === 0) {
throw new Error('There is no previous stack. 323900');
}
var lastStackComponentId = stackList[stackList.length - 1];
var updatedStackList = stackList.slice(0, -1);
return {
lastStackComponentId: lastStackComponentId,
updatedStackList: updatedStackList,
};
};
NavigationStore.prototype.onAllStackComponentsDismissed = function (stackList) {
if (stackList.length === 0) {
throw new Error('There is no previous stack. 120392');
}
return stackList[0];
};
__decorate([
mobx_1.observable
], NavigationStore.prototype, "status", void 0);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "updateNavigationStatus", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "setRoot", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "setStackRoot", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "push", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "pop", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "popTo", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "popToRoot", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "showModal", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "dismissModal", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "dismissAllModals", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "showOverlay", null);
__decorate([
mobx_1.action.bound
], NavigationStore.prototype, "dismissOverlay", null);
return NavigationStore;
}());
exports.NavigationStore = NavigationStore;
exports.navigationStore = new NavigationStore();
exports.NavigationStoreContext = react_1.createContext(exports.navigationStore);
exports.useNavigationStore = function () { return react_1.useContext(exports.NavigationStoreContext); };
//# sourceMappingURL=navigation.store.js.map