Skip to content

Commit

Permalink
Generalize goBack feature for CollectionCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
Kévin Commaille committed Jan 21, 2021
1 parent e52ea14 commit 58625b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/RootStackParamList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ export type RootStackParamList = {
Home: undefined;
Login: undefined;
Signup: undefined;
CollectionCreate: undefined;
CollectionCreate: {
goBack: boolean;
colUid?: undefined;
} | undefined;
Collection: { colUid: string };
CollectionEdit: { colUid: string };
CollectionEdit: {
colUid: string;
goBack?: boolean;
};
CollectionChangelog: { colUid: string };
CollectionMembers: { colUid: string };
NoteCreate: {
Expand Down
15 changes: 6 additions & 9 deletions src/screens/CollectionEditScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as React from "react";
import { useSelector } from "react-redux";
import { TextInput as NativeTextInput } from "react-native";
import { Text, HelperText, Button, Appbar, Paragraph } from "react-native-paper";
import { useNavigation, RouteProp, useNavigationState, CommonActions } from "@react-navigation/native";
import { useNavigation, RouteProp, useNavigationState } from "@react-navigation/native";
import { StackNavigationProp } from "@react-navigation/stack";

import { SyncManager } from "../sync/SyncManager";
Expand Down Expand Up @@ -47,7 +47,7 @@ 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 previousScreen = useNavigationState((state) => (state.index > 0) ? state.routes[state.index - 1].name as keyof RootStackParamList : undefined);
const etebase = useCredentials()!;
const [loading, error, setPromise] = useLoading();
const colType = C.colType;
Expand Down Expand Up @@ -126,13 +126,10 @@ export default function CollectionEditScreen(props: PropsType) {
} else {
dispatch(pushMessage({ message: "Notebook created", severity: "success" }));

const previousScreen = navigationState?.name;
if (navigationState && previousScreen === "NoteCreate") {
// We change the colUid
navigation.dispatch({
...CommonActions.setParams({ colUid: collection.uid }),
source: navigationState.key,
});
const goBack = props.route.params?.goBack;
if (goBack && previousScreen) {
// We send back the colUid
navigation.navigate(previousScreen, { colUid: collection.uid });
} else {
// We're gonna navigate to the notebook's page
navigation.replace("Collection", { colUid: collection.uid });
Expand Down
2 changes: 1 addition & 1 deletion src/screens/NotePropertiesScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export default function NotePropertiesScreen(props: PropsType) {
disabled={!!itemUid}
icon="plus"
iconAccessibilityLabel="Create Notebook"
iconOnPress={() => navigation.navigate("CollectionCreate")}
iconOnPress={() => navigation.navigate("CollectionCreate", { goBack: true })}
/>
</TouchableRipple>
)}
Expand Down

0 comments on commit 58625b3

Please sign in to comment.