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 11, 2024
1 parent ab63bbe commit 9ada92d
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 15 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: 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 {
onOtelChange: (consent: boolean) => void;
}

export function AnalyticsConsent(props: ConsentProps): ReactElement {
const acceptAnalytics = (): void => {
props.onOtelChange(true);
};
const denyAnalytics = (): void => {
props.onOtelChange(false);
};
return (
<div>
<button onClick={acceptAnalytics}>Accept</button>
<button onClick={denyAnalytics}>Deny</button>
</div>
);
}
37 changes: 37 additions & 0 deletions src/components/App/AppLoggedIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { Path } from "types/path";
import FontContext, { ProjectFonts } from "utilities/fontContext";
import { getProjCss } from "utilities/fontCssUtilities";
import { routerPath } from "utilities/pathUtilities";
import { AnalyticsConsent } from "components/AnalyticsConsent/AnalyticsConsent";

Check warning on line 20 in src/components/App/AppLoggedIn.tsx

View workflow job for this annotation

GitHub Actions / lint_build (20)

`components/AnalyticsConsent/AnalyticsConsent` import should occur before import of `components/App/DatePickersLocalizationProvider`
import { updateUser } from "backend";

Check warning on line 21 in src/components/App/AppLoggedIn.tsx

View workflow job for this annotation

GitHub Actions / lint_build (20)

`backend` import should occur before import of `components/App/DatePickersLocalizationProvider`
import { getCurrentUser } from "backend/localStorage";

Check warning on line 22 in src/components/App/AppLoggedIn.tsx

View workflow job for this annotation

GitHub Actions / lint_build (20)

`backend/localStorage` import should occur before import of `components/App/DatePickersLocalizationProvider`

const BaseGoalScreen = loadable(
() => import("goals/DefaultGoal/BaseGoalScreen")
Expand All @@ -30,6 +33,8 @@ const UserSettings = loadable(
() => import("components/UserSettings/UserSettings")
);

// export function UserConsent(): ReactElement;

export default function AppWithBar(): ReactElement {
const proj = useAppSelector(
(state: StoreState) => state.currentProjectState.project,
Expand All @@ -48,6 +53,29 @@ export default function AppWithBar(): ReactElement {

const [styleOverrides, setStyleOverrides] = useState<string>();

const [answeredConsent, setAnsweredConsent] = useState(
getCurrentUser()?.answeredConsent
);

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

// useEffect(() => {
// setAnsweredConsent(true);
// }, [answeredConsent]);

// const handleConsent = (otelConsent: boolean): void => {
// const user = getCurrentUser();
// console.log(user);
// console.log(otelConsent);
// };

useEffect(() => {
updateLangFromUser();
}, []);
Expand Down Expand Up @@ -80,9 +108,18 @@ export default function AppWithBar(): ReactElement {
<DatePickersLocalizationProvider>
<SignalRHub />
<AppBar />
<div>HI THERE</div>
<FontContext.Provider value={projFonts}>
{/* <div>HI</div> */}
<ThemeProvider theme={overrideThemeFont}>
{/* <div>HI</div> */}
<CssBaseline />
<div>HEY</div>
{answeredConsent ? null : (
<div>
<AnalyticsConsent onOtelChange={handleConsent}></AnalyticsConsent>
</div>
)}
<Routes>
<Route path={routerPath(Path.DataEntry)} element={<DataEntry />} />
<Route path={routerPath(Path.Goals)} element={<GoalTimeline />} />
Expand Down
4 changes: 2 additions & 2 deletions src/components/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RouterProvider } from "react-router-dom";

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

/**
Expand All @@ -13,7 +13,7 @@ export default function App(): ReactElement {
return (
<div className="App">
<Suspense fallback={<div />}>
<CookieConsent />
{/* <CookieConsent /> */}
<AnnouncementBanner />
<UpperRightToastContainer />
<RouterProvider router={router} />
Expand Down
37 changes: 24 additions & 13 deletions src/components/UserSettings/UserSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ 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 { StoreState } from "rootRedux/types";
import theme from "types/theme";
import { uiWritingSystems } from "types/writingSystem";

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

const analyticsConsent = useAppSelector(
(state: StoreState) => state.analyticsState.consent
);
// 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 +81,13 @@ export function UserSettings(props: {
return unchanged || !(await isEmailTaken(unicodeEmail));
}

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

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

const disabled =
name === props.user.name &&
Expand Down Expand Up @@ -293,19 +297,26 @@ 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>
{showOptions ? (
<div>
<AnalyticsConsent
onOtelChange={handleConsentChange}
></AnalyticsConsent>
</div>
) : 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 9ada92d

Please sign in to comment.