Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: remove global Fider service from non-page components (#825)

Guilherme Oenning1dae3a752e06
diff --git a/public/components/common/form/Input.tsx b/public/components/common/form/Input.tsx
index 39744a48e..e7eff8716 100644
--- a/public/components/common/form/Input.tsx
+++ b/public/components/common/form/Input.tsx
@@ -20,7 +20,7 @@ interface InputProps {
placeholder?: string;
onIconClick?: () => void;
onFocus?: () => void;
- inputRef?: (node: HTMLInputElement) => void;
+ inputRef?: React.MutableRefObject<any>;
onChange?: (value: string) => void;
}
diff --git a/public/pages/Administration/components/TagListItem.tsx b/public/pages/Administration/components/TagListItem.tsx
index 1eca68d38..1cb6547f1 100644
--- a/public/pages/Administration/components/TagListItem.tsx
+++ b/public/pages/Administration/components/TagListItem.tsx
@@ -1,9 +1,10 @@
-import React from "react";
+import React, { useState } from "react";
import { Tag } from "@fider/models";
import { ListItem, ShowTag, Button } from "@fider/components";
import { TagFormState, TagForm } from "./TagForm";
-import { actions, Failure, Fider } from "@fider/services";
+import { actions, Failure } from "@fider/services";
import { FaTimes, FaEdit } from "react-icons/fa";
+import { useFider } from "@fider/hooks";
interface TagListItemProps {
tag: Tag;
@@ -11,94 +12,64 @@ interface TagListItemProps {
onTagDeleted: (tag: Tag) => void;
}
-interface TagListItemState {
- tag: Tag;
- isDeleting: boolean;
- isEditing: boolean;
-}
-
-export class TagListItem extends React.Component<TagListItemProps, TagListItemState> {
- constructor(props: TagListItemProps) {
- super(props);
- this.state = {
- tag: props.tag,
- isDeleting: false,
- isEditing: false
- };
- }
-
- private startDelete = async () => {
- this.setState({ isDeleting: true, isEditing: false });
- };
+export const TagListItem = (props: TagListItemProps) => {
+ const fider = useFider();
+ const [tag] = useState(props.tag);
+ const [state, setState] = useState<"view" | "edit" | "delete">("view");
- private cancelDelete = async () => {
- this.setState({ isDeleting: false });
- };
+ const startDelete = async () => setState("delete");
+ const startEdit = async () => setState("edit");
+ const resetState = async () => setState("view");
- private deleteTag = async () => {
- const result = await actions.deleteTag(this.state.tag.slug);
+ const deleteTag = async () => {
+ const result = await actions.deleteTag(tag.slug);
if (result.ok) {
- this.setState({
- isDeleting: false
- });
- this.props.onTagDeleted(this.state.tag);
+ resetState();
+ props.onTagDeleted(tag);
}
};
- private startEdit = async () => {
- this.setState({ isDeleting: false, isEditing: true });
- };
-
- private cancelEdit = async () => {
- this.setState({ isEditing: false });
- };
-
- private updateTag = async (data: TagFormState): Promise<Failure | undefined> => {
- const result = await actions.updateTag(this.state.tag.slug, data.name, data.color, data.isPublic);
+ const updateTag = async (data: TagFormState): Promise<Failure | undefined> => {
+ const result = await actions.updateTag(tag.slug, data.name, data.color, data.isPublic);
if (result.ok) {
- const tag = this.state.tag;
tag.name = result.data.name;
tag.slug = result.data.slug;
tag.color = result.data.color;
tag.isPublic = result.data.isPublic;
- this.setState({
- isEditing: false,
- tag
- });
-
- this.props.onTagEdited(tag);
+ resetState();
+ props.onTagEdited(tag);
} else {
return result.error;
}
};
- private renderDeleteMode() {
+ const renderDeleteMode = () => {
return (
<>
<div className="content">
<b>Are you sure?</b>{" "}
<span>
- The tag <ShowTag tag={this.state.tag} /> will be removed from all posts.
+ The tag <ShowTag tag={tag} /> will be removed from all posts.
</span>
</div>
- <Button className="right" onClick={this.cancelDelete} color="cancel">
+ <Button className="right" onClick={resetState} color="cancel">
Cancel
</Button>
- <Button color="danger" className="right" onClick={this.deleteTag}>
+ <Button color="danger" className="right" onClick={deleteTag}>
Delete tag
</Button>
</>
);
- }
+ };
- private renderViewMode() {
- const buttons = Fider.session.user.isAdministrator && [
- <Button size="mini" key={0} onClick={this.startDelete} className="right">
+ const renderViewMode = () => {
+ const buttons = fider.session.user.isAdministrator && [
+ <Button size="mini" key={0} onClick={startDelete} className="right">
<FaTimes />
Delete
</Button>,
- <Button size="mini" key={1} onClick={this.startEdit} className="right">
+ <Button size="mini" key={1} onClick={startEdit} className="right">
<FaEdit />
Edit
</Button>
@@ -106,31 +77,25 @@ export class TagListItem extends React.Component<TagListItemProps, TagListItemSt
return (
<>
- <ShowTag tag={this.state.tag} />
+ <ShowTag tag={tag} />
{buttons}
</>
);
- }
+ };
- private renderEditMode() {
+ const renderEditMode = () => {
return (
<TagForm
- name={this.props.tag.name}
- color={this.props.tag.color}
- isPublic={this.props.tag.isPublic}
- onSave={this.updateTag}
- onCancel={this.cancelEdit}
+ name={props.tag.name}
+ color={props.tag.color}
+ isPublic={props.tag.isPublic}
+ onSave={updateTag}
+ onCancel={resetState}
/>
);
- }
+ };
- public render() {
- const view = this.state.isDeleting
- ? this.renderDeleteMode()
- : this.state.isEditing
- ? this.renderEditMode()
- : this.renderViewMode();
+ const view = state === "delete" ? renderDeleteMode() : state === "edit" ? renderEditMode() : renderViewMode();
- return <ListItem>{view}</ListItem>;
- }
-}
+ return <ListItem>{view}</ListItem>;
+};
diff --git a/public/pages/Home/components/PostInput.tsx b/public/pages/Home/components/PostInput.tsx
index 9c57bf197..8010a2858 100644
--- a/public/pages/Home/components/PostInput.tsx
+++ b/public/pages/Home/components/PostInput.tsx
@@ -1,122 +1,105 @@
-import React from "react";
+import React, { useState, useEffect, useRef } from "react";
import { Button, ButtonClickEvent, Input, Form, TextArea, MultiImageUploader } from "@fider/components";
import { SignInModal } from "@fider/components";
-import { cache, actions, Failure, Fider } from "@fider/services";
+import { cache, actions, Failure } from "@fider/services";
import { ImageUpload } from "@fider/models";
+import { useFider } from "@fider/hooks";
interface PostInputProps {
placeholder: string;
onTitleChanged: (title: string) => void;
}
-interface PostInputState {
- title: string;
- description: string;
- attachments: ImageUpload[];
- focused: boolean;
- showSignIn: boolean;
- error?: Failure;
-}
-
const CACHE_TITLE_KEY = "PostInput-Title";
const CACHE_DESCRIPTION_KEY = "PostInput-Description";
-export class PostInput extends React.Component<PostInputProps, PostInputState> {
- private title?: HTMLInputElement;
+export const PostInput = (props: PostInputProps) => {
+ const getCachedValue = (key: string): string => {
+ if (fider.session.isAuthenticated) {
+ return cache.session.get(key) || "";
+ }
+ return "";
+ };
- constructor(props: PostInputProps) {
- super(props);
- this.state = {
- title: (Fider.session.isAuthenticated && cache.session.get(CACHE_TITLE_KEY)) || "",
- description: (Fider.session.isAuthenticated && cache.session.get(CACHE_DESCRIPTION_KEY)) || "",
- focused: false,
- showSignIn: false,
- attachments: []
- };
+ const fider = useFider();
+ const titleRef = useRef<HTMLInputElement>();
+ const [title, setTitle] = useState(getCachedValue(CACHE_TITLE_KEY));
+ const [description, setDescription] = useState(getCachedValue(CACHE_DESCRIPTION_KEY));
+ const [isSignInModalOpen, setIsSignInModalOpen] = useState(false);
+ const [attachments, setAttachments] = useState<ImageUpload[]>([]);
+ const [error, setError] = useState<Failure | undefined>(undefined);
- if (this.state.title) {
- this.props.onTitleChanged(this.state.title);
- }
- }
+ useEffect(() => {
+ props.onTitleChanged(title);
+ }, [title]);
- private handleTitleFocus = () => {
- if (!Fider.session.isAuthenticated && this.title) {
- this.title.blur();
- this.setState({ showSignIn: true });
+ const handleTitleFocus = () => {
+ if (!fider.session.isAuthenticated && titleRef.current) {
+ titleRef.current.blur();
+ setIsSignInModalOpen(true);
}
};
- private setTitle = (title: string) => {
- cache.session.set(CACHE_TITLE_KEY, title);
- this.setState({ title });
- this.props.onTitleChanged(title);
+ const handleTitleChange = (value: string) => {
+ cache.session.set(CACHE_TITLE_KEY, value);
+ setTitle(value);
+ props.onTitleChanged(value);
};
- private setDescription = (description: string) => {
- cache.session.set(CACHE_DESCRIPTION_KEY, description);
- this.setState({ description });
- };
+ const hideModal = () => setIsSignInModalOpen(false);
+ const clearError = () => setError(undefined);
- private setAttachments = (attachments: ImageUpload[]) => {
- this.setState({ attachments });
+ const handleDescriptionChange = (value: string) => {
+ cache.session.set(CACHE_DESCRIPTION_KEY, value);
+ setDescription(value);
};
- private submit = async (event: ButtonClickEvent) => {
- if (this.state.title) {
- const result = await actions.createPost(this.state.title, this.state.description, this.state.attachments);
+ const submit = async (event: ButtonClickEvent) => {
+ if (title) {
+ const result = await actions.createPost(title, description, attachments);
if (result.ok) {
- this.setState({ error: undefined });
+ clearError();
cache.session.remove(CACHE_TITLE_KEY, CACHE_DESCRIPTION_KEY);
location.href = `/posts/${result.data.number}/${result.data.slug}`;
event.preventEnable();
} else if (result.error) {
- this.setState({ error: result.error });
+ setError(result.error);
}
}
};
- private hideModal = () => {
- this.setState({ showSignIn: false });
- };
-
- private setInputRef = (e: HTMLInputElement) => {
- this.title = e;
- };
+ const details = () => (
+ <>
+ <TextArea
+ field="description"
+ onChange={handleDescriptionChange}
+ value={description}
+ minRows={5}
+ placeholder="Describe your suggestion (optional)"
+ />
+ <MultiImageUploader field="attachments" maxUploads={3} previewMaxWidth={100} onChange={setAttachments} />
+ <Button type="submit" color="positive" onClick={submit}>
+ Submit
+ </Button>
+ </>
+ );
- public render() {
- const details = (
- <>
- <TextArea
- field="description"
- onChange={this.setDescription}
- value={this.state.description}
- minRows={5}
- placeholder="Describe your suggestion (optional)"
+ return (
+ <>
+ <SignInModal isOpen={isSignInModalOpen} onClose={hideModal} />
+ <Form error={error}>
+ <Input
+ field="title"
+ noTabFocus={!fider.session.isAuthenticated}
+ inputRef={titleRef}
+ onFocus={handleTitleFocus}
+ maxLength={100}
+ value={title}
+ onChange={handleTitleChange}
+ placeholder={props.placeholder}
/>
- <MultiImageUploader field="attachments" maxUploads={3} previewMaxWidth={100} onChange={this.setAttachments} />
- <Button type="submit" color="positive" onClick={this.submit}>
- Submit
- </Button>
- </>
- );
-
- return (
- <>
- <SignInModal isOpen={this.state.showSignIn} onClose={this.hideModal} />
- <Form error={this.state.error}>
- <Input
- field="title"
- noTabFocus={!Fider.session.isAuthenticated}
- inputRef={this.setInputRef}
- onFocus={this.handleTitleFocus}
- maxLength={100}
- value={this.state.title}
- onChange={this.setTitle}
- placeholder={this.props.placeholder}
- />
- {this.state.title && details}
- </Form>
- </>
- );
- }
-}
+ {title && details()}
+ </Form>
+ </>
+ );
+};
diff --git a/public/pages/MySettings/components/NotificationSettings.tsx b/public/pages/MySettings/components/NotificationSettings.tsx
index f4386d5d5..49ab8bcf5 100644
--- a/public/pages/MySettings/components/NotificationSettings.tsx
+++ b/public/pages/MySettings/components/NotificationSettings.tsx
@@ -1,63 +1,49 @@
-import React from "react";
+import React, { useState } from "react";
import { UserSettings } from "@fider/models";
import { Toggle, Segment, Segments, Field } from "@fider/components";
-import { Fider } from "@fider/services";
+import { useFider } from "@fider/hooks";
interface NotificationSettingsProps {
userSettings: UserSettings;
settingsChanged: (settings: UserSettings) => void;
}
-interface NotificationSettingsState {
- userSettings: UserSettings;
-}
-
type Channel = number;
const WebChannel: Channel = 1;
const EmailChannel: Channel = 2;
-export class NotificationSettings extends React.Component<NotificationSettingsProps, NotificationSettingsState> {
- constructor(props: NotificationSettingsProps) {
- super(props);
-
- this.state = {
- userSettings: this.props.userSettings
- };
- }
+export const NotificationSettings = (props: NotificationSettingsProps) => {
+ const fider = useFider();
+ const [userSettings, setUserSettings] = useState(props.userSettings);
- private isEnabled(settingsKey: string, channel: Channel): boolean {
- if (settingsKey in this.state.userSettings) {
- return (parseInt(this.state.userSettings[settingsKey], 10) & channel) > 0;
+ const isEnabled = (settingsKey: string, channel: Channel): boolean => {
+ if (settingsKey in userSettings) {
+ return (parseInt(userSettings[settingsKey], 10) & channel) > 0;
}
return false;
- }
-
- private async toggle(settingsKey: string, channel: Channel) {
- const userSettings = { ...this.state.userSettings };
- userSettings[settingsKey] = (parseInt(this.state.userSettings[settingsKey], 10) ^ channel).toString();
+ };
- this.setState({ userSettings });
- this.props.settingsChanged(userSettings);
- }
+ const toggle = async (settingsKey: string, channel: Channel) => {
+ const nextSettings = {
+ ...userSettings,
+ [settingsKey]: (parseInt(userSettings[settingsKey], 10) ^ channel).toString()
+ };
+ setUserSettings(nextSettings);
+ props.settingsChanged(nextSettings);
+ };
- private icon(settingsKey: string, channel: Channel) {
- const active = this.isEnabled(settingsKey, channel);
+ const icon = (settingsKey: string, channel: Channel) => {
+ const active = isEnabled(settingsKey, channel);
const label = channel === WebChannel ? "Web" : "Email";
- return (
- <Toggle
- key={`${settingsKey}_${channel}`}
- active={active}
- label={label}
- onToggle={this.toggle.bind(this, settingsKey, channel)}
- />
- );
- }
+ const onToggle = () => toggle(settingsKey, channel);
+ return <Toggle key={`${settingsKey}_${channel}`} active={active} label={label} onToggle={onToggle} />;
+ };
- private info(settingsKey: string, aboutForVisitors: string, aboutForCollaborators: string) {
- const about = Fider.session.user.isCollaborator ? aboutForCollaborators : aboutForVisitors;
- const webEnabled = this.isEnabled(settingsKey, WebChannel);
- const emailEnabled = this.isEnabled(settingsKey, EmailChannel);
+ const info = (settingsKey: string, aboutForVisitors: string, aboutForCollaborators: string) => {
+ const about = fider.session.user.isCollaborator ? aboutForCollaborators : aboutForVisitors;
+ const webEnabled = isEnabled(settingsKey, WebChannel);
+ const emailEnabled = isEnabled(settingsKey, EmailChannel);
if (!webEnabled && !emailEnabled) {
return (
@@ -85,52 +71,50 @@ export class NotificationSettings extends React.Component<NotificationSettingsPr
);
}
return null;
- }
+ };
- public render() {
- return (
- <>
- <Field label="Notifications">
- <p className="info">Use following panel to choose which events you'd like to receive notification</p>
- </Field>
+ return (
+ <>
+ <Field label="Notifications">
+ <p className="info">Use following panel to choose which events you'd like to receive notification</p>
+ </Field>
- <div className="notifications-settings">
- <Segments>
- <Segment>
- <span className="event-title">New Post</span>
- {this.info("event_notification_new_post", "new posts on this site", "new posts on this site")}
- <p>
- {this.icon("event_notification_new_post", WebChannel)}
- {this.icon("event_notification_new_post", EmailChannel)}
- </p>
- </Segment>
- <Segment>
- <span className="event-title">Discussion</span>
- {this.info(
- "event_notification_new_comment",
- "comments on posts you've subscribed to",
- "comments on all posts unless individually unsubscribed"
- )}
- <p>
- {this.icon("event_notification_new_comment", WebChannel)}
- {this.icon("event_notification_new_comment", EmailChannel)}
- </p>
- </Segment>
- <Segment>
- <span className="event-title">Status Changed</span>
- {this.info(
- "event_notification_change_status",
- "status change on posts you've subscribed to",
- "status change on all posts unless individually unsubscribed"
- )}
- <p>
- {this.icon("event_notification_change_status", WebChannel)}
- {this.icon("event_notification_change_status", EmailChannel)}
- </p>
- </Segment>
- </Segments>
- </div>
- </>
- );
- }
-}
+ <div className="notifications-settings">
+ <Segments>
+ <Segment>
+ <span className="event-title">New Post</span>
+ {info("event_notification_new_post", "new posts on this site", "new posts on this site")}
+ <p>
+ {icon("event_notification_new_post", WebChannel)}
+ {icon("event_notification_new_post", EmailChannel)}
+ </p>
+ </Segment>
+ <Segment>
+ <span className="event-title">Discussion</span>
+ {info(
+ "event_notification_new_comment",
+ "comments on posts you've subscribed to",
+ "comments on all posts unless individually unsubscribed"
+ )}
+ <p>
+ {icon("event_notification_new_comment", WebChannel)}
+ {icon("event_notification_new_comment", EmailChannel)}
+ </p>
+ </Segment>
+ <Segment>
+ <span className="event-title">Status Changed</span>
+ {info(
+ "event_notification_change_status",
+ "status change on posts you've subscribed to",
+ "status change on all posts unless individually unsubscribed"
+ )}
+ <p>
+ {icon("event_notification_change_status", WebChannel)}
+ {icon("event_notification_change_status", EmailChannel)}
+ </p>
+ </Segment>
+ </Segments>
+ </div>
+ </>
+ );
+};
diff --git a/public/pages/ShowPost/components/NotificationsPanel.tsx b/public/pages/ShowPost/components/NotificationsPanel.tsx
index 43653fd00..241d1c375 100644
--- a/public/pages/ShowPost/components/NotificationsPanel.tsx
+++ b/public/pages/ShowPost/components/NotificationsPanel.tsx
@@ -1,68 +1,57 @@
-import React from "react";
+import React, { useState } from "react";
import { Post } from "@fider/models";
import { Button, List, ListItem } from "@fider/components";
-import { actions, Fider } from "@fider/services";
+import { actions } from "@fider/services";
import { FaVolumeUp, FaVolumeMute } from "react-icons/fa";
+import { useFider } from "@fider/hooks";
interface NotificationsPanelProps {
post: Post;
subscribed: boolean;
}
-interface NotificationsPanelState {
- subscribed: boolean;
-}
+export const NotificationsPanel = (props: NotificationsPanelProps) => {
+ const fider = useFider();
+ const [subscribed, setSubscribed] = useState(props.subscribed);
-export class NotificationsPanel extends React.Component<NotificationsPanelProps, NotificationsPanelState> {
- constructor(props: NotificationsPanelProps) {
- super(props);
- this.state = {
- subscribed: this.props.subscribed
- };
- }
+ const subscribeOrUnsubscribe = async () => {
+ const action = subscribed ? actions.unsubscribe : actions.subscribe;
- private subscribeOrUnsubscribe = async () => {
- const action = this.state.subscribed ? actions.unsubscribe : actions.subscribe;
-
- const response = await action(this.props.post.number);
+ const response = await action(props.post.number);
if (response.ok) {
- this.setState(state => ({
- subscribed: !state.subscribed
- }));
+ setSubscribed(!subscribed);
}
};
- public render() {
- if (!Fider.session.isAuthenticated) {
- return null;
- }
-
- const button = this.state.subscribed ? (
- <Button fluid={true} onClick={this.subscribeOrUnsubscribe}>
- <FaVolumeMute /> Unsubscribe
- </Button>
- ) : (
- <Button fluid={true} onClick={this.subscribeOrUnsubscribe}>
- <FaVolumeUp /> Subscribe
- </Button>
- );
-
- const text = this.state.subscribed ? (
- <span className="info">You’re receiving notifications about activity on this post.</span>
- ) : (
- <span className="info">You'll not receive any notification about this post.</span>
- );
-
- return (
- <>
- <span className="subtitle">Notifications</span>
- <List>
- <ListItem>
- {button}
- {text}
- </ListItem>
- </List>
- </>
- );
+ if (!fider.session.isAuthenticated) {
+ return null;
}
-}
+
+ const button = subscribed ? (
+ <Button fluid={true} onClick={subscribeOrUnsubscribe}>
+ <FaVolumeMute /> Unsubscribe
+ </Button>
+ ) : (
+ <Button fluid={true} onClick={subscribeOrUnsubscribe}>
+ <FaVolumeUp /> Subscribe
+ </Button>
+ );
+
+ const text = subscribed ? (
+ <span className="info">You’re receiving notifications about activity on this post.</span>
+ ) : (
+ <span className="info">You'll not receive any notification about this post.</span>
+ );
+
+ return (
+ <>
+ <span className="subtitle">Notifications</span>
+ <List>
+ <ListItem>
+ {button}
+ {text}
+ </ListItem>
+ </List>
+ </>
+ );
+};
diff --git a/public/pages/ShowPost/components/TagsPanel.tsx b/public/pages/ShowPost/components/TagsPanel.tsx
index 490f2e55f..4aa7b2b3d 100644
--- a/public/pages/ShowPost/components/TagsPanel.tsx
+++ b/public/pages/ShowPost/components/TagsPanel.tsx
@@ -1,100 +1,85 @@
-import React from "react";
+import React, { useState } from "react";
import { Tag, Post } from "@fider/models";
-import { actions, Fider } from "@fider/services";
+import { actions } from "@fider/services";
import { ShowTag, List, ListItem } from "@fider/components";
import { TagListItem } from "./TagListItem";
import { FaCheckCircle, FaCog } from "react-icons/fa";
+import { useFider } from "@fider/hooks";
interface TagsPanelProps {
post: Post;
tags: Tag[];
}
-interface TagsPanelState {
- canEdit: boolean;
- assignedTags: Tag[];
- isEditing: boolean;
-}
+export const TagsPanel = (props: TagsPanelProps) => {
+ const fider = useFider();
+ const canEdit = fider.session.isAuthenticated && fider.session.user.isCollaborator && props.tags.length > 0;
-export class TagsPanel extends React.Component<TagsPanelProps, TagsPanelState> {
- constructor(props: TagsPanelProps) {
- super(props);
- this.state = {
- canEdit: Fider.session.isAuthenticated && Fider.session.user.isCollaborator && this.props.tags.length > 0,
- isEditing: false,
- assignedTags: this.props.tags.filter(t => this.props.post.tags.indexOf(t.slug) >= 0)
- };
- }
+ const [isEditing, setIsEditing] = useState(false);
+ const [assignedTags, setAssignedTags] = useState(props.tags.filter(t => props.post.tags.indexOf(t.slug) >= 0));
+
+ const assignOrUnassignTag = async (tag: Tag) => {
+ const idx = assignedTags.indexOf(tag);
+ let nextAssignedTags: Tag[] = [];
- private assignOrUnassignTag = async (tag: Tag) => {
- const idx = this.state.assignedTags.indexOf(tag);
- let assignedTags: Tag[] = [];
if (idx >= 0) {
- const response = await actions.unassignTag(tag.slug, this.props.post.number);
+ const response = await actions.unassignTag(tag.slug, props.post.number);
if (response.ok) {
- assignedTags = this.state.assignedTags.splice(idx, 1) && this.state.assignedTags;
+ nextAssignedTags = [...assignedTags];
+ nextAssignedTags.splice(idx, 1);
}
} else {
- const response = await actions.assignTag(tag.slug, this.props.post.number);
+ const response = await actions.assignTag(tag.slug, props.post.number);
if (response.ok) {
- assignedTags = this.state.assignedTags.concat(tag);
+ nextAssignedTags = [...assignedTags, tag];
}
}
- this.setState({
- assignedTags
- });
+ setAssignedTags(nextAssignedTags);
};
- private onSubtitleClick = () => {
- if (this.state.canEdit) {
- this.setState({ isEditing: !this.state.isEditing });
+ const onSubtitleClick = () => {
+ if (canEdit) {
+ setIsEditing(!isEditing);
}
};
- public render() {
- if (!this.state.canEdit && this.state.assignedTags.length === 0) {
- return null;
- }
-
- const tagsList =
- this.state.assignedTags.length > 0 ? (
- <List className="c-tag-list">
- {this.state.assignedTags.map(tag => (
- <ListItem key={tag.id}>
- <ShowTag tag={tag} />
- </ListItem>
- ))}
- </List>
- ) : (
- <span className="info">None yet</span>
- );
+ if (!canEdit && assignedTags.length === 0) {
+ return null;
+ }
- const editTagsList = this.props.tags.length > 0 && (
+ const tagsList =
+ assignedTags.length > 0 ? (
<List className="c-tag-list">
- {this.props.tags.map(tag => (
- <TagListItem
- key={tag.id}
- tag={tag}
- assigned={this.state.assignedTags.indexOf(tag) >= 0}
- onClick={this.assignOrUnassignTag}
- />
+ {assignedTags.map(tag => (
+ <ListItem key={tag.id}>
+ <ShowTag tag={tag} />
+ </ListItem>
))}
</List>
+ ) : (
+ <span className="info">None yet</span>
);
- const subtitleClasses = `subtitle ${this.state.canEdit && "active"}`;
- const icon = this.state.canEdit && (this.state.isEditing ? <FaCheckCircle /> : <FaCog />);
+ const editTagsList = props.tags.length > 0 && (
+ <List className="c-tag-list">
+ {props.tags.map(tag => (
+ <TagListItem key={tag.id} tag={tag} assigned={assignedTags.indexOf(tag) >= 0} onClick={assignOrUnassignTag} />
+ ))}
+ </List>
+ );
- return (
- <>
- <span className={subtitleClasses} onClick={this.onSubtitleClick}>
- Tags {icon}
- </span>
+ const subtitleClasses = `subtitle ${canEdit && "active"}`;
+ const icon = canEdit && (isEditing ? <FaCheckCircle /> : <FaCog />);
- {!this.state.isEditing && tagsList}
- {this.state.isEditing && editTagsList}
- </>
- );
- }
-}
+ return (
+ <>
+ <span className={subtitleClasses} onClick={onSubtitleClick}>
+ Tags {icon}
+ </span>
+
+ {!isEditing && tagsList}
+ {isEditing && editTagsList}
+ </>
+ );
+};