beautiful-react-hooks
Version:
A collection of beautiful (and hopefully useful) React hooks to speed-up your components and hooks development
81 lines (80 loc) • 3.66 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { useState, useEffect, useCallback } from 'react';
import noop from './shared/noop';
import isClient from './shared/isClient';
import isDevelopment from './shared/isDevelopment';
import isAPISupported from './shared/isAPISupported';
import createHandlerSetter from './factory/createHandlerSetter';
export var ECookieSameSite;
(function (ECookieSameSite) {
ECookieSameSite["STRICT"] = "strict";
ECookieSameSite["LAX"] = "lax";
ECookieSameSite["NONE"] = "none";
})(ECookieSameSite || (ECookieSameSite = {}));
const useCookie = (key, options) => {
const hookNotSupportedResponse = Object.freeze({
onError: noop,
updateCookie: noop,
deleteCookie: noop,
cookieValue: options === null || options === void 0 ? void 0 : options.defaultValue,
});
if (!isClient) {
if (!isDevelopment) {
// eslint-disable-next-line no-console
console.warn('Please be aware that cookieStore could not be available during SSR');
}
return hookNotSupportedResponse;
}
if (!isAPISupported('cookieStore')) {
// eslint-disable-next-line no-console
console.warn("The current device does not support the 'cookieStore' API, you should avoid using useCookie");
return hookNotSupportedResponse;
}
const [cookieValue, setCookieValue] = useState();
const [onErrorRef, setOnErrorRef] = createHandlerSetter();
const cookieStoreObject = window.cookieStore;
const onError = (err) => {
if (onErrorRef.current) {
onErrorRef.current(err);
}
};
useEffect(() => {
const getInitialValue = () => __awaiter(void 0, void 0, void 0, function* () {
try {
const getFunctionResult = yield cookieStoreObject.get(key);
if (getFunctionResult === null || getFunctionResult === void 0 ? void 0 : getFunctionResult.value) {
return setCookieValue(getFunctionResult.value);
}
yield cookieStoreObject.set(Object.assign({ name: key, value: options === null || options === void 0 ? void 0 : options.defaultValue }, options));
return setCookieValue(options === null || options === void 0 ? void 0 : options.defaultValue);
}
catch (err) {
return onError(err);
}
});
getInitialValue();
}, []);
const updateCookie = useCallback((newValue) => cookieStoreObject
.set(Object.assign({ name: key, value: newValue }, options))
.then(() => setCookieValue(newValue))
.catch(onError), []);
const deleteCookie = useCallback(() => cookieStoreObject
.delete(Object.assign({ name: key }, options))
.then(() => setCookieValue(undefined))
.catch(onError), []);
return Object.freeze({
cookieValue,
updateCookie,
deleteCookie,
onError: setOnErrorRef,
});
};
export default useCookie;