Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize goBack feature for CollectionCreate #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/RootStackParamList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { StackNavigationProp } from "@react-navigation/stack";

export type RootStackParamList = {
Home: undefined;
Home: { colUid: string } | undefined;
Login: undefined;
Signup: undefined;
CollectionCreate: undefined;
Expand Down
18 changes: 6 additions & 12 deletions src/screens/CollectionEditScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import * as React from "react";
import { useSelector } from "react-redux";
import { TextInput as NativeTextInput } from "react-native";
import { HelperText, Paragraph } from "react-native-paper";
import { useNavigation, RouteProp, useNavigationState, CommonActions } from "@react-navigation/native";
import { useNavigation, RouteProp } from "@react-navigation/native";
import { StackNavigationProp } from "@react-navigation/stack";

import { useSyncGate } from "../SyncGate";
import { useCredentials } from "../credentials";
import { StoreState, useAsyncDispatch } from "../store";
import { collectionUpload, pushMessage } from "../store/actions";
import { collectionUpload, pushMessage, setActiveNotebook } from "../store/actions";

import TextInput from "../widgets/TextInput";
import ScrollView from "../widgets/ScrollView";
Expand Down Expand Up @@ -48,7 +48,6 @@ export default function CollectionEditScreen(props: PropsType) {
const cacheCollections = useSelector((state: StoreState) => state.cache.collections);
const syncGate = useSyncGate();
const navigation = useNavigation<NavigationProp>();
const navigationState = useNavigationState((state) => (state.index > 0) ? state.routes[state.index - 1] : null);
const etebase = useCredentials()!;
const [loading, error, setPromise] = useLoading();
const colType = C.colType;
Expand Down Expand Up @@ -126,17 +125,12 @@ export default function CollectionEditScreen(props: PropsType) {
navigation.goBack();
} else {
dispatch(pushMessage({ message: "Notebook created", severity: "success" }));
dispatch(setActiveNotebook({ meta, uid: collection.uid }));

const previousScreen = navigationState?.name;
if (navigationState && previousScreen === "NoteCreate") {
// We change the colUid
navigation.dispatch({
...CommonActions.setParams({ colUid: collection.uid }),
source: navigationState.key,
});
if (navigation.canGoBack()) {
navigation.goBack();
} else {
// We're gonna navigate to the notebook's page
navigation.replace("Collection", { colUid: collection.uid });
navigation.replace("Home");
}
}
});
Expand Down
25 changes: 13 additions & 12 deletions src/screens/NotebookListScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { StyleSheet, FlatList, Platform, View, BackHandler } from "react-native"
import { Appbar as PaperAppbar, List, FAB, Avatar } from "react-native-paper";
import { useNavigation, useFocusEffect } from "@react-navigation/native";
import { useDispatch, useSelector } from "react-redux";
import * as Etebase from "etebase";

import { useSyncGate } from "../SyncGate";
import { StoreState } from "../store";
import { Notebook, StoreState, useAsyncDispatch } from "../store";
import { SyncManager } from "../sync/SyncManager";
import { performSync } from "../store/actions";
import { performSync, setActiveNotebook } from "../store/actions";
import { useCredentials } from "../credentials";

import NoteList from "../components/NoteList";
Expand All @@ -26,25 +25,27 @@ interface PropsType {
active: boolean;
}

type Notebook = {
meta: Etebase.ItemMetadata;
uid: string;
};

export default function NotebookListScreen(props: PropsType) {
const cacheCollections = useSelector((state: StoreState) => state.cache.collections);
const notebook = useSelector((state: StoreState) => state.activeNotebook);
const notebooks: Notebook[] = React.useMemo(() => Array.from(cacheCollections
.sort((a, b) => (a.meta!.name!.toUpperCase() >= b.meta!.name!.toUpperCase()) ? 1 : -1)
.map(({ meta }, uid) => {return { meta, uid }})
.values()
), [cacheCollections]);
const navigation = useNavigation<DefaultNavigationProp>();
const dispatch = useAsyncDispatch();
const syncGate = useSyncGate();
const theme = useTheme();

const { colUid, active } = props;
const cacheCollection = (colUid) ? notebooks.find((col) => col.uid === colUid) : undefined;
const [notebook, setNotebook] = React.useState(cacheCollection);

React.useEffect(() => {
if (cacheCollection) {
dispatch(setActiveNotebook(cacheCollection));
}
}, [cacheCollection]);

React.useEffect(() => {
if (!active) {
Expand All @@ -54,7 +55,7 @@ export default function NotebookListScreen(props: PropsType) {
navigation.setOptions({
header: (props) => <Appbar {...props} menuFallback />,
title: notebook?.meta.name || "Notebooks",
headerLeft: (notebook) ? () => <PaperAppbar.BackAction onPress={() => setNotebook(undefined)} /> : undefined,
headerLeft: (notebook) ? () => <PaperAppbar.BackAction onPress={() => dispatch(setActiveNotebook(cacheCollection))} /> : undefined,
headerRight: () => (
<RightAction colUid={notebook?.uid} />
),
Expand All @@ -63,7 +64,7 @@ export default function NotebookListScreen(props: PropsType) {

const onBackPress = React.useCallback(() => {
if (active && notebook) {
setNotebook(undefined);
dispatch(setActiveNotebook(undefined));
return true;
} else {
return false;
Expand Down Expand Up @@ -95,7 +96,7 @@ export default function NotebookListScreen(props: PropsType) {
</View>
)}
onPress={() => {
setNotebook(item);
dispatch(setActiveNotebook(item));
}}
/>
);
Expand Down
13 changes: 12 additions & 1 deletion src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createAction as origCreateAction, ActionMeta } from "redux-actions";
import * as Etebase from "etebase";

import { ConnectionInfo, SettingsType } from "./";
import { Message } from "./reducers";
import { Message, Notebook } from "./reducers";

type FunctionAny = (...args: any[]) => any;

Expand Down Expand Up @@ -245,3 +245,14 @@ export const setSettings = createAction(
return { ...settings };
}
);

export const setActiveNotebook = createAction(
"SET_ACTIVE_NOTEBOOK",
(activeNotebook: Notebook | undefined) => {
if (activeNotebook) {
return { ...activeNotebook };
} else {
return null;
}
}
);
6 changes: 4 additions & 2 deletions src/store/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { List, Map as ImmutableMap } from "immutable";

import {
SettingsType,
fetchCount, syncCount, credentials, settingsReducer, syncStatusReducer, lastSyncReducer, connectionReducer, errorsReducer,
fetchCount, syncCount, credentials, settingsReducer, syncStatusReducer, lastSyncReducer, connectionReducer, errorsReducer, activeNotebookReducer,
CredentialsData, SyncCollectionsData, SyncGeneralData,
collections, items, syncCollections, syncItems, syncGeneral, CachedCollectionsData, CachedItemsData, SyncItemsData, messagesReducer, Message,
collections, items, syncCollections, syncItems, syncGeneral, CachedCollectionsData, CachedItemsData, SyncItemsData, messagesReducer, Message, Notebook,
} from "./reducers";

export interface StoreState {
Expand All @@ -39,6 +39,7 @@ export interface StoreState {
connection: NetInfoStateType | null;
errors: List<Error>;
messages: List<Message>;
activeNotebook: Notebook;
}

const settingsMigrations = {
Expand Down Expand Up @@ -168,6 +169,7 @@ const reducers = combineReducers({
connection: connectionReducer,
errors: errorsReducer,
messages: messagesReducer,
activeNotebook: activeNotebookReducer,
});

export default reducers;
14 changes: 14 additions & 0 deletions src/store/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,17 @@ export const settingsReducer = handleActions(
},
}
);

export type Notebook = {
meta: Etebase.ItemMetadata;
uid: string;
};

export const activeNotebookReducer = handleActions(
{
[actions.setActiveNotebook.toString()]: (_state: Notebook | null, action: Action<Notebook | null>) => {
return action.payload;
},
},
null
);