Skip to content

Commit

Permalink
Support Web Storage API & consentObject fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Jersyfi committed Jan 11, 2023
1 parent bbde1ec commit 21b262f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 52 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

All notable changes to `react-cookify` will be documented in this file.

## v0.4.4-beta.1 - In progress...
## v0.4.6-beta.1 - 2022-01-11

* Added Web Storage API Support for the local storage
* Added `store` in `options` Object to choose between HTML cookies and Web Storage API
* Modified `getMemoryData()` and `setMemoryData()` to match the `store` in `options`
* Modified the initialization code in a useEffect function to only render it once
* Modified `actionCheckbox()`, `actionAccept()`, `actionAccept()` & `actionNecessary()` to fix issue of [#6](https://github.com/Jersyfi/react-cookify/issues/6)

## v0.3.4-beta.1 - 2022-01-08

Expand Down
71 changes: 20 additions & 51 deletions src/context/useCookify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ export const useCookify = (options: CookifyOptionsType) => {
* @returns {ConsentObjectType | boolean}
*/
const getMemoryData = (): ConsentObjectType | boolean => {
const cookie = _this.jscookie.get(_this.name)

if (typeof cookie !== 'undefined') {
return JSON.parse(atob(cookie))
}

return false

/*switch (_this.store) {
switch (_this.store) {
case 'storage': {
let storage

Expand All @@ -72,7 +64,7 @@ export const useCookify = (options: CookifyOptionsType) => {

return false
}
}*/
}
}

/**
Expand All @@ -81,12 +73,7 @@ export const useCookify = (options: CookifyOptionsType) => {
* @param {ConsentObjectType} tempConsentObject
*/
const setMemoryData = (tempConsentObject: ConsentObjectType = consentObject) => {
_this.jscookie.set(
_this.name,
btoa(JSON.stringify(tempConsentObject))
)

/*const tempConsentObjectString: string = btoa(JSON.stringify(tempConsentObject))
const tempConsentObjectString: string = btoa(JSON.stringify(tempConsentObject))

switch (_this.store) {
case 'storage': {
Expand All @@ -100,7 +87,7 @@ export const useCookify = (options: CookifyOptionsType) => {
_this.jscookie.set(_this.name, tempConsentObjectString)
break;
}
}*/
}
}

/**
Expand All @@ -125,15 +112,11 @@ export const useCookify = (options: CookifyOptionsType) => {
* @param {string} type
*/
const actionCheckbox = (type: string) => {
const newConsentObjectViewed: boolean = consentObject.viewed
const newConsentObjectData: ConsentObjectDataType = consentObject.data
const newConsentObject = consentObject

newConsentObjectData[type] = !consentObject.data[type]
newConsentObject.data[type] = !consentObject.data[type]

handleConsentObjectChange({
viewed: newConsentObjectViewed,
data: newConsentObjectData
})
handleConsentObjectChange(newConsentObject)

if (_this.saveWithChange === true) {
setMemoryData()
Expand All @@ -145,32 +128,30 @@ export const useCookify = (options: CookifyOptionsType) => {
* Event on action accept click
*/
const actionAccept = () => {
const newConsentObjectData = consentObject.data
const newConsentObject = consentObject

newConsentObject['viewed'] = true

afterSomeActions({
viewed: true,
data: newConsentObjectData
})
afterSomeActions(newConsentObject)
}

/**
* Event action accept only necessary click
*/
const actionNecessary = () => {
const newConsentObjectData = consentObject.data
const newConsentObject = consentObject

for (const type in newConsentObjectData) {
for (const type in newConsentObject.data) {
if (type == _this.typeDefault) {
newConsentObjectData[type] = true
newConsentObject.data[type] = true
} else {
newConsentObjectData[type] = false
newConsentObject.data[type] = false
}
}

afterSomeActions({
viewed: true,
data: newConsentObjectData
})
newConsentObject['viewed'] = true

afterSomeActions(newConsentObject)
}

/**
Expand All @@ -183,16 +164,9 @@ export const useCookify = (options: CookifyOptionsType) => {
newConsentObject.data[type] = true
}

// Testing the error
/*afterSomeActions({
viewed: true,
data: newConsentObjectData
})*/
newConsentObject['viewed'] = true

handleConsentObjectChange(newConsentObject)
setMemoryData()
handleConsentDisplayedChange(false)
handleConsentTrackingChange()
afterSomeActions(newConsentObject)
}

/* Create state object for temporary memory data storage */
Expand All @@ -203,12 +177,7 @@ export const useCookify = (options: CookifyOptionsType) => {
const [consentTracking, setConsentTracking] = useState(0)

const handleConsentObjectChange = (newConsentObject: ConsentObjectType) => {
//console.log('handleConsentObjectChange: ', newConsentObject)
//something not working

setConsentObject(newConsentObject)

console.log('after -> handleConsentObjectChange: ', consentObject)
}

const handleConsentDisplayedChange = (newConsentDisplayed: boolean) => {
Expand Down

0 comments on commit 21b262f

Please sign in to comment.