From c4306787f826ae62a46f367a4a295b66c998f37e Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com> Date: Sat, 13 Apr 2024 20:35:11 -0400 Subject: [PATCH] Put adult consent logic all in one place --- lemmy-translations | 2 +- src/shared/components/app/app.tsx | 41 +------------ .../components/common/adult-consent-modal.tsx | 59 ++++++++++++++++--- 3 files changed, 56 insertions(+), 46 deletions(-) diff --git a/lemmy-translations b/lemmy-translations index a94ef775..4a2a30f4 160000 --- a/lemmy-translations +++ b/lemmy-translations @@ -1 +1 @@ -Subproject commit a94ef775f3f923067b48c1719cda206dbcf1a9e5 +Subproject commit 4a2a30f4a3778df85e28ceaf541b6c978b321e2a diff --git a/src/shared/components/app/app.tsx b/src/shared/components/app/app.tsx index 64d4b6b5..a9c8b793 100644 --- a/src/shared/components/app/app.tsx +++ b/src/shared/components/app/app.tsx @@ -1,5 +1,5 @@ import { isAnonymousPath, isAuthPath, setIsoData } from "@utils/app"; -import { Component, RefObject, createRef, linkEvent } from "inferno"; +import { Component, createRef, linkEvent } from "inferno"; import { Provider } from "inferno-i18next-dess"; import { Route, Switch } from "inferno-router"; import { IsoDataOptionalSite } from "../../interfaces"; @@ -14,46 +14,14 @@ import "./styles.scss"; import { Theme } from "./theme"; import AnonymousGuard from "../common/anonymous-guard"; import { destroyTippy, setupTippy } from "../../tippy"; -import { adultConsentLocalStorageKey } from "../../config"; import AdultConsentModal from "../common/adult-consent-modal"; -interface AppState { - showAdultConsentModal: boolean; -} - -function handleAdultConsent(i: App) { - localStorage.setItem(adultConsentLocalStorageKey, "true"); - i.setState({ showAdultConsentModal: false }); -} - -function handleAdultConsentGoBack(i: App) { - i.context.router.history.back(); -} - -export class App extends Component { +export class App extends Component { private isoData: IsoDataOptionalSite = setIsoData(this.context); - private readonly mainContentRef: RefObject; + private readonly mainContentRef = createRef(); private readonly rootRef = createRef(); - state: AppState = { - showAdultConsentModal: false, - }; - - constructor(props: any, context: any) { - super(props, context); - this.mainContentRef = createRef(); - } - componentDidMount(): void { - const siteRes = this.isoData.site_res; - - if ( - siteRes?.site_view.site.content_warning && - !(siteRes?.my_user || localStorage.getItem(adultConsentLocalStorageKey)) - ) { - this.setState({ showAdultConsentModal: true }); - } - setupTippy(this.rootRef); } @@ -88,9 +56,6 @@ export class App extends Component { {siteRes?.site_view.site.content_warning && ( )}
diff --git a/src/shared/components/common/adult-consent-modal.tsx b/src/shared/components/common/adult-consent-modal.tsx index 5a70d9e4..e9d8f1ac 100644 --- a/src/shared/components/common/adult-consent-modal.tsx +++ b/src/shared/components/common/adult-consent-modal.tsx @@ -1,5 +1,8 @@ -import { Component, LinkedEvent, RefObject, createRef } from "inferno"; +import { Component, LinkedEvent, createRef, linkEvent } from "inferno"; import { modalMixin } from "../mixins/modal-mixin"; +import { adultConsentLocalStorageKey } from "../../config"; +import { setIsoData } from "@utils/app"; +import { IsoDataOptionalSite } from "../../interfaces"; interface AdultConsentModalProps { contentWarning: string; @@ -9,12 +12,9 @@ interface AdultConsentModalProps { } @modalMixin -export default class AdultConsentModal extends Component< - AdultConsentModalProps, - any -> { - readonly modalDivRef: RefObject = createRef(); - readonly continueButtonRef: RefObject = createRef(); +class AdultConsentModalInner extends Component { + readonly modalDivRef = createRef(); + readonly continueButtonRef = createRef(); render() { const { contentWarning, onContinue, onBack } = this.props; @@ -57,3 +57,48 @@ export default class AdultConsentModal extends Component< this.continueButtonRef.current?.focus(); } } + +interface AdultConsentModalState { + show: boolean; +} + +function handleAdultConsent(i: AdultConsentModal) { + localStorage.setItem(adultConsentLocalStorageKey, "true"); + i.setState({ show: false }); +} + +function handleAdultConsentGoBack(i: AdultConsentModal) { + i.context.router.history.back(); +} + +export default class AdultConsentModal extends Component< + { contentWarning: string }, + AdultConsentModalState +> { + private isoData: IsoDataOptionalSite = setIsoData(this.context); + state: AdultConsentModalState = { + show: false, + }; + + componentDidMount(): void { + const siteRes = this.isoData.site_res; + + if ( + siteRes?.site_view.site.content_warning && + !(siteRes?.my_user || localStorage.getItem(adultConsentLocalStorageKey)) + ) { + this.setState({ show: true }); + } + } + + render() { + return ( + + ); + } +}