-
-
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.
Merge pull request #27 from opensrc0/main
All the features work as Components as well as feature and provide support of default/multi export
- Loading branch information
Showing
39 changed files
with
807 additions
and
558 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
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
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
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
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
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,5 +1,2 @@ | ||
import LocateMe from './LocateMe'; | ||
|
||
export { LocateMe }; | ||
|
||
export default LocateMe; | ||
export * from './LocateMe'; | ||
export { default } from './LocateMe'; |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
handleSuccess, | ||
handleError, | ||
handleLoading, | ||
} from '../services/handlerService'; | ||
import Wrapper from '../Wrapper/Wrapper'; | ||
|
||
const failureMsgDefault = { | ||
unSupported: 'NetworkConnection is not supporting in your device', | ||
error: 'Unable to fetch details from NetworkConnection', | ||
}; | ||
|
||
const isBrowserSupport = () => globalThis?.navigator?.onLine || false; | ||
|
||
const networkConnection = ({ | ||
successCb = () => {}, | ||
failureCb = () => {}, | ||
loadingCb = () => {}, | ||
successMsg = 'NetworkConnection details fetch Successfully!', | ||
failureMsg: failureMsgProps = { ...failureMsgDefault }, | ||
} = {}) => { | ||
const failureMsg = { ...failureMsgDefault, ...failureMsgProps }; | ||
|
||
const init = () => { | ||
if (isBrowserSupport() === true || isBrowserSupport() === false) { | ||
handleLoading({ loadingCb }); | ||
|
||
handleSuccess({ | ||
msgType: 'SUCCESSFUL', | ||
msg: successMsg, | ||
successCb, | ||
data: { | ||
online: isBrowserSupport(), | ||
connectionDetail: globalThis?.navigator?.connection, | ||
}, | ||
}); | ||
} else { | ||
return handleError({ | ||
msgType: 'UN_SUPPORTED_FEATURE', | ||
msg: failureMsg.unSupported, | ||
failureCb, | ||
}); | ||
} | ||
return true; | ||
}; | ||
|
||
init(); | ||
}; | ||
|
||
function NetworkConnection({ | ||
children, | ||
successCb, | ||
failureCb, | ||
loadingCb, | ||
successMsg, | ||
failureMsg, | ||
}) { | ||
const [isOnline, setIsOnline] = useState(isBrowserSupport()); | ||
|
||
const onlineOfflineHandler = () => { | ||
setIsOnline(isBrowserSupport()); | ||
networkConnection(successCb, failureCb, loadingCb, successMsg, failureMsg); | ||
}; | ||
|
||
const networkChangeHandler = () => { | ||
if (!isBrowserSupport()) return; | ||
|
||
networkConnection(successCb, failureCb, loadingCb, successMsg, failureMsg); | ||
}; | ||
|
||
useEffect(() => { | ||
networkConnection(successCb, failureCb, loadingCb, successMsg, failureMsg); | ||
|
||
globalThis.addEventListener('online', onlineOfflineHandler); | ||
globalThis.addEventListener('offline', onlineOfflineHandler); | ||
globalThis?.navigator.connection.addEventListener('change', networkChangeHandler); | ||
|
||
return () => { | ||
globalThis.removeEventListener('online', onlineOfflineHandler); | ||
globalThis.removeEventListener('offline', onlineOfflineHandler); | ||
globalThis.navigator.connection.removeEventListener('change', networkChangeHandler); | ||
}; | ||
}, []); | ||
|
||
return React.Children.map(children || 'NetworkConnection', (child) => React.cloneElement( | ||
typeof child === 'string' ? <span>{child}</span> : child, | ||
{ | ||
isOnline, | ||
}, | ||
)); | ||
} | ||
|
||
NetworkConnection.propTypes = { | ||
showForever: PropTypes.bool, | ||
successCb: PropTypes.func, | ||
failureCb: PropTypes.func, | ||
loadingCb: PropTypes.func, | ||
successMsg: PropTypes.string, | ||
failureMsg: PropTypes.object, | ||
}; | ||
|
||
const WNetworkConnection = Wrapper(NetworkConnection, isBrowserSupport); | ||
|
||
export { networkConnection, WNetworkConnection as NetworkConnection }; | ||
|
||
export default WNetworkConnection; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react'; | ||
|
||
function Offline({ children, isOnline }) { | ||
return !isOnline && React.Children.map(children || 'Online', (child) => React.cloneElement(typeof child === 'string' ? <span>{child}</span> : child)); | ||
} | ||
|
||
export default Offline; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react'; | ||
|
||
function Online({ children, isOnline }) { | ||
return isOnline && React.Children.map(children || 'Online', (child) => React.cloneElement(typeof child === 'string' ? <span>{child}</span> : child)); | ||
} | ||
|
||
export default Online; |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
# Network Connection | ||
|
||
A Network Connection Component returns online status and also information about the system's connection in terms of general connection type (e.g., 'Wi-Fi, 'cellular', etc.). Which can be used to select high-definition content or low-definition content based on the user's connection. | ||
|
||
|
||
## Usage/Examples | ||
|
||
```javascript | ||
import { NetworkConnection, Online, Offline} from 'fe-pilot/NetworkConnection'; | ||
|
||
const success = (response) => { | ||
console.log(response, " success response") | ||
} | ||
|
||
const failure = (response) => { | ||
console.log(response, " failure response") | ||
} | ||
|
||
return ( | ||
<NetworkConnection successCb={success} failureCb={failure}> | ||
<Online>Online</Online> | ||
<Offline> | ||
<div>Pass custom Html to be displayed when appeared offline</div> | ||
</Offline> | ||
</NetworkConnection> | ||
); | ||
} | ||
``` | ||
|
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { NetworkConnection } from './NetworkConnection'; | ||
import Online from './Online'; | ||
import Offline from './Offline'; | ||
|
||
export { NetworkConnection, Online, Offline }; | ||
|
||
export default NetworkConnection; |
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
Oops, something went wrong.