Skip to content

Commit

Permalink
Fix code smells (#154)
Browse files Browse the repository at this point in the history
* Fix various code smells

* Rename label for development channel

* Encapsulate nested React components
  • Loading branch information
ConjuringCoffee authored Nov 18, 2023
1 parent 18e3ac1 commit 1cf5f7c
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 31 deletions.
7 changes: 2 additions & 5 deletions src/Hooks/useInitialFetchStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
1 change: 0 additions & 1 deletion src/Screens/AmountsScreen/AmountView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { MyStackNavigationProp, StackParameterList } from '../../Navigation/Scre
import { selectCategoryCombos } from '../../redux/features/categoryCombos/categoryCombosSlice';

type Props<T extends keyof StackParameterList> = {
key: number,
index: number,
amountText: string,
payerBudgetId: string,
Expand Down
2 changes: 1 addition & 1 deletion src/Screens/AmountsScreen/SplitPercentInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const SplitPercentInput = ({ setSplitPercentToPayer, ...props }: Props) =
return null;
}

const value = String(props.splitPercentToPayer || '');
const value = String(props.splitPercentToPayer ?? '');

return (
<TextInput
Expand Down
12 changes: 7 additions & 5 deletions src/Screens/InitialScreen/AccessTokenListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo } from 'react';
import { useCallback } from 'react';
import { List } from 'react-native-paper';
import { LoadingStatus } from '../../Helper/LoadingStatus';
import { useAutomaticConnectionTest } from '../../Hooks/useAutomaticConnectionTest';
Expand All @@ -22,16 +22,18 @@ export const AccessTokenListItem = <T extends keyof StackParameterList>({ 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 <List.Icon {...props} icon={icon} />;
},
[connectionStatus],
);

return (
<List.Item
title='Connect YNAB account'
// eslint-disable-next-line react/no-unstable-nested-components
left={(props) => <List.Icon {...props} icon={icon} />}
left={listIcon}
onPress={navigateToAccessTokenScreen}
/>
);
Expand Down
12 changes: 7 additions & 5 deletions src/Screens/InitialScreen/ProfileListItem.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -27,16 +27,18 @@ export const ProfileListItem = <T extends keyof StackParameterList>({ 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 <List.Icon {...props} icon={icon} />;
},
[profiles],
);

return (
<List.Item
title='Set up profile'
// eslint-disable-next-line react/no-unstable-nested-components
left={(props) => <List.Icon {...props} icon={icon} />}
left={listIcon}
onPress={navigateToProfileSettings}
/>
);
Expand Down
10 changes: 7 additions & 3 deletions src/Screens/Settings/CategoryCombos/ChooseCategoryListItem.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -50,12 +50,16 @@ export const ChooseCategoryListItem = <T extends keyof StackParameterList>({ sel
[categoryName, selectedCategoryId],
);

const listIcon = useCallback(
(props: object) => (<List.Icon {...props} icon={icon} />),
[icon],
);

return (
<List.Item
title={title}
description={`Category from ${displayBudgetName}`}
// eslint-disable-next-line react/no-unstable-nested-components
left={(props) => <List.Icon {...props} icon={icon} />}
left={listIcon}
onPress={() => {
props.navigation.navigate(ScreenNames.CATEGORY_SCREEN, {
budgetId: props.budgetId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const CreateCategoryComboScreen = ({ navigation }: MyStackScreenProps<Scr
const [name, setName] = useState<string>('');
const [categoryIdFirstProfile, setCategoryIdFirstProfile] = useState<string | undefined>(undefined);
const [categoryIdSecondProfile, setCategoryIdSecondProfile] = useState<string | undefined>(undefined);
const readyToSave = name.length > 0 && categoryIdFirstProfile && categoryIdSecondProfile ? true : false;
const readyToSave = name.length > 0 && categoryIdFirstProfile;

const saveAndNavigate = useCallback(
() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const DevelopmentSettingsScreen = ({ navigation }: MyStackScreenProps<Scr
[isDevelopmentMode, latestUpdate],
);

const releaseChannel = useMemo(
const channel = useMemo(
(): string => {
if (Updates.channel && Updates.channel.length > 0) {
return Updates.channel;
Expand Down Expand Up @@ -107,16 +107,14 @@ export const DevelopmentSettingsScreen = ({ navigation }: MyStackScreenProps<Scr
);

const onPressUpdate = useMemo(
() => latestUpdate && latestUpdate.isAvailable ? updateApp : undefined,
() => latestUpdate?.isAvailable ? updateApp : undefined,
[latestUpdate, updateApp],
);

const onDeleteButtonPress = useCallback(
async () => {
await dispatch(deleteAllCategoryCombos());
await dispatch(deleteAllProfiles());
// TODO: Delete access token
// TODO: Delete display settings
},
[dispatch],
);
Expand All @@ -130,8 +128,8 @@ export const DevelopmentSettingsScreen = ({ navigation }: MyStackScreenProps<Scr
description={isDevelopmentMode ? 'Development' : 'Release'}
/>
<List.Item
title='Release Channel'
description={releaseChannel}
title='Channel'
description={channel}
/>
<List.Item
title='Date of current update'
Expand All @@ -147,7 +145,7 @@ export const DevelopmentSettingsScreen = ({ navigation }: MyStackScreenProps<Scr
mode='contained'
disabled={!isDevelopmentMode}
>
Delete all data
Delete profiles and category combos
</Button>
</ScrollView>
);
Expand Down
11 changes: 9 additions & 2 deletions src/Screens/Settings/Profiles/ChooseBudgetListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <List.Icon {...props} icon={icon} />;
},
[budgetName],
);

return (
<>
<List.Item
title={budgetName ?? 'No budget selected'}
// eslint-disable-next-line react/no-unstable-nested-components
left={(props) => <List.Icon {...props} icon={budgetName ? ICON_BUDGET_SET : ICON_BUDGET_NOT_SET} />}
left={listIcon}
onPress={showModal}
/>
<BudgetSelectModalPortal
Expand Down

0 comments on commit 1cf5f7c

Please sign in to comment.