Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookies fix #159

Merged
merged 6 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
378 changes: 189 additions & 189 deletions docs/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bldrs-ai",
"version": "1.0.0-r268",
"name": "buildrs",
"version": "1.0.0-r270",
"main": "src/index.jsx",
"homepage": "https://github.com/bldrs-ai/Share",
"bugs": {
Expand Down
30 changes: 25 additions & 5 deletions src/Components/AboutControl.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState} from 'react'
import React, {useState, useEffect} from 'react'
import Slider from '@mui/material/Slider'
import Typography from '@mui/material/Typography'
import {makeStyles} from '@mui/styles'
Expand Down Expand Up @@ -69,6 +69,16 @@ function AboutDialog({isDialogDisplayed, setIsDialogDisplayed, installPrefix}) {
*/
function AboutContent({installPrefix}) {
const classes = useStyles()
const [privacySlider, setPrivacySlider] = useState(0)
OlegMoshkovich marked this conversation as resolved.
Show resolved Hide resolved
useEffect(()=>{
if (Privacy.isPrivacySocialEnabled()) {
setPrivacySlider(20)
} else if (Privacy.isPrivacyUsageEnabled()) {
setPrivacySlider(10)
} else {
setPrivacySlider(0)
}
}, [])
const marks = [
{value: 0, label: 'Functional', info: 'Theme, UI state, cookie preference'},
{value: 10, label: 'Usage', info: 'Stats from your use of Bldrs'},
Expand All @@ -77,11 +87,21 @@ function AboutContent({installPrefix}) {
const setPrivacy = (event) => {
debug().log('AboutContent#setPrivacy: ', event.target.value)
switch (event.target.value) {
case 0: Privacy.setUsageAndSocialEnabled(false, false); break
case 10: Privacy.setUsageAndSocialEnabled(true, false); break
case 20: Privacy.setUsageAndSocialEnabled(true, true); break
case 0:
Privacy.setUsageAndSocialEnabled(false, false)
setPrivacySlider(0)
break
case 10:
Privacy.setUsageAndSocialEnabled(true, false)
setPrivacySlider(10)
break
case 20:
Privacy.setUsageAndSocialEnabled(true, true)
setPrivacySlider(20)
break
}
}

return (
<div className={classes.content}>
<Typography
Expand Down Expand Up @@ -116,7 +136,7 @@ function AboutContent({installPrefix}) {
<Slider
onChange={setPrivacy}
marks={marks}
defaultValue={30}
value={privacySlider}
step={10}
min={0}
max={20}
Expand Down
50 changes: 38 additions & 12 deletions src/privacy/Privacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@ export function getCookie({component, name, defaultValue}) {
}


/**
* @param {boolean} isUsageEnabled
* @param {boolean} isSocialEnabled
*/
export function setUsageAndSocialEnabled(isUsageEnabled, isSocialEnabled) {
assertDefined(isUsageEnabled, isSocialEnabled)
debug().log('Privacy#setUsageAndSocialEnabled: ', isUsageEnabled, isSocialEnabled)
setCookieBoolean({component: 'cookies', name: 'usage', value: isUsageEnabled})
setCookieBoolean({component: 'cookies', name: 'social', value: isSocialEnabled})
}


/**
* @param {string} component
* @param {string} name
Expand Down Expand Up @@ -69,3 +57,41 @@ export function setCookieBoolean({component, name, value}) {
assertDefined(component, name, value)
setCookiePrivate(name, value)
}


/**
* @param {boolean} isUsageEnabled
* @param {boolean} isSocialEnabled
*/
export function setUsageAndSocialEnabled(isUsageEnabled, isSocialEnabled) {
assertDefined(isUsageEnabled, isSocialEnabled)
debug().log('Privacy#setUsageAndSocialEnabled: ', isUsageEnabled, isSocialEnabled)
setCookieBoolean({component: 'cookies', name: 'usage', value: isUsageEnabled})
setCookieBoolean({component: 'cookies', name: 'social', value: isSocialEnabled})
}


/**
* @return {boolean} for social privacy level
*/
export function isPrivacySocialEnabled() {
return (
getCookieBoolean({
component: 'privacy',
name: 'social',
defaultValue: true})
)
}


/**
* @return {boolean} for usage privacy level
*/
export function isPrivacyUsageEnabled() {
return (
getCookieBoolean({
component: 'privacy',
name: 'usage',
defaultValue: true})
)
}
35 changes: 35 additions & 0 deletions src/privacy/Privacy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Privacy from './Privacy'


test('getCookie', () => {
Privacy.setCookie({component: 'component', name: 'name', value: 'true'})
const retValue = Privacy.getCookie({
component: 'component',
name: 'name',
defaultValue: 'defaultValue'})
expect(retValue).toBe('true')
})


test('getCookieBoolean', () => {
Privacy.setCookie({component: 'component', name: 'name', value: 'true'})
const retValue = Privacy.getCookieBoolean({
component: 'component',
name: 'name',
defaultValue: 'value'})
expect(retValue).toBe(true)
})


test('isPrivacySocialEnabled', () => {
Privacy.setCookie({component: 'privacy', name: 'social', value: 'true'})
const retValue = Privacy.isPrivacySocialEnabled()
expect(retValue).toBe(true)
})


test('isPrivacyUsageEnabled', () => {
Privacy.setCookie({component: 'privacy', name: 'usage', value: 'true'})
const retValue = Privacy.isPrivacySocialEnabled()
expect(retValue).toBe(true)
})
5 changes: 3 additions & 2 deletions src/privacy/analytics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gtag from '../utils/gtag'
import * as Functional from './functional'
import * as Privacy from './Privacy'
import {assertDefined} from '../utils/assert'


Expand All @@ -20,10 +21,10 @@ export function recordEvent(commandParameters, additionalConfigInfo) {


/**
* @return {boolean} is analytics enabled
* @return {boolean} is social level of privacy enabled
*/
export function isAnalyticsAllowed() {
return Functional.getCookieBoolean('isAnalyticsAllowed', false) // defaultValue
return Privacy.isPrivacySocialEnabled()
}


Expand Down
16 changes: 7 additions & 9 deletions src/privacy/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ export function isCookieSet(name) {
*/
export function getCookie(name, defaultValue) {
assertDefined(name, defaultValue)
const namePrefix = name + '='
const decodedCookie = decodeURIComponent(document.cookie)
const ca = decodedCookie.split(';')
for (let i = 0; i < ca.length; i++) {
let c = ca[i]
while (c.charAt(0) == ' ') {
c = c.substring(1)
}
if (c.indexOf(namePrefix) == 0) {
return c.substring(namePrefix.length, c.length)
const properties = decodedCookie.split(';')
for (let i = 0; i < properties.length; i++) {
const parts = properties[i].trim().split('=')
const propName = parts[0]
const propValue = parts[1]
if (propName == name) {
return propValue
}
}
return defaultValue + ''
Expand Down