|
| 1 | +**Offline Sync Provider - README** |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +Offline Sync Provider is a JavaScript module designed to handle API requests in web applications with offline capabilities. It offers a robust solution to synchronize data with the server even when the device is offline and automatically retries failed requests upon reconnection. This module utilizes `axios` for making API requests and `localforage` for offline storage, ensuring data integrity and smooth synchronization. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +$ npm install --save offline-sync-handler |
| 13 | +$ yarn add offline-sync-handler |
| 14 | +``` |
| 15 | + |
| 16 | +## Demo |
| 17 | + You can find the working demo [here](https://offline-handler-demo.vercel.app) |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Offline Sync Provider |
| 22 | + |
| 23 | +Wrap your application with the `OfflineSyncProvider` component to enable offline sync and manage data synchronization. |
| 24 | + |
| 25 | +```jsx |
| 26 | +import { OfflineSyncProvider } from 'offline-sync-handler'; |
| 27 | +const App = () => { |
| 28 | + // Your application components and logic |
| 29 | +}; |
| 30 | + |
| 31 | +const rootElement = document.getElementById('root'); |
| 32 | +ReactDOM.render( |
| 33 | + <OfflineSyncProvider> |
| 34 | + <App /> |
| 35 | + </OfflineSyncProvider>, |
| 36 | + rootElement |
| 37 | +); |
| 38 | +``` |
| 39 | + |
| 40 | +### Sending API Requests |
| 41 | + |
| 42 | +You can use the `sendRequest` function to send API requests. It handles retries in case of failure due to an unstable internet connection. |
| 43 | +Refer to the `axios-create` documentation for available config options. |
| 44 | + |
| 45 | +```javascript |
| 46 | +import { useOfflineSyncContext } from from 'offline-sync-handler'; |
| 47 | + const { sendRequest } = useOfflineSyncContext(); |
| 48 | + const config = { |
| 49 | + method: 'POST', |
| 50 | + url: 'https://api.example.com/data', |
| 51 | + data : { name : 'John Doe', email : '[email protected]' }, |
| 52 | + }; |
| 53 | + |
| 54 | +try { |
| 55 | + const response = await sendRequest(config); |
| 56 | + console.log('API Response:', response); |
| 57 | +} catch (error) { |
| 58 | + console.error('API Request failed:', error.message); |
| 59 | +} |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +### Passing Custom Component to display during Offline |
| 64 | + |
| 65 | +You can pass the custom component to show during offline using the `render` prop of the `OfflineSyncProvider`. |
| 66 | + |
| 67 | +```jsx |
| 68 | + |
| 69 | +import { OfflineSyncProvider } from './offline-sync-provider'; |
| 70 | + |
| 71 | +const App = () => { |
| 72 | + // Your application components and logic |
| 73 | +}; |
| 74 | + |
| 75 | +const rootElement = document.getElementById('root'); |
| 76 | +ReactDOM.render( |
| 77 | + <OfflineSyncProvider |
| 78 | + render={({ isOffline, isOnline }) => { |
| 79 | + return isOnline ? null : <div>I am offline</div>; |
| 80 | + }} |
| 81 | + > |
| 82 | + <App /> |
| 83 | + </OfflineSyncProvider>, |
| 84 | + rootElement |
| 85 | +); |
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +### Track online status change to perform certain operation |
| 90 | + |
| 91 | +You can track online status change using the `onStatusChange` prop of the `OfflineSyncProvider`. |
| 92 | + |
| 93 | +```jsx |
| 94 | +import { OfflineSyncProvider } from './offline-sync-provider'; |
| 95 | + |
| 96 | +const App = () => { |
| 97 | + // Your application components and logic |
| 98 | +}; |
| 99 | + |
| 100 | + |
| 101 | +const rootElement = document.getElementById('root'); |
| 102 | +ReactDOM.render( |
| 103 | + <OfflineSyncProvider |
| 104 | + onStatusChange={(status)=>{ |
| 105 | + console.log({status}) |
| 106 | + }} |
| 107 | + > |
| 108 | + <App /> |
| 109 | + </OfflineSyncProvider>, |
| 110 | + rootElement |
| 111 | +); |
| 112 | +``` |
| 113 | + |
| 114 | +### Customizing Toast Notifications |
| 115 | + |
| 116 | +You can customize toast notifications using the `toastConfig` prop of the `OfflineSyncProvider` component. Refer to the `react-toastify` documentation for available options. |
| 117 | + |
| 118 | +```jsx |
| 119 | +import { OfflineSyncProvider } from './offline-sync-provider'; |
| 120 | + |
| 121 | +const App = () => { |
| 122 | + // Your application components and logic |
| 123 | +}; |
| 124 | + |
| 125 | +const toastConfig = { |
| 126 | + position: 'bottom-left', |
| 127 | + autoClose: 3000, |
| 128 | +}; |
| 129 | + |
| 130 | +const rootElement = document.getElementById('root'); |
| 131 | +ReactDOM.render( |
| 132 | + <OfflineSyncProvider toastConfig={toastConfig}> |
| 133 | + <App /> |
| 134 | + </OfflineSyncProvider>, |
| 135 | + rootElement |
| 136 | +); |
| 137 | +``` |
| 138 | + |
| 139 | +## Roadmaps |
| 140 | + |
| 141 | + * Passing callbacks functions to be triggered on request success/failure. |
| 142 | + |
| 143 | +## License |
| 144 | + |
| 145 | +This project is licensed under the [MIT License](https://opensource.org/licenses/MIT). |
| 146 | + |
| 147 | +## Issues and Contributions |
| 148 | + |
| 149 | +If you encounter any issues or have suggestions for improvement, please [submit an issue](https://github.com/example/offline-sync-provider/issues). Contributions are welcome! Please fork the repository and create a pull request. |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +Thank you for using the Offline Sync Provider module! We hope it simplifies handling API requests and enhances the offline experience for your web application. If you have any questions or need further assistance, feel free to reach out to us. Happy coding! |
0 commit comments