Open platform to collect and prioritize feedback
refactor: move some components tostateless components (#647)
Guilherme Oenning964748ee021b
diff --git a/public/assets/styles/main.scss b/public/assets/styles/main.scssindex 6492f270d..5089ec754 100644--- a/public/assets/styles/main.scss+++ b/public/assets/styles/main.scss@@ -250,19 +250,31 @@ svg.icon {.markdown-body {overflow-wrap: break-word;word-break: break-word;+blockquote {padding: 0 8px;color: $gray-darkest;border-left: 4px solid $gray-light;margin: 5px 0;}+ul, ol {- margin: 0;padding-left: 30px;+ margin: 0 0 14px;++ &:first-child {+ margin-top: 0;+ }++ &:last-child {+ margin-bottom: 0;+ }}+img {max-width: 100%;}+pre {overflow-x: scroll;padding: 16px;diff --git a/public/components/common/EnvironmentInfo.tsx b/public/components/common/EnvironmentInfo.tsxindex 29a04eeac..d3dcee8ca 100644--- a/public/components/common/EnvironmentInfo.tsx+++ b/public/components/common/EnvironmentInfo.tsx@@ -1,17 +1,15 @@import React from "react";import { Fider } from "@fider/services";-export class EnvironmentInfo extends React.Component<{}, {}> {- public render() {- if (Fider.isProduction()) {- return null;- }-- return (- <div className="c-env-info">- Env: {Fider.settings.environment} | Compiler: {Fider.settings.compiler} | Version: {Fider.settings.version} |- BuildTime: {Fider.settings.buildTime}- </div>- );+export const EnvironmentInfo = () => {+ if (Fider.isProduction()) {+ return null;}-}++ return (+ <div className="c-env-info">+ Env: {Fider.settings.environment} | Compiler: {Fider.settings.compiler} | Version: {Fider.settings.version} |+ BuildTime: {Fider.settings.buildTime}+ </div>+ );+};diff --git a/public/components/common/SocialSignInButton.tsx b/public/components/common/SocialSignInButton.tsxindex 3b54702d4..f00dcd79b 100644--- a/public/components/common/SocialSignInButton.tsx+++ b/public/components/common/SocialSignInButton.tsx@@ -13,23 +13,18 @@ interface SocialSignInButtonProps {redirectTo?: string;}-export class SocialSignInButton extends React.Component<SocialSignInButtonProps, {}> {- public render() {- const redirectTo = this.props.redirectTo || location.href;- const href = this.props.option.url ? `${this.props.option.url}?redirect=${redirectTo}` : undefined;- const className = classSet({- "m-social": true,- [`m-${this.props.option.provider}`]: this.props.option.provider- });- return (- <Button href={href} rel="nofollow" fluid={true} className={className}>- {this.props.option.logoURL ? (- <img src={this.props.option.logoURL} />- ) : (- <OAuthProviderLogo option={this.props.option} />- )}- <span>{this.props.option.displayName}</span>- </Button>- );- }-}+export const SocialSignInButton = (props: SocialSignInButtonProps) => {+ const redirectTo = props.redirectTo || location.href;+ const href = props.option.url ? `${props.option.url}?redirect=${redirectTo}` : undefined;+ const className = classSet({+ "m-social": true,+ [`m-${props.option.provider}`]: props.option.provider+ });++ return (+ <Button href={href} rel="nofollow" fluid={true} className={className}>+ {props.option.logoURL ? <img src={props.option.logoURL} /> : <OAuthProviderLogo option={props.option} />}+ <span>{props.option.displayName}</span>+ </Button>+ );+};diff --git a/public/components/common/form/Form.tsx b/public/components/common/form/Form.tsxindex 68f5dcd6b..a0106353f 100644--- a/public/components/common/form/Form.tsx+++ b/public/components/common/form/Form.tsx@@ -16,27 +16,17 @@ interface FormProps {export const ValidationContext = React.createContext<ValidationContext>({});-export class Form extends React.Component<FormProps, {}> {- constructor(props: FormProps) {- super(props);- this.state = {- error: this.props.error- };- }+export const Form: React.StatelessComponent<FormProps> = props => {+ const className = classSet({+ "c-form": true,+ [props.className!]: props.className,+ [`m-${props.size}`]: props.size+ });- public render() {- const className = classSet({- "c-form": true,- [this.props.className!]: this.props.className,- [`m-${this.props.size}`]: this.props.size- });- return (- <form autoComplete="off" className={className}>- <DisplayError error={this.props.error} />- <ValidationContext.Provider value={{ error: this.props.error }}>- {this.props.children}- </ValidationContext.Provider>- </form>- );- }-}+ return (+ <form autoComplete="off" className={className}>+ <DisplayError error={props.error} />+ <ValidationContext.Provider value={{ error: props.error }}>{props.children}</ValidationContext.Provider>+ </form>+ );+};diff --git a/public/components/common/form/Input.tsx b/public/components/common/form/Input.tsxindex 42912621a..33483b8f4 100644--- a/public/components/common/form/Input.tsx+++ b/public/components/common/form/Input.tsx@@ -23,79 +23,69 @@ interface InputProps {onChange?: (value: string) => void;}-export class Input extends React.Component<InputProps, {}> {- constructor(props: InputProps) {- super(props);- }-- private onChange = (e: React.FormEvent<HTMLInputElement>) => {- if (this.props.onChange) {- this.props.onChange(e.currentTarget.value);+export const Input: React.StatelessComponent<InputProps> = props => {+ const onChange = (e: React.FormEvent<HTMLInputElement>) => {+ if (props.onChange) {+ props.onChange(e.currentTarget.value);}};- private onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>): void => {- if (event.keyCode === 13 && this.props.onSubmit) {- this.props.onSubmit();+ const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>): void => {+ if (event.keyCode === 13 && props.onSubmit) {+ props.onSubmit();event.preventDefault();}};- public render() {- const suffix =- typeof this.props.suffix === "string" ? (- <span className="c-form-input-suffix">{this.props.suffix}</span>- ) : (- this.props.suffix- );+ const suffix =+ typeof props.suffix === "string" ? <span className="c-form-input-suffix">{props.suffix}</span> : props.suffix;- const icon = !!this.props.icon- ? React.createElement(this.props.icon, {- onClick: this.props.onIconClick,- className: classSet({ link: !!this.props.onIconClick })- })- : undefined;+ const icon = !!props.icon+ ? React.createElement(props.icon, {+ onClick: props.onIconClick,+ className: classSet({ link: !!props.onIconClick })+ })+ : undefined;- return (- <ValidationContext.Consumer>- {ctx => (- <div- className={classSet({- "c-form-field": true,- "m-suffix": this.props.suffix,- "m-error": hasError(this.props.field, ctx.error),- "m-icon": !!this.props.icon,- [`${this.props.className}`]: this.props.className- })}- >- {!!this.props.label && (- <label htmlFor={`input-${this.props.field}`}>- {this.props.label}- {this.props.afterLabel}- </label>- )}- <div className="c-form-field-wrapper">- <input- id={`input-${this.props.field}`}- type="text"- ref={this.props.inputRef}- autoFocus={this.props.autoFocus}- onFocus={this.props.onFocus}- maxLength={this.props.maxLength}- disabled={this.props.disabled}- value={this.props.value}- placeholder={this.props.placeholder}- onKeyDown={this.props.onSubmit ? this.onKeyDown : undefined}- onChange={this.onChange}- />- {icon}- {suffix}- </div>- <DisplayError fields={[this.props.field]} error={ctx.error} />- {this.props.children}+ return (+ <ValidationContext.Consumer>+ {ctx => (+ <div+ className={classSet({+ "c-form-field": true,+ "m-suffix": props.suffix,+ "m-error": hasError(props.field, ctx.error),+ "m-icon": !!props.icon,+ [`${props.className}`]: props.className+ })}+ >+ {!!props.label && (+ <label htmlFor={`input-${props.field}`}>+ {props.label}+ {props.afterLabel}+ </label>+ )}+ <div className="c-form-field-wrapper">+ <input+ id={`input-${props.field}`}+ type="text"+ ref={props.inputRef}+ autoFocus={props.autoFocus}+ onFocus={props.onFocus}+ maxLength={props.maxLength}+ disabled={props.disabled}+ value={props.value}+ placeholder={props.placeholder}+ onKeyDown={props.onSubmit ? onKeyDown : undefined}+ onChange={onChange}+ />+ {icon}+ {suffix}</div>- )}- </ValidationContext.Consumer>- );- }-}+ <DisplayError fields={[props.field]} error={ctx.error} />+ {props.children}+ </div>+ )}+ </ValidationContext.Consumer>+ );+};diff --git a/public/components/common/form/TextArea.tsx b/public/components/common/form/TextArea.tsxindex ea3e002ee..77fdb664e 100644--- a/public/components/common/form/TextArea.tsx+++ b/public/components/common/form/TextArea.tsx@@ -16,47 +16,41 @@ interface TextAreaProps {onFocus?: React.FocusEventHandler<HTMLTextAreaElement>;}-export class TextArea extends React.Component<TextAreaProps, {}> {- constructor(props: TextAreaProps) {- super(props);- }-- private onChange = (e: React.FormEvent<HTMLTextAreaElement>) => {- if (this.props.onChange) {- this.props.onChange(e.currentTarget.value);+export const TextArea: React.StatelessComponent<TextAreaProps> = props => {+ const onChange = (e: React.FormEvent<HTMLTextAreaElement>) => {+ if (props.onChange) {+ props.onChange(e.currentTarget.value);}};- public render() {- return (- <ValidationContext.Consumer>- {ctx => (- <>- <div- className={classSet({- "c-form-field": true,- "m-error": hasError(this.props.field, ctx.error)- })}- >- {!!this.props.label && <label htmlFor={`input-${this.props.field}`}>{this.props.label}</label>}- <div className="c-form-field-wrapper">- <Textarea- id={`input-${this.props.field}`}- disabled={this.props.disabled}- onChange={this.onChange}- value={this.props.value}- minRows={this.props.minRows || 3}- placeholder={this.props.placeholder}- inputRef={this.props.inputRef}- onFocus={this.props.onFocus}- />- </div>- <DisplayError fields={[this.props.field]} error={ctx.error} />- {this.props.children}+ return (+ <ValidationContext.Consumer>+ {ctx => (+ <>+ <div+ className={classSet({+ "c-form-field": true,+ "m-error": hasError(props.field, ctx.error)+ })}+ >+ {!!props.label && <label htmlFor={`input-${props.field}`}>{props.label}</label>}+ <div className="c-form-field-wrapper">+ <Textarea+ id={`input-${props.field}`}+ disabled={props.disabled}+ onChange={onChange}+ value={props.value}+ minRows={props.minRows || 3}+ placeholder={props.placeholder}+ inputRef={props.inputRef}+ onFocus={props.onFocus}+ /></div>- </>- )}- </ValidationContext.Consumer>- );- }-}+ <DisplayError fields={[props.field]} error={ctx.error} />+ {props.children}+ </div>+ </>+ )}+ </ValidationContext.Consumer>+ );+};diff --git a/public/pages/Home/components/ListPosts.tsx b/public/pages/Home/components/ListPosts.tsxindex 85f4a7dc7..d677dc560 100644--- a/public/pages/Home/components/ListPosts.tsx+++ b/public/pages/Home/components/ListPosts.tsx@@ -34,30 +34,20 @@ const ListPostItem = (props: { post: Post; user?: CurrentUser; tags: Tag[] }) =>);};-export class ListPosts extends React.Component<ListPostsProps, {}> {- constructor(props: ListPostsProps) {- super(props);+export const ListPosts = (props: ListPostsProps) => {+ if (!props.posts) {+ return null;}- public render() {- if (!this.props.posts) {- return null;- }-- if (this.props.posts.length === 0) {- return <p className="center">{this.props.emptyText}</p>;- }-- return (- <List className="c-post-list" divided={true}>- {this.props.posts.map(post => (- <ListPostItem- key={post.id}- post={post}- tags={this.props.tags.filter(tag => post.tags.indexOf(tag.slug) >= 0)}- />- ))}- </List>- );+ if (props.posts.length === 0) {+ return <p className="center">{props.emptyText}</p>;}-}++ return (+ <List className="c-post-list" divided={true}>+ {props.posts.map(post => (+ <ListPostItem key={post.id} post={post} tags={props.tags.filter(tag => post.tags.indexOf(tag.slug) >= 0)} />+ ))}+ </List>+ );+};diff --git a/public/pages/Home/components/PostFilter.tsx b/public/pages/Home/components/PostFilter.tsxindex dfd7483e2..7006a3a73 100644--- a/public/pages/Home/components/PostFilter.tsx+++ b/public/pages/Home/components/PostFilter.tsx@@ -11,59 +11,53 @@ interface PostFilterProps {viewChanged: (name: string) => void;}-export class PostFilter extends React.Component<PostFilterProps, {}> {- constructor(props: PostFilterProps) {- super(props);- }-- private handleChangeView = (item: DropDownItem) => {- this.props.viewChanged(item.value as string);+export const PostFilter = (props: PostFilterProps) => {+ const handleChangeView = (item: DropDownItem) => {+ props.viewChanged(item.value as string);};- public renderText = (item?: DropDownItem) => {+ const renderText = (item?: DropDownItem) => {return <>{item!.label.toLowerCase()}</>;};- public render() {- const options: DropDownItem[] = [- { value: "trending", label: "Trending" },- { value: "recent", label: "Recent" },- { value: "most-wanted", label: "Most Wanted" },- { value: "most-discussed", label: "Most Discussed" }- ];+ const options: DropDownItem[] = [+ { value: "trending", label: "Trending" },+ { value: "recent", label: "Recent" },+ { value: "most-wanted", label: "Most Wanted" },+ { value: "most-discussed", label: "Most Discussed" }+ ];- if (Fider.session.isAuthenticated) {- options.push({ value: "my-votes", label: "My Votes" });- }+ if (Fider.session.isAuthenticated) {+ options.push({ value: "my-votes", label: "My Votes" });+ }- PostStatus.All.filter(s => s.filterable && this.props.countPerStatus[s.value]).forEach(s => {- options.push({- label: s.title,- value: s.value,- render: (- <span>- {s.title} <a className="counter">{this.props.countPerStatus[s.value]}</a>- </span>- )- });+ PostStatus.All.filter(s => s.filterable && props.countPerStatus[s.value]).forEach(s => {+ options.push({+ label: s.title,+ value: s.value,+ render: (+ <span>+ {s.title} <a className="counter">{props.countPerStatus[s.value]}</a>+ </span>+ )});-- const viewExists = options.filter(x => x.value === this.props.activeView).length > 0;- const activeView = viewExists ? this.props.activeView : "trending";-- return (- <>- Show{" "}- <DropDown- className="l-post-filter"- header="What do you want to see?"- inline={true}- items={options}- renderText={this.renderText}- defaultValue={activeView}- onChange={this.handleChangeView}- />{" "}- </>- );- }-}+ });++ const viewExists = options.filter(x => x.value === props.activeView).length > 0;+ const activeView = viewExists ? props.activeView : "trending";++ return (+ <>+ Show{" "}+ <DropDown+ className="l-post-filter"+ header="What do you want to see?"+ inline={true}+ items={options}+ renderText={renderText}+ defaultValue={activeView}+ onChange={handleChangeView}+ />{" "}+ </>+ );+};diff --git a/public/pages/ShowPost/components/DiscussionPanel.tsx b/public/pages/ShowPost/components/DiscussionPanel.tsxindex f16f0017f..faf2366a6 100644--- a/public/pages/ShowPost/components/DiscussionPanel.tsx+++ b/public/pages/ShowPost/components/DiscussionPanel.tsx@@ -11,22 +11,16 @@ interface DiscussionPanelProps {comments: Comment[];}-export class DiscussionPanel extends React.Component<DiscussionPanelProps, {}> {- constructor(props: DiscussionPanelProps) {- super(props);- }-- public render() {- return (- <div className="comments-col">- <div className="c-comment-list">- <span className="subtitle">Discussion</span>- {this.props.comments.map(c => (- <ShowComment key={c.id} post={this.props.post} comment={c} />- ))}- <CommentInput post={this.props.post} />- </div>+export const DiscussionPanel = (props: DiscussionPanelProps) => {+ return (+ <div className="comments-col">+ <div className="c-comment-list">+ <span className="subtitle">Discussion</span>+ {props.comments.map(c => (+ <ShowComment key={c.id} post={props.post} comment={c} />+ ))}+ <CommentInput post={props.post} /></div>- );- }-}+ </div>+ );+};diff --git a/public/pages/ShowPost/components/TagListItem.tsx b/public/pages/ShowPost/components/TagListItem.tsxindex 77f1861c9..4d246f7a8 100644--- a/public/pages/ShowPost/components/TagListItem.tsx+++ b/public/pages/ShowPost/components/TagListItem.tsx@@ -9,18 +9,16 @@ interface TagListItemProps {onClick: (tag: Tag) => void;}-export class TagListItem extends React.Component<TagListItemProps, {}> {- private onClick = () => {- this.props.onClick(this.props.tag);+export const TagListItem = (props: TagListItemProps) => {+ const onClick = () => {+ props.onClick(props.tag);};- public render() {- return (- <ListItem onClick={this.onClick}>- {this.props.assigned ? <FaCheck /> : <svg className="icon" />}- <ShowTag tag={this.props.tag} size="mini" circular={true} />- <span>{this.props.tag.name}</span>- </ListItem>- );- }-}+ return (+ <ListItem onClick={onClick}>+ {props.assigned ? <FaCheck /> : <svg className="icon" />}+ <ShowTag tag={props.tag} size="mini" circular={true} />+ <span>{props.tag.name}</span>+ </ListItem>+ );+};