Skip to content

Commit 5ec54e9

Browse files
authored
Merge pull request #1590 from topcoder-platform/pm-225
PM-225 Fix privacy violation related to cookie
2 parents 5ba2a81 + 8f1339b commit 5ec54e9

File tree

5 files changed

+66
-39
lines changed

5 files changed

+66
-39
lines changed

src/components/ChallengeEditor/ChallengeView/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import ChallengeTotalField from '../ChallengeTotal-Field'
1818
import Loader from '../../Loader'
1919
import AssignedMemberField from '../AssignedMember-Field'
2020
import { getResourceRoleByName } from '../../../util/tc'
21-
import { isBetaMode } from '../../../util/cookie'
2221
import { loadGroupDetails } from '../../../actions/challenges'
2322
import {
2423
REVIEW_TYPES,
@@ -29,6 +28,7 @@ import {
2928
} from '../../../config/constants'
3029
import PhaseInput from '../../PhaseInput'
3130
import CheckpointPrizesField from '../CheckpointPrizes-Field'
31+
import { isBetaMode } from '../../../util/localstorage'
3232

3333
const ChallengeView = ({
3434
projectDetail,

src/components/ChallengeEditor/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ import Tooltip from '../Tooltip'
6969
import CancelDropDown from './Cancel-Dropdown'
7070
import UseSchedulingAPIField from './UseSchedulingAPIField'
7171

72-
import { isBetaMode } from '../../util/cookie'
7372
import MilestoneField from './Milestone-Field'
7473
import DiscussionField from './Discussion-Field'
7574
import CheckpointPrizesField from './CheckpointPrizes-Field'
7675
import { canChangeDuration } from '../../util/phase'
76+
import { isBetaMode } from '../../util/localstorage'
7777

7878
const theme = {
7979
container: styles.modalContainer

src/routes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import { saveToken } from './actions/auth'
1717
import { loadChallengeDetails } from './actions/challenges'
1818
import { connect } from 'react-redux'
1919
import { checkAllowedRoles, checkOnlyReadOnlyRoles, checkReadOnlyRoles } from './util/tc'
20-
import { setCookie, removeCookie, isBetaMode } from './util/cookie'
2120
import IdleTimer from 'react-idle-timer'
2221
import modalStyles from './styles/modal.module.scss'
2322
import ConfirmationModal from './components/Modal/ConfirmationModal'
2423
import Users from './containers/Users'
24+
import { isBetaMode, removeFromLocalStorage, saveToLocalStorage } from './util/localstorage'
2525

2626
const { ACCOUNTS_APP_LOGIN_URL, IDLE_TIMEOUT_MINUTES, IDLE_TIMEOUT_GRACE_MINUTES, COMMUNITY_APP_URL } = process.env
2727

@@ -105,9 +105,9 @@ class Routes extends React.Component {
105105
const params = new URLSearchParams(search)
106106
if (!_.isEmpty(params.get('beta'))) {
107107
if (params.get('beta') === 'true' && !isBetaMode()) {
108-
setCookie(BETA_MODE_COOKIE_TAG, 'true')
108+
saveToLocalStorage(BETA_MODE_COOKIE_TAG, 'true')
109109
} else if (params.get('beta') === 'false' && isBetaMode()) {
110-
removeCookie(BETA_MODE_COOKIE_TAG)
110+
removeFromLocalStorage(BETA_MODE_COOKIE_TAG)
111111
}
112112
this.props.history.push(this.props.location.pathname)
113113
}

src/util/cookie.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/util/localstorage.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { BETA_MODE_COOKIE_TAG } from '../config/constants'
2+
3+
/**
4+
* Save an item to localStorage.
5+
* @param {string} key - The key under which the data will be stored.
6+
* @param {any} value - The data to store (will be stringified).
7+
*/
8+
export function saveToLocalStorage (key, value) {
9+
if (!key || typeof key !== 'string') {
10+
throw new Error('Key must be a valid string.')
11+
}
12+
13+
try {
14+
const jsonValue = JSON.stringify(value)
15+
window.localStorage.setItem(key, jsonValue)
16+
} catch (error) {
17+
console.error('Failed to save to localStorage:', error)
18+
}
19+
}
20+
21+
/**
22+
* Get an item from localStorage.
23+
* @param {string} key - The key under which the data is stored.
24+
* @returns {any} - The parsed data from localStorage, or null if not found.
25+
*/
26+
export function getFromLocalStorage (key) {
27+
if (!key || typeof key !== 'string') {
28+
throw new Error('Key must be a valid string.')
29+
}
30+
31+
try {
32+
const jsonValue = window.localStorage.getItem(key)
33+
return jsonValue ? JSON.parse(jsonValue) : null
34+
} catch (error) {
35+
console.error('Failed to retrieve from localStorage:', error)
36+
return null
37+
}
38+
}
39+
40+
/**
41+
* Remove an item from localStorage.
42+
* @param {string} key - The key of the item to remove.
43+
*/
44+
export function removeFromLocalStorage (key) {
45+
if (!key || typeof key !== 'string') {
46+
throw new Error('Key must be a valid string.')
47+
}
48+
49+
try {
50+
window.localStorage.removeItem(key)
51+
} catch (error) {
52+
console.error('Failed to remove from localStorage:', error)
53+
}
54+
}
55+
56+
/**
57+
* A function that checks whether beta mode is enabled or not
58+
*/
59+
export function isBetaMode () {
60+
return getFromLocalStorage(BETA_MODE_COOKIE_TAG) === 'true'
61+
}

0 commit comments

Comments
 (0)