From 1cf5f7cfc57aad414becd04b7a204047386a95b9 Mon Sep 17 00:00:00 2001 From: ConjuringCoffee <72548231+ConjuringCoffee@users.noreply.github.com> Date: Sat, 18 Nov 2023 17:22:10 +0100 Subject: [PATCH] Fix code smells (#154) * Fix various code smells * Rename label for development channel * Encapsulate nested React components --- src/Hooks/useInitialFetchStatus.ts | 7 ++----- src/Screens/AmountsScreen/AmountView.tsx | 1 - src/Screens/AmountsScreen/SplitPercentInput.tsx | 2 +- src/Screens/InitialScreen/AccessTokenListItem.tsx | 12 +++++++----- src/Screens/InitialScreen/ProfileListItem.tsx | 12 +++++++----- .../CategoryCombos/ChooseCategoryListItem.tsx | 10 +++++++--- .../CreateCategoryComboScreen.tsx | 2 +- .../EditCategoryComboScreen.tsx | 2 +- .../DevelopmentSettingsScreen.tsx | 12 +++++------- .../Settings/Profiles/ChooseBudgetListItem.tsx | 11 +++++++++-- 10 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/Hooks/useInitialFetchStatus.ts b/src/Hooks/useInitialFetchStatus.ts index f7ea82a..1f2d28b 100644 --- a/src/Hooks/useInitialFetchStatus.ts +++ b/src/Hooks/useInitialFetchStatus.ts @@ -81,14 +81,11 @@ export const useInitialFetchStatus = (): [InitialFetchStatus] => { useEffect( () => { if (!localLoaded || connectionStatus.status === LoadingStatus.IDLE || connectionStatus.status === LoadingStatus.LOADING) { - // Still unknown return; } else if (connectionStatus.status === LoadingStatus.ERROR || profiles.length === 0) { setInitialFetchStatus(InitialFetchStatus.SETUP_REQUIRED); - } else { - if (budgetsFetchStatus.status === LoadingStatus.SUCCESSFUL) { - setInitialFetchStatus(InitialFetchStatus.READY); - } + } else if (budgetsFetchStatus.status === LoadingStatus.SUCCESSFUL) { + setInitialFetchStatus(InitialFetchStatus.READY); } }, [localLoaded, connectionStatus, profiles, budgetsFetchStatus, initialFetchStatus], diff --git a/src/Screens/AmountsScreen/AmountView.tsx b/src/Screens/AmountsScreen/AmountView.tsx index 435a0e6..4cd5d23 100644 --- a/src/Screens/AmountsScreen/AmountView.tsx +++ b/src/Screens/AmountsScreen/AmountView.tsx @@ -11,7 +11,6 @@ import { MyStackNavigationProp, StackParameterList } from '../../Navigation/Scre import { selectCategoryCombos } from '../../redux/features/categoryCombos/categoryCombosSlice'; type Props = { - key: number, index: number, amountText: string, payerBudgetId: string, diff --git a/src/Screens/AmountsScreen/SplitPercentInput.tsx b/src/Screens/AmountsScreen/SplitPercentInput.tsx index e14163c..04c3aa9 100644 --- a/src/Screens/AmountsScreen/SplitPercentInput.tsx +++ b/src/Screens/AmountsScreen/SplitPercentInput.tsx @@ -18,7 +18,7 @@ export const SplitPercentInput = ({ setSplitPercentToPayer, ...props }: Props) = return null; } - const value = String(props.splitPercentToPayer || ''); + const value = String(props.splitPercentToPayer ?? ''); return ( ({ naviga [navigation], ); - const icon = useMemo( - () => connectionStatus.status === LoadingStatus.SUCCESSFUL ? ICON_CONNECTION_OK : ICON_CONNECTION_NOT_OK, + const listIcon = useCallback( + (props: object) => { + const icon = connectionStatus.status === LoadingStatus.SUCCESSFUL ? ICON_CONNECTION_OK : ICON_CONNECTION_NOT_OK; + return ; + }, [connectionStatus], ); return ( } + left={listIcon} onPress={navigateToAccessTokenScreen} /> ); diff --git a/src/Screens/InitialScreen/ProfileListItem.tsx b/src/Screens/InitialScreen/ProfileListItem.tsx index c02dd88..882244c 100644 --- a/src/Screens/InitialScreen/ProfileListItem.tsx +++ b/src/Screens/InitialScreen/ProfileListItem.tsx @@ -1,4 +1,4 @@ -import { useCallback, useMemo } from 'react'; +import { useCallback } from 'react'; import { List } from 'react-native-paper'; import { useAppSelector } from '../../Hooks/useAppSelector'; import { ScreenNames } from '../../Navigation/ScreenNames'; @@ -27,16 +27,18 @@ export const ProfileListItem = ({ navigation [navigation, profiles], ); - const icon = useMemo( - () => profiles.length ? ICON_PROFILE_EXISTS : ICON_PROFILE_MISSING, + const listIcon = useCallback( + (props: object) => { + const icon = profiles.length ? ICON_PROFILE_EXISTS : ICON_PROFILE_MISSING; + return ; + }, [profiles], ); return ( } + left={listIcon} onPress={navigateToProfileSettings} /> ); diff --git a/src/Screens/Settings/CategoryCombos/ChooseCategoryListItem.tsx b/src/Screens/Settings/CategoryCombos/ChooseCategoryListItem.tsx index 577cf28..9e7a25e 100644 --- a/src/Screens/Settings/CategoryCombos/ChooseCategoryListItem.tsx +++ b/src/Screens/Settings/CategoryCombos/ChooseCategoryListItem.tsx @@ -1,4 +1,4 @@ -import { useMemo } from 'react'; +import { useCallback, useMemo } from 'react'; import { List } from 'react-native-paper'; import { ScreenNames } from '../../../Navigation/ScreenNames'; import { MyStackNavigationProp, StackParameterList } from '../../../Navigation/ScreenParameters'; @@ -50,12 +50,16 @@ export const ChooseCategoryListItem = ({ sel [categoryName, selectedCategoryId], ); + const listIcon = useCallback( + (props: object) => (), + [icon], + ); + return ( } + left={listIcon} onPress={() => { props.navigation.navigate(ScreenNames.CATEGORY_SCREEN, { budgetId: props.budgetId, diff --git a/src/Screens/Settings/CategoryCombos/CreateCategoryComboScreen/CreateCategoryComboScreen.tsx b/src/Screens/Settings/CategoryCombos/CreateCategoryComboScreen/CreateCategoryComboScreen.tsx index f0bc195..46a1f7b 100644 --- a/src/Screens/Settings/CategoryCombos/CreateCategoryComboScreen/CreateCategoryComboScreen.tsx +++ b/src/Screens/Settings/CategoryCombos/CreateCategoryComboScreen/CreateCategoryComboScreen.tsx @@ -26,7 +26,7 @@ export const CreateCategoryComboScreen = ({ navigation }: MyStackScreenProps(''); const [categoryIdFirstProfile, setCategoryIdFirstProfile] = useState(undefined); const [categoryIdSecondProfile, setCategoryIdSecondProfile] = useState(undefined); - const readyToSave = name.length > 0 && categoryIdFirstProfile && categoryIdSecondProfile ? true : false; + const readyToSave = name.length > 0 && categoryIdFirstProfile; const saveAndNavigate = useCallback( () => { diff --git a/src/Screens/Settings/CategoryCombos/EditCategoryComboScreen/EditCategoryComboScreen.tsx b/src/Screens/Settings/CategoryCombos/EditCategoryComboScreen/EditCategoryComboScreen.tsx index bb262fb..efc5828 100644 --- a/src/Screens/Settings/CategoryCombos/EditCategoryComboScreen/EditCategoryComboScreen.tsx +++ b/src/Screens/Settings/CategoryCombos/EditCategoryComboScreen/EditCategoryComboScreen.tsx @@ -32,7 +32,7 @@ export const EditCategoryComboScreen = ({ navigation, route }: MyStackScreenProp const [navigateBack] = useNavigateBack(navigation); - const readyToSave = name.length > 0 && categoryIdFirstProfile && categoryIdSecondProfile ? true : false; + const readyToSave = name.length > 0 && categoryIdFirstProfile && categoryIdSecondProfile; const onSelectDeletion = useCallback( (): void => { diff --git a/src/Screens/Settings/DevelopmentSettingsScreen/DevelopmentSettingsScreen.tsx b/src/Screens/Settings/DevelopmentSettingsScreen/DevelopmentSettingsScreen.tsx index f41eb8b..7710b2a 100644 --- a/src/Screens/Settings/DevelopmentSettingsScreen/DevelopmentSettingsScreen.tsx +++ b/src/Screens/Settings/DevelopmentSettingsScreen/DevelopmentSettingsScreen.tsx @@ -78,7 +78,7 @@ export const DevelopmentSettingsScreen = ({ navigation }: MyStackScreenProps { if (Updates.channel && Updates.channel.length > 0) { return Updates.channel; @@ -107,7 +107,7 @@ export const DevelopmentSettingsScreen = ({ navigation }: MyStackScreenProps latestUpdate && latestUpdate.isAvailable ? updateApp : undefined, + () => latestUpdate?.isAvailable ? updateApp : undefined, [latestUpdate, updateApp], ); @@ -115,8 +115,6 @@ export const DevelopmentSettingsScreen = ({ navigation }: MyStackScreenProps { await dispatch(deleteAllCategoryCombos()); await dispatch(deleteAllProfiles()); - // TODO: Delete access token - // TODO: Delete display settings }, [dispatch], ); @@ -130,8 +128,8 @@ export const DevelopmentSettingsScreen = ({ navigation }: MyStackScreenProps - Delete all data + Delete profiles and category combos ); diff --git a/src/Screens/Settings/Profiles/ChooseBudgetListItem.tsx b/src/Screens/Settings/Profiles/ChooseBudgetListItem.tsx index f493643..9b9ab63 100644 --- a/src/Screens/Settings/Profiles/ChooseBudgetListItem.tsx +++ b/src/Screens/Settings/Profiles/ChooseBudgetListItem.tsx @@ -28,12 +28,19 @@ export const ChooseBudgetListItem = (props: Props) => { const budgetName = props.selectedBudgetId ? budgets.find((budget) => budget.id === props.selectedBudgetId)?.name : undefined; + const listIcon = useCallback( + (props: object) => { + const icon = budgetName ? ICON_BUDGET_SET : ICON_BUDGET_NOT_SET; + return ; + }, + [budgetName], + ); + return ( <> } + left={listIcon} onPress={showModal} />