From 877171b2ea25e46dfab19dfd5346f7a52ac14a11 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 5 Aug 2020 20:33:07 +0300 Subject: [PATCH] App: cleanup the component and make sync add to fetch count. --- src/App.tsx | 51 +++++++++++++++---------------------------- src/store/reducers.ts | 1 + 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c2d1896..4591c13 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,8 +2,7 @@ // SPDX-License-Identifier: AGPL-3.0-only import * as React from "react"; -import { List as ImmutableList } from "immutable"; -import { connect, useDispatch } from "react-redux"; +import { useDispatch, useSelector } from "react-redux"; import { useHistory, useLocation } from "react-router"; import { BrowserRouter } from "react-router-dom"; import { MuiThemeProvider as ThemeProvider, createMuiTheme } from "@material-ui/core/styles"; // v1.x @@ -36,7 +35,8 @@ import { RouteResolver } from "./routes"; import * as store from "./store"; import * as actions from "./store/actions"; -import { credentialsSelector } from "./login"; +import { useCredentials } from "./credentials"; +import { SyncManager } from "./sync/SyncManager"; export const routeResolver = new RouteResolver({ home: "", @@ -145,26 +145,25 @@ function AppBarWitHistory(props: AppBarPropsType) { const IconRefreshWithSpin = withSpin(NavigationRefresh); -interface PropsType { - credentials: store.CredentialsData; - entries: store.EntriesData; - fetchCount: number; - darkMode: boolean; - errors: ImmutableList; -} - -function App(props: PropsType) { +export default function App() { const [drawerOpen, setDrawerOpen] = React.useState(false); const [errorsDialog, setErrorsDialog] = React.useState(false); const dispatch = useDispatch(); + const etebase = useCredentials(); + const darkMode = useSelector((state: store.StoreState) => state.settings.darkMode); + const fetchCount = useSelector((state: store.StoreState) => state.fetchCount); + const errors = useSelector((state: store.StoreState) => state.errors); - function refresh() { - dispatch(actions.fetchAll(props.credentials, props.entries)); + async function refresh() { + const syncManager = SyncManager.getManager(etebase!); + const sync = syncManager.sync(); + dispatch(actions.performSync(sync)); + await sync; } function autoRefresh() { - if (navigator.onLine && props.credentials && - !(window.location.pathname.match(/.*\/(new|edit|duplicate)$/))) { + if (navigator.onLine && etebase && + !(window.location.pathname.match(/.*\/(new|edit|duplicate)$/))) { refresh(); } } @@ -183,11 +182,9 @@ function App(props: PropsType) { setDrawerOpen(false); } - const credentials = props.credentials ?? null; - const { darkMode } = props; + const credentials = etebase ?? null; - const errors = props.errors; - const fetching = props.fetchCount > 0; + const fetching = fetchCount > 0; const muiTheme = createMuiTheme({ palette: { @@ -269,17 +266,3 @@ function App(props: PropsType) { ); } - -const mapStateToProps = (state: store.StoreState) => { - return { - credentials: credentialsSelector(state), - entries: state.cache.entries, - fetchCount: state.fetchCount, - darkMode: !!state.settings.darkMode, - errors: state.errors, - }; -}; - -export default connect( - mapStateToProps -)(App as any); diff --git a/src/store/reducers.ts b/src/store/reducers.ts index 5244368..2807223 100644 --- a/src/store/reducers.ts +++ b/src/store/reducers.ts @@ -319,6 +319,7 @@ for (const func in actions) { // Indicates network activity, not just fetch export const fetchCount = handleAction( combineActions( + actions.performSync.toString(), ...fetchActions ), (state: number, action: any) => {