Skip to content

Commit

Permalink
track consent without state - functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
andracc committed Dec 13, 2024
1 parent ab63bbe commit b792db7
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 17 deletions.
7 changes: 7 additions & 0 deletions Backend/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public class User
[BsonElement("otelConsent")]
public bool OtelConsent { get; set; }

[BsonElement("answeredConsent")]
public bool AnsweredConsent { get; set; }

[BsonElement("uiLang")]
public string UILang { get; set; }

Expand Down Expand Up @@ -101,6 +104,7 @@ public User()
Password = "";
Username = "";
OtelConsent = false;
AnsweredConsent = false;
UILang = "";
GlossSuggestion = AutocompleteSetting.On;
Token = "";
Expand All @@ -124,6 +128,7 @@ public User Clone()
Password = Password,
Username = Username,
OtelConsent = OtelConsent,
AnsweredConsent = AnsweredConsent,
UILang = UILang,
GlossSuggestion = GlossSuggestion,
Token = Token,
Expand All @@ -147,6 +152,7 @@ public bool ContentEquals(User other)
other.Password.Equals(Password, StringComparison.Ordinal) &&
other.Username.Equals(Username, StringComparison.Ordinal) &&
other.OtelConsent == OtelConsent &&
other.AnsweredConsent == AnsweredConsent &&
other.UILang.Equals(UILang, StringComparison.Ordinal) &&
other.GlossSuggestion.Equals(GlossSuggestion) &&
other.Token.Equals(Token, StringComparison.Ordinal) &&
Expand Down Expand Up @@ -185,6 +191,7 @@ public override int GetHashCode()
hash.Add(Password);
hash.Add(Username);
hash.Add(OtelConsent);
hash.Add(AnsweredConsent);
hash.Add(UILang);
hash.Add(GlossSuggestion);
hash.Add(Token);
Expand Down
1 change: 0 additions & 1 deletion Backend/Otel/OtelKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public override async void OnEnd(Activity data)
var consentString = data?.GetBaggageItem("otelConsentBaggage");
data?.AddTag("otelConsent", consentString);
var consent = bool.TryParse(consentString, out bool value) ? value : false;
// Note: A bool value also would have worked for SetTag
if (consent)
{
var uriPath = (string?)data?.GetTagItem("url.full");
Expand Down
1 change: 1 addition & 0 deletions Backend/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public async Task<ResultOfUpdate> Update(string userId, User user, bool updateIs
.Set(x => x.Agreement, user.Agreement)
.Set(x => x.Username, user.Username)
.Set(x => x.OtelConsent, user.OtelConsent)
.Set(x => x.AnsweredConsent, user.AnsweredConsent)
.Set(x => x.UILang, user.UILang)
.Set(x => x.GlossSuggestion, user.GlossSuggestion);

Expand Down
6 changes: 6 additions & 0 deletions src/api/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export interface User {
* @memberof User
*/
otelConsent?: boolean;
/**
*
* @type {boolean}
* @memberof User
*/
answeredConsent?: boolean;
/**
*
* @type {string}
Expand Down
20 changes: 20 additions & 0 deletions src/components/AnalyticsConsent/AnalyticsConsent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ReactElement } from "react";

interface ConsentProps {
onChangeConsent: (consentVal: boolean) => void;
}

export function AnalyticsConsent(props: ConsentProps): ReactElement {
const acceptAnalytics = (): void => {
props.onChangeConsent(true);
};
const rejectAnalytics = (): void => {
props.onChangeConsent(false);
};
return (
<div>
<button onClick={acceptAnalytics}>Accept</button>
<button onClick={rejectAnalytics}>Reject</button>
</div>
);
}
18 changes: 18 additions & 0 deletions src/components/App/AppLoggedIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Theme, ThemeProvider, createTheme } from "@mui/material/styles";
import { ReactElement, useEffect, useMemo, useState } from "react";
import { Route, Routes } from "react-router-dom";

import { updateUser } from "backend";
import { getCurrentUser } from "backend/localStorage";
import { AnalyticsConsent } from "components/AnalyticsConsent/AnalyticsConsent";
import DatePickersLocalizationProvider from "components/App/DatePickersLocalizationProvider";
import SignalRHub from "components/App/SignalRHub";
import AppBar from "components/AppBar/AppBarComponent";
Expand Down Expand Up @@ -47,6 +50,18 @@ export default function AppWithBar(): ReactElement {
const projFonts = useMemo(() => new ProjectFonts(proj), [proj]);

const [styleOverrides, setStyleOverrides] = useState<string>();
const [answeredConsent, setAnsweredConsent] = useState(
getCurrentUser()?.answeredConsent
);

async function handleConsentChange(otelConsent: boolean): Promise<void> {
await updateUser({
...getCurrentUser()!,
otelConsent,
answeredConsent: true,
});
setAnsweredConsent(true);
}

useEffect(() => {
updateLangFromUser();
Expand Down Expand Up @@ -83,6 +98,9 @@ export default function AppWithBar(): ReactElement {
<FontContext.Provider value={projFonts}>
<ThemeProvider theme={overrideThemeFont}>
<CssBaseline />
{answeredConsent ? null : (
<AnalyticsConsent onChangeConsent={handleConsentChange}></AnalyticsConsent>
)}
<Routes>
<Route path={routerPath(Path.DataEntry)} element={<DataEntry />} />
<Route path={routerPath(Path.Goals)} element={<GoalTimeline />} />
Expand Down
2 changes: 0 additions & 2 deletions src/components/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { RouterProvider } from "react-router-dom";

import AnnouncementBanner from "components/AnnouncementBanner";
import UpperRightToastContainer from "components/Toast/UpperRightToastContainer";
import CookieConsent from "cookies/CookieConsent";
import router from "router/browserRouter";

/**
Expand All @@ -13,7 +12,6 @@ export default function App(): ReactElement {
return (
<div className="App">
<Suspense fallback={<div />}>
<CookieConsent />
<AnnouncementBanner />
<UpperRightToastContainer />
<RouterProvider router={router} />
Expand Down
32 changes: 18 additions & 14 deletions src/components/UserSettings/UserSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ import {
Typography,
} from "@mui/material";
import { enqueueSnackbar } from "notistack";
import { FormEvent, Fragment, ReactElement, useEffect, useState } from "react";
import { FormEvent, Fragment, ReactElement, useState } from "react";
import { useTranslation } from "react-i18next";
import { show } from "vanilla-cookieconsent";

import { AutocompleteSetting, User } from "api/models";
import { isEmailTaken, updateUser } from "backend";
import { getAvatar, getCurrentUser } from "backend/localStorage";
import { AnalyticsConsent } from "components/AnalyticsConsent/AnalyticsConsent";
import { asyncLoadSemanticDomains } from "components/Project/ProjectActions";
import ClickableAvatar from "components/UserSettings/ClickableAvatar";
import { updateLangFromUser } from "i18n";
import { useAppDispatch, useAppSelector } from "rootRedux/hooks";
import { StoreState } from "rootRedux/types";
import { useAppDispatch } from "rootRedux/hooks";
import theme from "types/theme";
import { uiWritingSystems } from "types/writingSystem";

Expand Down Expand Up @@ -58,14 +57,10 @@ export function UserSettings(props: {
}): ReactElement {
const dispatch = useAppDispatch();

const analyticsConsent = useAppSelector(
(state: StoreState) => state.analyticsState.consent
);

const [name, setName] = useState(props.user.name);
const [phone, setPhone] = useState(props.user.phone);
const [email, setEmail] = useState(props.user.email);
const [otelConsent, setOtelConsent] = useState(analyticsConsent);
const [otelConsent, setOtelConsent] = useState(props.user.otelConsent);
const [uiLang, setUiLang] = useState(props.user.uiLang ?? "");
const [glossSuggestion, setGlossSuggestion] = useState(
props.user.glossSuggestion
Expand All @@ -81,9 +76,13 @@ export function UserSettings(props: {
return unchanged || !(await isEmailTaken(unicodeEmail));
}

useEffect(() => {
setOtelConsent(analyticsConsent);
}, [analyticsConsent]);
const [displayConsent, setDisplayConsent] = useState(false);
const show = (): void => setDisplayConsent(true);

const handleConsentChange = (consentVal: boolean): void => {
setOtelConsent(consentVal);
setDisplayConsent(false);
};

const disabled =
name === props.user.name &&
Expand Down Expand Up @@ -293,19 +292,24 @@ export function UserSettings(props: {
<Grid item>
<Typography>
{t(
analyticsConsent
otelConsent
? "userSettings.analyticsConsent.consentYes"
: "userSettings.analyticsConsent.consentNo"
)}
</Typography>
<Button
data-testid={UserSettingsIds.ButtonChangeConsent}
id={UserSettingsIds.ButtonChangeConsent}
onClick={() => show(true)}
onClick={show}
variant="outlined"
>
{t("userSettings.analyticsConsent.button")}
</Button>
{displayConsent ? (
<AnalyticsConsent
onChangeConsent={handleConsentChange}
></AnalyticsConsent>
) : null}
</Grid>
</Grid>

Expand Down
1 change: 1 addition & 0 deletions src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function newUser(name = "", username = "", password = ""): User {
glossSuggestion: AutocompleteSetting.On,
token: "",
isAdmin: false,
answeredConsent: false,
};
}

Expand Down

0 comments on commit b792db7

Please sign in to comment.