-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): wakelock component added'
- Loading branch information
Alok
committed
Apr 8, 2024
1 parent
f4349f3
commit 1e94802
Showing
2 changed files
with
60 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,68 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { handleError, handleSuccess } from '../services/handlerService'; | ||
|
||
function WakeLock(props = {}) { | ||
const successCb = props.successCb || (() => {}); | ||
const failureCb = props.failureCb || (() => {}); | ||
const successCb = props.successCb || (() => { }); | ||
const failureCb = props.failureCb || (() => { }); | ||
const successMsg = props.successMsg || ''; | ||
const failureMsg = props.failureMsg || {}; | ||
const { children } = props; | ||
|
||
if (WakeLock.isBrowserSupport()) { | ||
// const abort = new AbortController(); | ||
// abortAutoFill(abort, 3); | ||
// navigator.credentials.get({ | ||
// otp: { transport: ['sms'] }, | ||
// signal: abort.signal, | ||
// }).then((otp) => { | ||
// const { code } = otp; | ||
// handleSuccess({ msgType: 'SUCCESSFUL', msg: successMsg, successCb, data: code }); | ||
// }).catch((error) => handleError({ msgType: 'ERROR', msg: error, failureCb })); | ||
} else { | ||
return handleError({ msgType: 'UN_SUPPORTED_FEATURE', msg: failureMsg.unSupported || 'WakeLock is not supporting in your device', failureCb }); | ||
} | ||
} | ||
const wakeLock = async () => { | ||
if (WakeLock.isBrowserSupport()) { | ||
let wakeLocker = null; | ||
wakeLocker = await navigator.wakeLock.request('screen'); | ||
try { | ||
if (wakeLocker) { | ||
handleSuccess({ msgType: 'SUCCESSFUL', msg: successMsg, successCb }); | ||
} else { | ||
return handleError({ | ||
msgType: 'CANCELLED', | ||
msg: failureMsg.cancelled, | ||
failureCb, | ||
}); | ||
} | ||
} catch (error) { | ||
return handleError({ | ||
msgType: 'ERROR', | ||
msg: failureMsg.error || JSON.stringify(error), | ||
failureCb, | ||
}); | ||
} | ||
} else { | ||
return handleError({ | ||
msgType: 'UN_SUPPORTED_FEATURE', | ||
msg: | ||
failureMsg.unSupported || 'WakeLock is not supporting in your device', | ||
failureCb, | ||
}); | ||
} | ||
return true; | ||
}; | ||
|
||
return ( | ||
React.Children.map(children || 'WakeLock', (child) => React.cloneElement(typeof child === 'string' ? <span>{child}</span> : child, { | ||
onClick: wakeLock, | ||
})) | ||
); | ||
} | ||
WakeLock.isBrowserSupport = () => 'wakeLock' in navigator; | ||
|
||
WakeLock.propTypes = { | ||
successCb: PropTypes.func, | ||
failureCb: PropTypes.func, | ||
successMsg: PropTypes.string, | ||
failureMsg: PropTypes.object, | ||
}; | ||
|
||
WakeLock.defaultProps = { | ||
successCb: () => { }, | ||
failureCb: () => { }, | ||
successMsg: 'WakeLock successfully!!', | ||
failureMsg: { | ||
unSupported: 'Your browser does not support the WakeLock fetaure', | ||
error: 'Unable to apply WakeLock', | ||
}, | ||
}; | ||
export default WakeLock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters