diff --git a/lib/shared/relationship-utils.js b/lib/shared/relationship-utils.js --- a/lib/shared/relationship-utils.js +++ b/lib/shared/relationship-utils.js @@ -1,10 +1,14 @@ // @flow +import invariant from 'invariant'; + import { type RelationshipButton, type UserRelationshipStatus, userRelationshipStatus, relationshipButtons, + relationshipActions, + type RelationshipAction, } from '../types/relationship-types'; import type { UserInfo } from '../types/user-types'; @@ -51,8 +55,31 @@ ); } +function getRelationshipDispatchAction( + relationshipButton: RelationshipButton, +): RelationshipAction { + if (relationshipButton === relationshipButtons.BLOCK) { + return relationshipActions.BLOCK; + } else if ( + relationshipButton === relationshipButtons.FRIEND || + relationshipButton === relationshipButtons.ACCEPT + ) { + return relationshipActions.FRIEND; + } else if ( + relationshipButton === relationshipButtons.UNFRIEND || + relationshipButton === relationshipButtons.REJECT || + relationshipButton === relationshipButtons.WITHDRAW + ) { + return relationshipActions.UNFRIEND; + } else if (relationshipButton === relationshipButtons.UNBLOCK) { + return relationshipActions.UNBLOCK; + } + invariant(false, 'relationshipButton conditions should be exhaustive'); +} + export { sortIDs, getAvailableRelationshipButtons, relationshipBlockedInEitherDirection, + getRelationshipDispatchAction, }; diff --git a/native/chat/settings/thread-settings-edit-relationship.react.js b/native/chat/settings/thread-settings-edit-relationship.react.js --- a/native/chat/settings/thread-settings-edit-relationship.react.js +++ b/native/chat/settings/thread-settings-edit-relationship.react.js @@ -8,12 +8,12 @@ updateRelationships as serverUpdateRelationships, updateRelationshipsActionTypes, } from 'lib/actions/relationship-actions'; +import { getRelationshipDispatchAction } from 'lib/shared/relationship-utils'; import { getSingleOtherUser } from 'lib/shared/thread-utils'; import { type RelationshipAction, type RelationshipButton, relationshipButtons, - relationshipActions, } from 'lib/types/relationship-types'; import type { ThreadInfo } from 'lib/types/thread-types'; import { @@ -63,25 +63,10 @@ ); const { relationshipButton } = props; - const relationshipAction = React.useMemo(() => { - if (relationshipButton === relationshipButtons.BLOCK) { - return relationshipActions.BLOCK; - } else if ( - relationshipButton === relationshipButtons.FRIEND || - relationshipButton === relationshipButtons.ACCEPT - ) { - return relationshipActions.FRIEND; - } else if ( - relationshipButton === relationshipButtons.UNFRIEND || - relationshipButton === relationshipButtons.REJECT || - relationshipButton === relationshipButtons.WITHDRAW - ) { - return relationshipActions.UNFRIEND; - } else if (relationshipButton === relationshipButtons.UNBLOCK) { - return relationshipActions.UNBLOCK; - } - invariant(false, 'relationshipButton conditions should be exhaustive'); - }, [relationshipButton]); + const relationshipAction = React.useMemo( + () => getRelationshipDispatchAction(relationshipButton), + [relationshipButton], + ); const dispatchActionPromise = useDispatchActionPromise(); const onButtonPress = React.useCallback(() => {