Skip to content

Commit 32a127d

Browse files
committed
UserStatusScreen [nfc]: Pull out status <-> input-value converters
These are nice and simple, and we could probably do fine without them. But it sets a good pattern for the emoji input, coming soon.
1 parent 2ce27e5 commit 32a127d

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/user-statuses/UserStatusScreen.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Screen from '../common/Screen';
1414
import ZulipButton from '../common/ZulipButton';
1515
import { getAuth, getOwnUserId } from '../selectors';
1616
import { getUserStatus } from './userStatusesModel';
17+
import type { UserStatus } from '../api/modelTypes';
1718
import { IconCancel, IconDone } from '../common/Icons';
1819
import statusSuggestions from './userStatusTextSuggestions';
1920
import * as api from '../api';
@@ -36,14 +37,20 @@ type Props = $ReadOnly<{|
3637
route: RouteProp<'user-status', void>,
3738
|}>;
3839

40+
const statusTextFromInputValue = (v: string): $PropertyType<UserStatus, 'status_text'> => v || null;
41+
42+
const inputValueFromStatusText = (t: $PropertyType<UserStatus, 'status_text'>): string => t ?? '';
43+
3944
export default function UserStatusScreen(props: Props): Node {
4045
const { navigation } = props;
4146

4247
const auth = useSelector(getAuth);
4348
const ownUserId = useSelector(getOwnUserId);
4449
const userStatusText = useSelector(state => getUserStatus(state, ownUserId).status_text);
4550

46-
const [textInputValue, setTextInputValue] = useState<string>(userStatusText ?? '');
51+
const [textInputValue, setTextInputValue] = useState<string>(
52+
inputValueFromStatusText(userStatusText),
53+
);
4754
const _ = useContext(TranslationContext);
4855

4956
const sendToServer = useCallback(
@@ -55,11 +62,11 @@ export default function UserStatusScreen(props: Props): Node {
5562
);
5663

5764
const handlePressUpdate = useCallback(() => {
58-
sendToServer({ status_text: textInputValue });
65+
sendToServer({ status_text: statusTextFromInputValue(textInputValue) });
5966
}, [textInputValue, sendToServer]);
6067

6168
const handlePressClear = useCallback(() => {
62-
setTextInputValue('');
69+
setTextInputValue(inputValueFromStatusText(null));
6370
sendToServer({ status_text: null });
6471
}, [sendToServer]);
6572

@@ -82,7 +89,7 @@ export default function UserStatusScreen(props: Props): Node {
8289
key={item}
8390
itemKey={item}
8491
title={item}
85-
selected={item === textInputValue}
92+
selected={item === statusTextFromInputValue(textInputValue)}
8693
onRequestSelectionChange={itemKey => {
8794
setTextInputValue(_(itemKey));
8895
}}

0 commit comments

Comments
 (0)