kwikid-components-react
Version:
KwikID's Component Library in React
99 lines (98 loc) • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _PwaInstaller = require("./PwaInstaller.defaults");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const KwikUIPwaInstaller = _ref => {
let {
render = _PwaInstaller.KWIKUI__PWA_INSTALLER__DEFAULTS.render,
callback = _PwaInstaller.KWIKUI__PWA_INSTALLER__DEFAULTS.callback
} = _ref;
const createStatus = object => {
return {
isInstallAllowed: Object.prototype.hasOwnProperty.call(object, "isInstallAllowed") ? object.isInstallAllowed : false,
isInstallWatingConfirm: Object.prototype.hasOwnProperty.call(object, "isInstallWatingConfirm") ? object.isInstallWatingConfirm : false,
isInstalling: Object.prototype.hasOwnProperty.call(object, "isInstalling") ? object.isInstalling : false,
isInstallCancelled: Object.prototype.hasOwnProperty.call(object, "isInstallCancelled") ? object.isInstallCancelled : false,
isInstallSuccess: Object.prototype.hasOwnProperty.call(object, "isInstallSuccess") ? object.isInstallSuccess : false,
isInstallFailed: Object.prototype.hasOwnProperty.call(object, "isInstallFailed") ? object.isInstallFailed : false
};
};
const [installStatus, setInstallStatus] = _react.default.useState(createStatus({}));
const [installEvent, setInstallEvent] = _react.default.useState(null);
_react.default.useEffect(() => {
if (callback) {
callback(installStatus);
}
}, [installStatus]);
const beforeAppInstallpromptHandler = e => {
e.preventDefault();
if (!installStatus.isInstalling) {
if (!installStatus.isInstallSuccess) {
setInstallEvent(e);
if (!installStatus.isInstallAllowed) {
setInstallStatus(createStatus({
isInstallAllowed: true,
isInstallCancelled: installStatus.isInstallCancelled
}));
}
}
}
};
const appInstalledHandler = e => {
if (!installStatus.isInstallSuccess) {
window.removeEventListener("beforeinstallprompt", beforeAppInstallpromptHandler);
e.preventDefault();
setInstallStatus(createStatus({
isInstallSuccess: true
}));
}
};
_react.default.useEffect(() => {
window.addEventListener("beforeinstallprompt", beforeAppInstallpromptHandler);
window.addEventListener("appinstalled", appInstalledHandler);
return () => {
window.removeEventListener("beforeinstallprompt", beforeAppInstallpromptHandler);
window.removeEventListener("appinstalled", appInstalledHandler);
};
}, []);
const handleOnInstall = () => {
setInstallStatus(createStatus({
isInstallWatingConfirm: true
}));
console.log(installEvent);
installEvent.prompt();
installEvent.userChoice.then(choiceResult => {
if (choiceResult.outcome === "accepted") {
setInstallStatus(createStatus({
isInstalling: true,
isInstallAllowed: false
}));
} else {
setInstallStatus(createStatus({
isInstallCancelled: true,
isInstallAllowed: true
}));
}
}).catch(() => {
setInstallStatus(createStatus({
isInstallFailed: true,
isInstallAllowed: true
}));
});
setInstallEvent(null);
};
if (!installStatus.isInstallAllowed) {
return null;
}
if (!render) {
return null;
}
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, render({
onClick: handleOnInstall
}));
};
var _default = exports.default = KwikUIPwaInstaller;