Skip to content

Commit

Permalink
convert cookieValues to a state variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Sneh1999 committed Jan 5, 2024
1 parent 1aefa93 commit 529571a
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/cookie-manager/src/CookieContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Cookies, { CookieAttributes } from 'js-cookie';
import React, { createContext, useCallback, useContext, useEffect } from 'react';
import React, { createContext, useCallback, useContext, useEffect, useState } from 'react';

import {
ADVERTISING_SHARING_ALLOWED,
Expand Down Expand Up @@ -39,7 +39,7 @@ export const CookieProvider = ({ children }: Props) => {
const { config, region, shadowMode, log, onPreferenceChange } = useTrackingManager();

const POLL_INTERVAL = 500;
let cookieValues: Record<string, any> = getAllCookies();
const [cookieValues, setCookieValues] = useState(() => getAllCookies());
let trackingPreference: TrackingPreference;
let adTrackingPreference: AdTrackingPreference;

Expand All @@ -63,12 +63,12 @@ export const CookieProvider = ({ children }: Props) => {
const checkCookies = () => {
const currentCookie = getAllCookies();
if (!areRecordsEqual(cookieValues, currentCookie)) {
cookieValues = currentCookie;
trackingPreference = getTrackingPreference(cookieValues, region, config);
adTrackingPreference = getAdTrackingPreference(cookieValues);
setCookieValues(currentCookie);
trackingPreference = getTrackingPreference(currentCookie, region, config);
adTrackingPreference = getAdTrackingPreference(currentCookie);
setGTMVariables(trackingPreference, adTrackingPreference);
const cookiesToRemove: Array<string> = [];
Object.keys(cookieValues).forEach((c) => {
Object.keys(currentCookie).forEach((c) => {
const trackerInfo = getTrackerInfo(c, config);
if (REQUIRED_COOKIE_MANAGER_COOKIES.includes(c)) {
return;
Expand Down

0 comments on commit 529571a

Please sign in to comment.