@hhgtech/hhg-components
Version:
Hello Health Group common components
90 lines (87 loc) • 4.47 kB
JavaScript
import React__default, { createContext, useContext, useRef } from 'react';
import { deleteCookie } from './miscCookieHelper.js';
import { u as useSSOV2Store, I as IS_SSOV2_ENABLED } from './store-5b85d43f.js';
import { T as TogetherComponentGlobalContext, k as getUrlToSignUp } from './utils-5e9c89a7.js';
import { B as BEARER_TOKEN_COOKIE } from './constants-367949ba.js';
import './tslib.es6-00ab44b2.js';
import 'zustand';
import './index-d535cfb0.js';
import './constantsIsProduction.js';
import './normalizeLink-c3e1dac7.js';
import './constantsDomainLocales.js';
import './Locale-dc1237b9.js';
import './index-8c40504a.js';
import 'date-fns/locale';
import './index-fe4471f4.js';
import './constantsSite.js';
import './constantsRiskScreener.js';
import 'react-phone-number-input';
import './index-dac15b93.js';
import 'slugify';
import 'string-format';
import './togetherApiPaths.js';
const TogetherAuthConfigContext = createContext({});
const TogetherAuthConfigContextProvider = ({ children, defaultParams, }) => {
return (React__default.createElement(TogetherAuthConfigContext.Provider, { value: {
defaultParams,
} }, children));
};
const useTogetherAuthRequiredAction = (propsParams) => {
const { triggerLogin } = useSSOV2Store();
const { data: { userInfo, env: { togetherBasePath }, }, } = useContext(TogetherComponentGlobalContext);
const { defaultParams } = useContext(TogetherAuthConfigContext) || {};
const loggedInRef = useRef(!!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id));
loggedInRef.current = !!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id);
const isFirstInteractRef = useRef(userInfo &&
typeof userInfo.communities_interacted === 'number' &&
userInfo.communities_interacted === 0);
isFirstInteractRef.current =
userInfo &&
typeof userInfo.communities_interacted === 'number' &&
userInfo.communities_interacted === 0;
const authActionWrapper = (func, extraParams) => {
const params = Object.assign(Object.assign(Object.assign({}, defaultParams), propsParams), extraParams);
if (!loggedInRef.current || isFirstInteractRef.current) {
if (!loggedInRef.current) {
deleteCookie(BEARER_TOKEN_COOKIE);
}
if (params === null || params === void 0 ? void 0 : params._joinCommunityIds) {
localStorage.setItem('to_join_community_ids', JSON.stringify(Array.from(new Set((JSON.parse(localStorage.getItem('to_join_community_ids') || '[]') || [])
.concat(params._joinCommunityIds)
.filter(Boolean)))));
params === null || params === void 0 ? true : delete params._joinCommunityIds;
}
if (params === null || params === void 0 ? void 0 : params._joinTopicIds) {
localStorage.setItem('to_join_topic_ids', JSON.stringify(Array.from(new Set((JSON.parse(localStorage.getItem('to_join_topic_ids') || '[]') || [])
.concat(params._joinTopicIds)
.filter(Boolean)))));
params === null || params === void 0 ? true : delete params._joinTopicIds;
}
const currentLocation = (params === null || params === void 0 ? void 0 : params._location) || window.location.href;
params === null || params === void 0 ? true : delete params._location;
const redirectUrl = getUrlToSignUp(currentLocation, params, togetherBasePath);
if (IS_SSOV2_ENABLED()) {
const returnUrlParam = redirectUrl.slice(redirectUrl.indexOf('returnUrl=') + 10);
if (returnUrlParam) {
const originalRedirectUrl = decodeURIComponent(returnUrlParam);
triggerLogin({
returnUrl: originalRedirectUrl,
source: 'communities',
});
}
}
else {
window.location.href = redirectUrl;
}
return null;
}
else {
return func && func();
}
};
return {
authActionWrapper,
params: Object.assign(Object.assign({}, defaultParams), propsParams),
};
};
export { TogetherAuthConfigContext, TogetherAuthConfigContextProvider, useTogetherAuthRequiredAction };