Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix privacy opt-out button not clickable #3818

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion website/src/bootstrapping/matomo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { History } from 'history';
import { each } from 'lodash';
import { useState, useEffect } from 'react';

import { Tracker } from 'types/vendor/piwik';
import insertScript from 'utils/insertScript';
Expand Down Expand Up @@ -36,7 +37,7 @@ function trackInitialPageView() {
}

// Code mostly adopted from https://github.com/AmazingDreams/vue-matomo
export function initializeMamoto() {
export function initializeMatomo() {
Comment on lines -39 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 24 or 25 of this same file, there's the same mispelling of Matomo, can we fix that as well?

const siteId = '1';
const host = 'https://analytics.nusmods.com';
const scriptSrc = `${host}/piwik.js`;
Expand Down Expand Up @@ -77,3 +78,13 @@ export function trackPageView(history: History) {
}
});
}

export function useMatomo() {
// need to use useState here or the matomo returned will always be undefined
const [matomoCopy, setMatomoCopy] = useState<Tracker | undefined>(undefined);
useEffect(() => {
setMatomoCopy(matomo);
}, [matomo]);

return matomoCopy;
}
Comment on lines +82 to +90
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the following code:

useEffect(() => {
    setMatomoCopy(matomo);
  }, [matomo]);

This effect is only ever called once (on component mount) since the matomo variable exists outside of the React context. This is reflected by the linter as well:

React Hook useEffect has an unnecessary dependency: 'matomo'. Either exclude it or remove the
dependency array. Outer scope values like 'matomo' aren't valid dependencies because mutating
them doesn't re-render the component.

4 changes: 2 additions & 2 deletions website/src/entry/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ReactModal from 'react-modal';

import configureStore from 'bootstrapping/configure-store';
import subscribeOnlineEvents from 'bootstrapping/subscribeOnlineEvents';
import { initializeMamoto } from 'bootstrapping/matomo';
import { initializeMatomo } from 'bootstrapping/matomo';
import registerServiceWorker from 'bootstrapping/service-worker-manager';

import 'styles/main.scss';
Expand All @@ -38,5 +38,5 @@ if (
}

if (NUSMODS_ENV === 'production') {
initializeMamoto();
initializeMatomo();
}
2 changes: 1 addition & 1 deletion website/src/types/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type ExamClashes = { [key: string]: Module[] };
export type OnModifyCell = (lesson: ModifiableLesson, position: ClientRect) => void;
export type OnHoverCell = (hoverLesson: HoverLesson | null) => void;

// Incomplete typing of Mamoto's API. If you need something not here, feel free
// Incomplete typing of Matomo's API. If you need something not here, feel free
// to declare the typing here.

export type TimeSegment = 'Morning' | 'Afternoon' | 'Evening';
Expand Down
36 changes: 20 additions & 16 deletions website/src/views/settings/SettingsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import classnames from 'classnames';
import { isEqual } from 'lodash';

import { ColorSchemePreference, ThemeId } from 'types/settings';
import { Tracker } from 'types/vendor/piwik';
import { ModRegNotificationSettings } from 'types/reducers';
import { State as StoreState } from 'types/state';
import { RegPeriod, SCHEDULE_TYPES, ScheduleType } from 'config';
Expand All @@ -26,7 +25,7 @@ import Title from 'views/components/Title';
import deferComponentRender from 'views/hocs/deferComponentRender';
import Online from 'views/components/Online';
import { supportsCSSVariables } from 'utils/css';
import { withTracker } from 'bootstrapping/matomo';
import { useMatomo } from 'bootstrapping/matomo';
import ExternalLink from 'views/components/ExternalLink';
import Toggle from 'views/components/Toggle';
import ModRegNotification from 'views/components/notfications/ModRegNotification';
Expand Down Expand Up @@ -68,22 +67,26 @@ const SettingsContainer: React.FC<Props> = ({
...props
}) => {
const [allowTracking, setAllowTracking] = useState(true);

const onToggleTracking = useCallback((isTrackingAllowed: boolean) => {
withTracker((tracker: Tracker) => {
if (isTrackingAllowed) {
tracker.forgetUserOptOut();
} else {
tracker.optUserOut();
const matomo = useMatomo();

const onToggleTracking = useCallback(
(isTrackingAllowed: boolean) => {
if (matomo !== undefined) {
if (isTrackingAllowed) {
matomo.forgetUserOptOut();
} else {
matomo.optUserOut();
}
setAllowTracking(!matomo.isUserOptedOut());
}

setAllowTracking(!tracker.isUserOptedOut());
});
}, []);
},
[matomo]);

useEffect(() => {
withTracker((tracker: Tracker) => setAllowTracking(!tracker.isUserOptedOut()));
}, [onToggleTracking]);
if (matomo !== undefined) {
setAllowTracking(!matomo.isUserOptedOut());
}
}, [onToggleTracking, matomo]);

const rounds = getRounds(modRegNotification);

Expand Down Expand Up @@ -263,8 +266,9 @@ const SettingsContainer: React.FC<Props> = ({
<div className="text-right">
<Toggle
labels={['Allow', 'Opt out']}
isOn={allowTracking}
isOn={matomo !== undefined ? allowTracking : false}
onChange={onToggleTracking}
className={matomo === undefined ? 'disabled' : ''}
/>
</div>
)}
Expand Down