0.1.85 • Published 4 months ago

offline-sync-handler-test v0.1.85

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Offline Sync Provider - README

Offline Sync Provider

Description

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.

Installation

$ npm install --save offline-sync-handler
$ yarn add offline-sync-handler

Demo

You can find the working demo here

Usage

Offline Sync Provider

Wrap your application with the OfflineSyncProvider component to enable offline sync and manage data synchronization.

import { OfflineSyncProvider } from 'offline-sync-handler';
const App = () => {
  // Your application components and logic
};

const rootElement = document.getElementById('root');
ReactDOM.render(
  <OfflineSyncProvider>
    <App />
  </OfflineSyncProvider>,
  rootElement
);

Sending API Requests

You can use the sendRequest function to send API requests. It handles retries in case of failure due to an unstable internet connection. Refer to the axios-create documentation for available config options.

import { useOfflineSyncContext } from from 'offline-sync-handler';
  const { sendRequest } = useOfflineSyncContext();
  const config = {
    method: 'POST',
    url: 'https://api.example.com/data',
    data: { name: 'John Doe', email: 'john@example.com' },
  };

try {
  const response = await sendRequest(config);
  console.log('API Response:', response);
} catch (error) {
  console.error('API Request failed:', error.message);
}

Passing Custom Component to display during Offline

You can pass the custom component to show during offline using the render prop of the OfflineSyncProvider.

import { OfflineSyncProvider } from './offline-sync-provider';

const App = () => {
  // Your application components and logic
};

const rootElement = document.getElementById('root');
ReactDOM.render(
  <OfflineSyncProvider
    render={({ isOffline, isOnline }) => {
      return isOnline ? null : <div>I am offline</div>;
    }}
  >
    <App />
  </OfflineSyncProvider>,
  rootElement
);

Track online status change to perform certain operation

You can track online status change using the onStatusChange prop of the OfflineSyncProvider.

import { OfflineSyncProvider } from './offline-sync-provider';

const App = () => {
  // Your application components and logic
};


const rootElement = document.getElementById('root');
ReactDOM.render(
  <OfflineSyncProvider
    onStatusChange={(status)=>{
      console.log({status})
    }}
  >
    <App />
  </OfflineSyncProvider>,
  rootElement
);

Customizing Toast Notifications

You can customize toast notifications using the toastConfig prop of the OfflineSyncProvider component. Refer to the react-toastify documentation for available options.

import { OfflineSyncProvider } from './offline-sync-provider';

const App = () => {
  // Your application components and logic
};

const toastConfig = {
  position: 'bottom-left',
  autoClose: 3000,
};

const rootElement = document.getElementById('root');
ReactDOM.render(
  <OfflineSyncProvider toastConfig={toastConfig}>
    <App />
  </OfflineSyncProvider>,
  rootElement
);

Roadmaps

  • Passing callbacks functions to be triggered on request success/failure.

License

This project is licensed under the MIT License.

Issues and Contributions

If you encounter any issues or have suggestions for improvement, please submit an issue. Contributions are welcome! Please fork the repository and create a pull request.


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.1.85

4 months ago

0.1.80

4 months ago

0.1.81

4 months ago

0.1.82

4 months ago

0.1.83

4 months ago

0.1.84

4 months ago

0.1.79

4 months ago

0.1.54

7 months ago

0.1.55

7 months ago

0.1.56

7 months ago

0.1.57

6 months ago

0.1.58

6 months ago

0.1.59

6 months ago

0.1.74

6 months ago

0.1.75

6 months ago

0.1.76

6 months ago

0.1.77

6 months ago

0.1.78

6 months ago

0.1.70

6 months ago

0.1.71

6 months ago

0.1.72

6 months ago

0.1.73

6 months ago

0.1.63

6 months ago

0.1.64

6 months ago

0.1.65

6 months ago

0.1.67

6 months ago

0.1.68

6 months ago

0.1.69

6 months ago

0.1.60

6 months ago

0.1.61

6 months ago

0.1.62

6 months ago

0.1.53

9 months ago

0.1.52

9 months ago

0.1.51

9 months ago

0.1.50

9 months ago

0.1.48

9 months ago

0.1.47

9 months ago

0.1.46

9 months ago

0.1.45

9 months ago

0.1.44

9 months ago

0.1.43

9 months ago

0.1.42

9 months ago

0.1.41

9 months ago

0.1.40

9 months ago

0.1.39

9 months ago

0.1.38

9 months ago

0.1.37

9 months ago

0.1.36

9 months ago

0.1.35

9 months ago

0.1.34

9 months ago

0.1.33

9 months ago

0.1.32

9 months ago

0.1.31

9 months ago

0.1.30

9 months ago

0.1.29

9 months ago

0.1.28

9 months ago

0.1.26

9 months ago

0.1.24

9 months ago

0.1.23

9 months ago

0.1.22

9 months ago

0.1.21

9 months ago

0.1.20

9 months ago

0.1.19

9 months ago

0.0.1

9 months ago

0.1.18

9 months ago

0.1.17

9 months ago

0.1.16

9 months ago

0.1.15

9 months ago

0.1.14

9 months ago

0.1.13

9 months ago

0.1.12

9 months ago

0.1.11

9 months ago

0.1.10

9 months ago

0.1.9

9 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.2

9 months ago