Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
eastandwestwind committed Sep 19, 2023
1 parent 99913af commit 7bce7a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
17 changes: 7 additions & 10 deletions clients/fides-js/src/fides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ import {
import {
constructFidesRegionString,
debugLog,
experienceIsValid,
experienceIsValidAndHasNotices,
experienceHasNotices,
transformConsentToFidesUserPreference,
validateOptions,
} from "./lib/consent-utils";
Expand Down Expand Up @@ -133,7 +134,7 @@ const automaticallyApplyGPCPreferences = (
fidesRegionString: string | null,
fidesApiUrl: string,
debug: boolean,
effectiveExperience?: PrivacyExperience | null
effectiveExperience?: PrivacyExperience
) => {
if (!effectiveExperience || !effectiveExperience.privacy_notices) {
return;
Expand Down Expand Up @@ -216,7 +217,7 @@ const init = async ({
_Fides.geolocation = geolocation;
_Fides.options = options;
_Fides.initialized = true;
if (experience && Object.keys(experience).length >= 0) {
if (experienceHasNotices(experience)) {
// at this point, pre-fetched experience contains no user consent, so we populate with the Fides cookie
updateExperienceFromCookieConsent(
experience as PrivacyExperience,
Expand Down Expand Up @@ -253,7 +254,7 @@ const init = async ({
`User location could not be obtained. Skipping overlay initialization.`
);
shouldInitOverlay = false;
} else if (!effectiveExperience) {
} else if (!experienceHasNotices(experience)) {
effectiveExperience = await fetchExperience(
fidesRegionString,
options.fidesApiUrl,
Expand All @@ -262,11 +263,7 @@ const init = async ({
);
}

if (
effectiveExperience &&
Object.keys(effectiveExperience).length >= 0 &&
experienceIsValid(effectiveExperience as PrivacyExperience, options)
) {
if (experienceIsValidAndHasNotices(effectiveExperience, options)) {
// Overwrite cookie consent with experience-based consent values
cookie.consent = buildCookieConsentForExperiences(
effectiveExperience as PrivacyExperience,
Expand All @@ -285,7 +282,7 @@ const init = async ({
}
}
if (shouldInitOverlay) {
if (effectiveExperience && Object.keys(effectiveExperience).length >= 0) {
if (experienceHasNotices(effectiveExperience)) {
automaticallyApplyGPCPreferences(
cookie,
fidesRegionString,
Expand Down
39 changes: 22 additions & 17 deletions clients/fides-js/src/lib/consent-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ export const debugLog = (
}
};

/**
* Returns true if privacy experience has notices
*/
export const experienceHasNotices = (
experience: PrivacyExperience | undefined | {}
): boolean => {
if (experience && Object.keys(experience).length >= 0) {
const privacyExperience = experience as PrivacyExperience;
return Boolean(
privacyExperience.privacy_notices &&
privacyExperience.privacy_notices.length > 0
);
}
return false;
};

/**
* Construct user location str to be ingested by Fides API
* Returns null if geolocation cannot be constructed by provided params, e.g. us_ca
Expand Down Expand Up @@ -139,37 +155,26 @@ export const validateOptions = (options: FidesOptions): boolean => {
/**
* Determines whether experience is valid and relevant notices exist within the experience
*/
export const experienceIsValid = (
effectiveExperience: PrivacyExperience | undefined,
export const experienceIsValidAndHasNotices = (
effectiveExperience: PrivacyExperience | undefined | {},
options: FidesOptions
): boolean => {
if (!effectiveExperience) {
debugLog(
options.debug,
`No relevant experience found. Skipping overlay initialization.`
);
return false;
}
if (
!(
effectiveExperience.privacy_notices &&
effectiveExperience.privacy_notices.length > 0
)
) {
if (!experienceHasNotices(effectiveExperience)) {
debugLog(
options.debug,
`No relevant notices in the privacy experience. Skipping overlay initialization.`,
effectiveExperience
`Privacy experience has no notices. Skipping overlay initialization.`
);
return false;
}
// @ts-ignore
if (effectiveExperience.component !== ComponentType.OVERLAY) {
debugLog(
options.debug,
"No experience found with overlay component. Skipping overlay initialization."
);
return false;
}
// @ts-ignore
if (!effectiveExperience.experience_config) {
debugLog(
options.debug,
Expand Down

0 comments on commit 7bce7a5

Please sign in to comment.