From 2eb1aa1475f1e38bcf050bd0bfb52d7c3a0a50ee Mon Sep 17 00:00:00 2001 From: Saumya Pandey Date: Wed, 10 Feb 2021 02:08:54 +0530 Subject: [PATCH] Covert Collections.js to a functional component --- .../Sidebar/components/Collections.js | 102 ++++++++---------- 1 file changed, 43 insertions(+), 59 deletions(-) diff --git a/app/components/Sidebar/components/Collections.js b/app/components/Sidebar/components/Collections.js index 14d9cdc8ee..f7555bd738 100644 --- a/app/components/Sidebar/components/Collections.js +++ b/app/components/Sidebar/components/Collections.js @@ -1,82 +1,66 @@ // @flow -import { observer, inject } from "mobx-react"; +import { observer } from "mobx-react"; import { PlusIcon } from "outline-icons"; import * as React from "react"; -import { withTranslation, type TFunction } from "react-i18next"; -import CollectionsStore from "stores/CollectionsStore"; -import DocumentsStore from "stores/DocumentsStore"; -import PoliciesStore from "stores/PoliciesStore"; -import UiStore from "stores/UiStore"; +import { useTranslation } from "react-i18next"; import Fade from "components/Fade"; import Flex from "components/Flex"; +import useStores from "../../../hooks/useStores"; import CollectionLink from "./CollectionLink"; import CollectionsLoading from "./CollectionsLoading"; import Header from "./Header"; import SidebarLink from "./SidebarLink"; type Props = { - policies: PoliciesStore, - collections: CollectionsStore, - documents: DocumentsStore, onCreateCollection: () => void, - ui: UiStore, - t: TFunction, }; -@observer -class Collections extends React.Component { - isPreloaded: boolean = !!this.props.collections.orderedData.length; - - componentDidMount() { - const { collections } = this.props; - +function Collections({ onCreateCollection }: Props) { + const { ui, policies, documents, collections } = useStores(); + const isPreloaded: boolean = !!collections.orderedData.length; + const { t } = useTranslation(); + React.useEffect(() => { if (!collections.isFetching && !collections.isLoaded) { collections.fetchPage({ limit: 100 }); } - } + }); - render() { - const { collections, ui, policies, documents, t } = this.props; - - const content = ( - <> - {collections.orderedData.map((collection) => ( - - ))} - } - label={`${t("New collection")}…`} - exact + const content = ( + <> + {collections.orderedData.map((collection) => ( + - - ); + ))} + } + label={`${t("New collection")}…`} + exact + /> + + ); - return ( - -
{t("Collections")}
- {collections.isLoaded ? ( - this.isPreloaded ? ( - content - ) : ( - {content} - ) + return ( + +
{t("Collections")}
+ {collections.isLoaded ? ( + isPreloaded ? ( + content ) : ( - - )} -
- ); - } + {content} + ) + ) : ( + + )} +
+ ); } -export default withTranslation()( - inject("collections", "ui", "documents", "policies")(Collections) -); +export default observer(Collections);