60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import * as SecureStore from 'expo-secure-store';
|
|
import { api } from './api';
|
|
|
|
const CONSENT_KEY = 'consents';
|
|
|
|
export interface Consents {
|
|
essential: boolean;
|
|
analytics: boolean;
|
|
preferences: boolean;
|
|
health_data: boolean;
|
|
}
|
|
|
|
const DEFAULT_CONSENTS: Consents = {
|
|
essential: true,
|
|
analytics: false,
|
|
preferences: false,
|
|
health_data: false,
|
|
};
|
|
|
|
export async function getLocalConsents(): Promise<Consents> {
|
|
try {
|
|
const stored = await SecureStore.getItemAsync(CONSENT_KEY);
|
|
if (stored) return { ...DEFAULT_CONSENTS, ...JSON.parse(stored), essential: true };
|
|
} catch {}
|
|
return { ...DEFAULT_CONSENTS };
|
|
}
|
|
|
|
export async function setLocalConsents(consents: Consents): Promise<void> {
|
|
try { await SecureStore.setItemAsync(CONSENT_KEY, JSON.stringify(consents)); } catch {}
|
|
}
|
|
|
|
export async function hasConsentChoice(): Promise<boolean> {
|
|
try { return (await SecureStore.getItemAsync(CONSENT_KEY)) !== null; } catch { return false; }
|
|
}
|
|
|
|
export async function fetchServerConsents(): Promise<Consents | null> {
|
|
try {
|
|
const res = await api.get('/consents');
|
|
await setLocalConsents(res.data);
|
|
return res.data;
|
|
} catch { return null; }
|
|
}
|
|
|
|
export async function saveConsents(consents: Consents): Promise<Consents> {
|
|
const toSave = { ...consents, essential: true };
|
|
await setLocalConsents(toSave);
|
|
try {
|
|
const res = await api.put('/consents', {
|
|
categories: { analytics: toSave.analytics, preferences: toSave.preferences, health_data: toSave.health_data },
|
|
});
|
|
await setLocalConsents(res.data);
|
|
return res.data;
|
|
} catch { return toSave; }
|
|
}
|
|
|
|
export async function hasConsent(category: keyof Consents): Promise<boolean> {
|
|
const consents = await getLocalConsents();
|
|
return consents[category] === true;
|
|
}
|