my-iubenda-package
Version:
Un pacchetto per gestire il consenso dei cookie con Iubenda in React Native.
89 lines (77 loc) • 2.45 kB
JavaScript
import { NativeModules, Alert } from 'react-native';
class IubendaService {
constructor(siteId, cookiePolicyId) {
this.siteId = siteId;
this.cookiePolicyId = cookiePolicyId;
this.IubendaBridge = NativeModules.IubendaBridge;
if (!this.IubendaBridge) {
Alert.alert('Errore', 'Il modulo IubendaBridge non è disponibile!');
}
}
getConsentStatus() {
if (!this.IubendaBridge) {
console.warn('IubendaBridge non disponibile.');
return Promise.reject('IubendaBridge non disponibile.');
}
return this.IubendaBridge.getConsentStatus()
.then(data => {
console.log('Dati del consenso:', data);
return data;
})
.catch(error => {
console.error('Errore durante il recupero dello stato del consenso:', error);
throw error;
});
}
// hasConsentForPurpose(purposeId) {
// if (!this.IubendaBridge) {
// console.warn('IubendaBridge non disponibile.');
// return Promise.reject('IubendaBridge non disponibile.');
// }
// return this.IubendaBridge.hasConsentForPurpose(purposeId)
// .then(hasConsent => {
// console.log(`Consenso per lo scopo ${purposeId}: ${hasConsent}`);
// return hasConsent;
// })
// .catch(error => {
// console.error(`Errore durante la verifica del consenso per lo scopo ${purposeId}:`, error);
// throw error;
// });
// }
initialize() {
if (!this.IubendaBridge) {
return;
}
try {
const config = {
gdprEnabled: true,
forceConsent: true,
siteId: this.siteId,
googleAds: true,
applyStyles: true,
cookiePolicyId: this.cookiePolicyId,
acceptIfDismissed: true,
preventDismissWhenLoaded: true,
jsonContent: '{"enableTcf": true, "tcfVersion": 2, "perPurposeConsent": true}',
skipNoticeWhenOffline: true,
};
this.IubendaBridge.initialize(config);
console.log('Successo', `Iubenda è stato inizializzato correttamente!`);
return true;
} catch (error) {
console.log('Errore', `Impossibile inizializzare Iubenda: ${error}`);
return false;
}
}
askConsent() {
if (!this.IubendaBridge) {
return;
}
try {
this.IubendaBridge.askConsent();
} catch (error) {
Alert.alert('Errore', 'Impossibile chiedere il consenso: ${error}');
}
}
}
export default IubendaService;