v.1.0.0. initial push of license-lib
This commit is contained in:
31
react/LicenseProvider.js
Normal file
31
react/LicenseProvider.js
Normal file
@ -0,0 +1,31 @@
|
||||
// react/LicenseProvider.js
|
||||
|
||||
import React, { createContext, useState, useEffect } from 'react';
|
||||
import { activateLicense } from '../client/validate';
|
||||
import { cacheLicenseData, getCachedLicenseData, isLicenseCacheValid } from '../client/offline';
|
||||
|
||||
export const LicenseContext = createContext();
|
||||
|
||||
export const LicenseProvider = ({ userId, licenseKey, children }) => {
|
||||
const [isLicensed, setIsLicensed] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const checkLicense = async () => {
|
||||
if (navigator.onLine) {
|
||||
const valid = await activateLicense(userId, licenseKey);
|
||||
setIsLicensed(valid);
|
||||
if (valid) cacheLicenseData({ userId, licenseKey });
|
||||
} else {
|
||||
setIsLicensed(isLicenseCacheValid());
|
||||
}
|
||||
};
|
||||
|
||||
checkLicense();
|
||||
}, [userId, licenseKey]);
|
||||
|
||||
return (
|
||||
<LicenseContext.Provider value={{ isLicensed }}>
|
||||
{children}
|
||||
</LicenseContext.Provider>
|
||||
);
|
||||
};
|
8
react/useLicense.js
Normal file
8
react/useLicense.js
Normal file
@ -0,0 +1,8 @@
|
||||
// react/useLicense.js
|
||||
|
||||
import { useContext } from 'react';
|
||||
import { LicenseContext } from './LicenseProvider';
|
||||
|
||||
const useLicense = () => useContext(LicenseContext);
|
||||
|
||||
export default useLicense;
|
Reference in New Issue
Block a user