Skip to content

Commit

Permalink
UserStatusScreen [nfc]: Pull out status <-> input-value converters
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chrisbobbe committed Mar 8, 2022
1 parent 2ad1611 commit 70a4c12
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/user-statuses/UserStatusScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Screen from '../common/Screen';
import ZulipButton from '../common/ZulipButton';
import { getAuth, getOwnUserId } from '../selectors';
import { getUserStatus } from './userStatusesModel';
import type { UserStatus } from '../api/modelTypes';
import { IconCancel, IconDone } from '../common/Icons';
import statusSuggestions from './userStatusTextSuggestions';
import * as api from '../api';
Expand All @@ -36,14 +37,20 @@ type Props = $ReadOnly<{|
route: RouteProp<'user-status', void>,
|}>;

const statusTextFromInputValue = (v: string): $PropertyType<UserStatus, 'status_text'> => v || null;

const inputValueFromStatusText = (t: $PropertyType<UserStatus, 'status_text'>): string => t ?? '';

export default function UserStatusScreen(props: Props): Node {
const { navigation } = props;

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

const [textInputValue, setTextInputValue] = useState<string>(userStatusText ?? '');
const [textInputValue, setTextInputValue] = useState<string>(
inputValueFromStatusText(userStatusText),
);
const _ = useContext(TranslationContext);

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

const handlePressUpdate = useCallback(() => {
sendToServer({ status_text: textInputValue });
sendToServer({ status_text: statusTextFromInputValue(textInputValue) });
}, [textInputValue, sendToServer]);

const handlePressClear = useCallback(() => {
setTextInputValue('');
setTextInputValue(inputValueFromStatusText(null));
sendToServer({ status_text: null });
}, [sendToServer]);

Expand All @@ -82,7 +89,7 @@ export default function UserStatusScreen(props: Props): Node {
key={item}
itemKey={item}
title={item}
selected={item === textInputValue}
selected={item === statusTextFromInputValue(textInputValue)}
onRequestSelectionChange={itemKey => {
setTextInputValue(_(itemKey));
}}
Expand Down

0 comments on commit 70a4c12

Please sign in to comment.