Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: remove Fider service from some components (#816)

Guilherme Oenningfa1082b8f319
diff --git a/public/components/ErrorBoundary.tsx b/public/components/ErrorBoundary.tsx
index 329a94b3f..8c7b2e60e 100644
--- a/public/components/ErrorBoundary.tsx
+++ b/public/components/ErrorBoundary.tsx
@@ -1,5 +1,6 @@
import React from "react";
import { ErrorPage } from "@fider/pages/Error/Error.page";
+import { FiderContext } from "@fider/services";
interface ErrorBoundaryProps {
onError?: (err: Error) => void;
@@ -36,7 +37,11 @@ export class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoun
const { error, errorInfo } = this.state;
if (error && errorInfo) {
- return <ErrorPage error={error} errorInfo={errorInfo} />;
+ return (
+ <FiderContext.Consumer>
+ {fider => <ErrorPage error={error} errorInfo={errorInfo} showDetails={!fider.isProduction()} />}
+ </FiderContext.Consumer>
+ );
} else {
return this.props.children;
}
diff --git a/public/components/common/FiderVersion.tsx b/public/components/common/FiderVersion.tsx
index 28f14b8a6..1e6539093 100644
--- a/public/components/common/FiderVersion.tsx
+++ b/public/components/common/FiderVersion.tsx
@@ -1,10 +1,12 @@
import React from "react";
-import { Fider } from "@fider/services";
+import { useFider } from "@fider/hooks";
export const FiderVersion = () => {
+ const fider = useFider();
+
return (
<p className="info center hidden-sm hidden-md">
- {!Fider.isBillingEnabled() && (
+ {!fider.isBillingEnabled() && (
<>
Support our{" "}
<a target="_blank" href="http://opencollective.com/fider">
@@ -13,7 +15,7 @@ export const FiderVersion = () => {
<br />
</>
)}
- Fider v{Fider.settings.version}
+ Fider v{fider.settings.version}
</p>
);
};
diff --git a/public/components/common/Footer.tsx b/public/components/common/Footer.tsx
index e510f919d..1c7115514 100644
--- a/public/components/common/Footer.tsx
+++ b/public/components/common/Footer.tsx
@@ -2,13 +2,15 @@ import "./Footer.scss";
import React from "react";
import { PrivacyPolicy, TermsOfService } from "@fider/components";
-import { Fider } from "@fider/services";
+import { useFider } from "@fider/hooks";
export const Footer = () => {
+ const fider = useFider();
+
return (
<div id="c-footer">
<div className="container">
- {Fider.settings.hasLegal && (
+ {fider.settings.hasLegal && (
<div className="l-links">
<PrivacyPolicy />
&middot;
diff --git a/public/components/common/Legal.tsx b/public/components/common/Legal.tsx
index 3fca5ec83..024f3bb93 100644
--- a/public/components/common/Legal.tsx
+++ b/public/components/common/Legal.tsx
@@ -1,13 +1,15 @@
import React from "react";
import { Modal, Checkbox } from "@fider/components/common";
-import { Fider } from "@fider/services";
+import { useFider } from "@fider/hooks";
interface LegalAgreementProps {
onChange: (agreed: boolean) => void;
}
export const TermsOfService: React.FunctionComponent<{}> = () => {
- if (Fider.settings.hasLegal) {
+ const fider = useFider();
+
+ if (fider.settings.hasLegal) {
return (
<a href="/terms" target="_blank">
Terms of Service
@@ -18,7 +20,9 @@ export const TermsOfService: React.FunctionComponent<{}> = () => {
};
export const PrivacyPolicy: React.FunctionComponent<{}> = () => {
- if (Fider.settings.hasLegal) {
+ const fider = useFider();
+
+ if (fider.settings.hasLegal) {
return (
<a href="/privacy" target="_blank">
Privacy Policy
@@ -29,7 +33,9 @@ export const PrivacyPolicy: React.FunctionComponent<{}> = () => {
};
export const LegalNotice: React.FunctionComponent<{}> = () => {
- if (Fider.settings.hasLegal) {
+ const fider = useFider();
+
+ if (fider.settings.hasLegal) {
return (
<p className="info">
By signing in, you agree to the <PrivacyPolicy /> and <TermsOfService />.
@@ -40,7 +46,9 @@ export const LegalNotice: React.FunctionComponent<{}> = () => {
};
export const LegalFooter: React.FunctionComponent<{}> = () => {
- if (Fider.settings.hasLegal) {
+ const fider = useFider();
+
+ if (fider.settings.hasLegal) {
return (
<Modal.Footer align="center">
<LegalNotice />
@@ -51,7 +59,9 @@ export const LegalFooter: React.FunctionComponent<{}> = () => {
};
export const LegalAgreement: React.FunctionComponent<LegalAgreementProps> = props => {
- if (Fider.settings.hasLegal) {
+ const fider = useFider();
+
+ if (fider.settings.hasLegal) {
return (
<Checkbox field="legalAgreement" onChange={props.onChange}>
I have read and agree to the <PrivacyPolicy /> and <TermsOfService />.
diff --git a/public/pages/Administration/components/SideMenu.tsx b/public/pages/Administration/components/SideMenu.tsx
index 23ee84766..35dc5f0a6 100644
--- a/public/pages/Administration/components/SideMenu.tsx
+++ b/public/pages/Administration/components/SideMenu.tsx
@@ -1,8 +1,9 @@
import "./SideMenu.scss";
import React from "react";
-import { classSet, Fider } from "@fider/services";
+import { classSet } from "@fider/services";
import { FiderVersion } from "@fider/components";
+import { useFider } from "@fider/hooks";
interface SiteMenuProps {
activeItem: string;
@@ -39,6 +40,7 @@ const SideMenuItem = (props: SideMenuItemProps) => {
};
export const SideMenu = (props: SiteMenuProps) => {
+ const fider = useFider();
const activeItem = props.activeItem || "general";
const style = { display: props.visible ? "" : "none" };
@@ -62,9 +64,9 @@ export const SideMenu = (props: SiteMenuProps) => {
isActive={activeItem === "authentication"}
/>
<SideMenuItem name="advanced" title="Advanced" href="/admin/advanced" isActive={activeItem === "advanced"} />
- {Fider.session.user.isAdministrator && (
+ {fider.session.user.isAdministrator && (
<>
- {Fider.isBillingEnabled() && !!Fider.session.tenant.billing && (
+ {fider.isBillingEnabled() && !!fider.session.tenant.billing && (
<SideMenuItem name="billing" title="Billing" href="/admin/billing" isActive={activeItem === "billing"} />
)}
<SideMenuItem name="export" title="Export" href="/admin/export" isActive={activeItem === "export"} />
diff --git a/public/pages/Error/Error.page.spec.tsx b/public/pages/Error/Error.page.spec.tsx
index 91c7305e5..e00067387 100644
--- a/public/pages/Error/Error.page.spec.tsx
+++ b/public/pages/Error/Error.page.spec.tsx
@@ -9,8 +9,7 @@ describe("<ErrorPage />", () => {
const error = new Error("Hello");
const errorInfo = createFakeErrorInfo();
- const showError = () => true;
- const wrapper = shallow(<ErrorPage error={error} errorInfo={errorInfo} showError={showError} />);
+ const wrapper = shallow(<ErrorPage error={error} errorInfo={errorInfo} showDetails={true} />);
expect(wrapper.find("pre")).toHaveLength(1);
});
@@ -18,8 +17,7 @@ describe("<ErrorPage />", () => {
const error = new Error("Hello");
const errorInfo = createFakeErrorInfo();
- const showError = () => false;
- const wrapper = shallow(<ErrorPage error={error} errorInfo={errorInfo} showError={showError} />);
+ const wrapper = shallow(<ErrorPage error={error} errorInfo={errorInfo} showDetails={false} />);
expect(wrapper.find("pre")).toHaveLength(0);
});
diff --git a/public/pages/Error/Error.page.tsx b/public/pages/Error/Error.page.tsx
index c7843a868..3858ef062 100644
--- a/public/pages/Error/Error.page.tsx
+++ b/public/pages/Error/Error.page.tsx
@@ -1,49 +1,34 @@
import "./Error.page.scss";
import React from "react";
-import { Fider } from "@fider/services";
import { TenantLogo } from "@fider/components";
+import { useFider } from "@fider/hooks";
interface ErrorPageProps {
- title?: string;
- message?: string;
error: Error;
errorInfo: React.ErrorInfo;
- // Must be a function to avoid Fider.isProduction call prior to init
- showError?: () => boolean;
+ showDetails?: boolean;
}
-const defaultProps: Partial<ErrorPageProps> = {
- title: "Shoot! Well, this is unexpected…",
- message: "An error has occurred and we're working to fix the problem!",
- showError: () => !Fider.isProduction()
-};
+export const ErrorPage = (props: ErrorPageProps) => {
+ const fider = useFider();
-export const ErrorPage: React.SFC<ErrorPageProps> = ({
- title = "Shoot! Well, this is unexpected…",
- message = "An error has occurred and we're working to fix the problem!",
- error,
- errorInfo,
- showError
-}: ErrorPageProps) => {
return (
<div id="p-error" className="container failure-page">
<TenantLogo size={100} useFiderIfEmpty={true} />
- <h1>{title}</h1>
- <p>{message}</p>
- {Fider.settings && (
+ <h1>Shoot! Well, this is unexpected…</h1>
+ <p>An error has occurred and we're working to fix the problem!</p>
+ {fider.settings && (
<span>
- Take me back to <a href={Fider.settings.baseURL}>{Fider.settings.baseURL}</a> home page.
+ Take me back to <a href={fider.settings.baseURL}>{fider.settings.baseURL}</a> home page.
</span>
)}
- {showError && showError() && (
+ {props.showDetails && (
<pre className="error">
- {error.toString()}
- {errorInfo.componentStack}
+ {props.error.toString()}
+ {props.errorInfo.componentStack}
</pre>
)}
</div>
);
};
-
-ErrorPage.defaultProps = defaultProps;
diff --git a/public/pages/Home/Home.page.tsx b/public/pages/Home/Home.page.tsx
index 36579a679..248fa93a9 100644
--- a/public/pages/Home/Home.page.tsx
+++ b/public/pages/Home/Home.page.tsx
@@ -1,13 +1,13 @@
import "./Home.page.scss";
-import React from "react";
+import React, { useState } from "react";
import { Post, Tag, PostStatus } from "@fider/models";
import { MultiLineText, Hint } from "@fider/components";
-import { Fider } from "@fider/services";
import { SimilarPosts } from "./components/SimilarPosts";
import { FaRegLightbulb } from "react-icons/fa";
import { PostInput } from "./components/PostInput";
import { PostsContainer } from "./components/PostsContainer";
+import { useFider } from "@fider/hooks";
export interface HomePageProps {
posts: Post[];
@@ -20,11 +20,13 @@ export interface HomePageState {
}
const Lonely = () => {
+ const fider = useFider();
+
return (
<div className="l-lonely center">
<Hint
permanentCloseKey="at-least-3-posts"
- condition={Fider.session.isAuthenticated && Fider.session.user.isAdministrator}
+ condition={fider.session.isAuthenticated && fider.session.user.isAdministrator}
>
It's recommended that you post <strong>at least 3</strong> suggestions here before sharing this site. The
initial content is key to start the interactions with your audience.
@@ -41,61 +43,49 @@ const defaultWelcomeMessage = `We'd love to hear what you're thinking about.
What can we do better? This is the place for you to vote, discuss and share ideas.`;
-export default class HomePage extends React.Component<HomePageProps, HomePageState> {
- constructor(props: HomePageProps) {
- super(props);
- this.state = {
- title: ""
- };
- }
+const HomePage = (props: HomePageProps) => {
+ const fider = useFider();
+ const [title, setTitle] = useState("");
- private isLonely(): boolean {
- const len = Object.keys(this.props.countPerStatus).length;
+ const isLonely = () => {
+ const len = Object.keys(props.countPerStatus).length;
if (len === 0) {
return true;
}
- if (len === 1 && PostStatus.Deleted.value in this.props.countPerStatus) {
+ if (len === 1 && PostStatus.Deleted.value in props.countPerStatus) {
return true;
}
return false;
- }
-
- private setTitle = async (title: string) => {
- this.setState({ title });
};
- public render() {
- return (
- <div id="p-home" className="page container">
- <div className="row">
- <div className="l-welcome-col col-md-4">
- <MultiLineText
- className="welcome-message"
- text={Fider.session.tenant.welcomeMessage || defaultWelcomeMessage}
- style="full"
- />
- <PostInput
- placeholder={Fider.session.tenant.invitation || "Enter your suggestion here..."}
- onTitleChanged={this.setTitle}
- />
- </div>
- <div className="l-posts-col col-md-8">
- {this.isLonely() ? (
- <Lonely />
- ) : this.state.title ? (
- <SimilarPosts title={this.state.title} tags={this.props.tags} />
- ) : (
- <PostsContainer
- posts={this.props.posts}
- tags={this.props.tags}
- countPerStatus={this.props.countPerStatus}
- />
- )}
- </div>
+ return (
+ <div id="p-home" className="page container">
+ <div className="row">
+ <div className="l-welcome-col col-md-4">
+ <MultiLineText
+ className="welcome-message"
+ text={fider.session.tenant.welcomeMessage || defaultWelcomeMessage}
+ style="full"
+ />
+ <PostInput
+ placeholder={fider.session.tenant.invitation || "Enter your suggestion here..."}
+ onTitleChanged={setTitle}
+ />
+ </div>
+ <div className="l-posts-col col-md-8">
+ {isLonely() ? (
+ <Lonely />
+ ) : title ? (
+ <SimilarPosts title={title} tags={props.tags} />
+ ) : (
+ <PostsContainer posts={props.posts} tags={props.tags} countPerStatus={props.countPerStatus} />
+ )}
</div>
</div>
- );
- }
-}
+ </div>
+ );
+};
+
+export default HomePage;