Skip to content

Commit

Permalink
fix: resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kubabutkiewicz committed Nov 30, 2023
1 parent 3164601 commit 26782f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/components/MessagesRow.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React from 'react';
import {StyleProp, View, ViewStyle} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import * as Localize from '@libs/Localize';
import useThemeStyles from '@styles/useThemeStyles';
import CONST from '@src/CONST';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import DotIndicatorMessage from './DotIndicatorMessage';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
Expand All @@ -11,7 +13,7 @@ import Tooltip from './Tooltip';

type MessagesRowProps = {
/** The messages to display */
messages: Record<string, string>;
messages: Record<string, Localize.MaybePhraseKey>;

/** The type of message, 'error' shows a red dot, 'success' shows a green dot */
type: 'error' | 'success';
Expand All @@ -30,7 +32,7 @@ function MessagesRow({messages = {}, type, onClose = () => {}, containerStyles,
const styles = useThemeStyles();
const {translate} = useLocalize();

if (Object.keys(messages).length === 0) {
if (isEmptyObject(messages)) {
return null;
}

Expand Down
28 changes: 16 additions & 12 deletions src/components/OfflineWithFeedback.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import lodashOmitBy from 'lodash/omitBy';
import React from 'react';
import {ImageStyle, StyleProp, TextStyle, View, ViewStyle} from 'react-native';
import useNetwork from '@hooks/useNetwork';
Expand All @@ -8,7 +7,7 @@ import useThemeStyles from '@styles/useThemeStyles';
import CONST from '@src/CONST';
import * as OnyxCommon from '@src/types/onyx/OnyxCommon';
import ChildrenProps from '@src/types/utils/ChildrenProps';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import {isNotEmptyObject} from '@src/types/utils/EmptyObject';
import MessagesRow from './MessagesRow';

/**
Expand All @@ -19,13 +18,13 @@ import MessagesRow from './MessagesRow';

type OfflineWithFeedbackProps = ChildrenProps & {
/** The type of action that's pending */
pendingAction: OnyxCommon.PendingAction | null;
pendingAction: OnyxCommon.PendingAction;

/** Determine whether to hide the component's children if deletion is pending */
shouldHideOnDelete?: boolean;

/** The errors to display */
errors?: OnyxCommon.Errors | null;
errors?: OnyxCommon.Errors;

/** Whether we should show the error messages */
shouldShowErrorMessages?: boolean;
Expand Down Expand Up @@ -60,7 +59,7 @@ type StrikethroughProps = Partial<ChildrenProps> & {style: Array<ViewStyle | Tex
/**
* This method applies the strikethrough to all the children passed recursively
*/
function applyStrikeThrough(children: React.ReactNode, styles: any): React.ReactNode {
function applyStrikeThrough(children: React.ReactNode, styles: ReturnType<typeof useThemeStyles>): React.ReactNode {
return React.Children.map(children, (child) => {
if (!React.isValidElement(child)) {
return child;
Expand All @@ -78,35 +77,40 @@ function applyStrikeThrough(children: React.ReactNode, styles: any): React.React
});
}

function omitBy<T>(obj: Record<string, T> | undefined, predicate: (value: T) => boolean) {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars
return Object.fromEntries(Object.entries(obj ?? {}).filter(([_, value]) => !predicate(value)));
}

function OfflineWithFeedback({
pendingAction = null,
pendingAction,
canDismissError = true,
contentContainerStyle,
errorRowStyles,
errors = null,
errors,
needsOffscreenAlphaCompositing = false,
onClose = () => {},
shouldDisableOpacity = false,
shouldDisableStrikeThrough = false,
shouldHideOnDelete = true,
shouldShowErrorMessages = true,
style,
...props
...rest
}: OfflineWithFeedbackProps) {
const styles = useThemeStyles();
const {isOffline} = useNetwork();

const hasErrors = !isEmptyObject(errors);
const hasErrors = isNotEmptyObject(errors ?? {});
// Some errors have a null message. This is used to apply opacity only and to avoid showing redundant messages.
const errorMessages = lodashOmitBy(errors, (e) => e === null);
const hasErrorMessages = !isEmptyObject(errorMessages);
const errorMessages = omitBy(errors, (e) => e === null);
const hasErrorMessages = isNotEmptyObject(errorMessages);
const isOfflinePendingAction = !!isOffline && !!pendingAction;
const isUpdateOrDeleteError = hasErrors && (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
const isAddError = hasErrors && pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD;
const needsOpacity = !shouldDisableOpacity && ((isOfflinePendingAction && !isUpdateOrDeleteError) || isAddError);
const needsStrikeThrough = !shouldDisableStrikeThrough && isOffline && pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
const hideChildren = shouldHideOnDelete && !isOffline && pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !hasErrors;
let children = props.children;
let children = rest.children;

// Apply strikethrough to children if needed, but skip it if we are not going to render them
if (needsStrikeThrough && !hideChildren) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/utils/EmptyObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function isNotEmptyObject<T extends Record<string, unknown> | Falsy>(arg: T | Em
return Object.keys(arg ?? {}).length > 0;
}

function isEmptyObject<T extends Record<string, unknown> | Falsy>(obj: T): boolean {
function isEmptyObject<T>(obj: T): boolean {
return Object.keys(obj ?? {}).length === 0;
}

Expand Down

0 comments on commit 26782f9

Please sign in to comment.