0

I want to conditionally render the make call button inline based on null value. If the Button makeCall() has a null value. I do not want it to display. If it has a string value then i want the button to display. I know it should look something like this:

{ fixMeTruthyConditionToCauseDisplay && <Button {...makeTestAttributes('contactSupport_call_button')} preset="secondary" onPress={() => makeCall(configStore.phone || '')} tx="contactSupport.call" /> }

but am not quite sure how to implement it.

full code here:

export const ContactSupport = observer(({ navigation }: StackScreenProps<AccountParamList, 'contactSupport'>) => {
  const theme = useTheme();
  const styles = makeStyles(theme);
  const { configStore } = useStores();

  const makeCall = (phone: string) => {
    const phoneLink = `tel:${phone || ''}`;
    Linking.canOpenURL(phoneLink)
      .then(() => Linking.openURL(phoneLink))
      .catch(() =>
        Alert.alert('', configStore.supportEmail ? translate('contactSupport.phoneMissingError', { supportEmail: configStore.supportEmail }) : translate('contactSupport.emailAndPhoneMissingError')),
      );
  };

  const sendEmail = (to: string) => {
    if (!to) {
      if (configStore.phone) {
        Alert.alert('', translate('contactSupport.emailMissingError', { supportPhone: configStore.phone || '' }));
      } else {
        Alert.alert('', translate('contactSupport.emailAndPhoneMissingError'));
      }
    } else {
      const emailLink = `mailto:${to}`;

      Linking.canOpenURL(emailLink)
        .then(() => Linking.openURL(emailLink))
        .catch(() => Alert.alert('', translate('contactSupport.emailClientError', { supportEmail: configStore.supportEmail || '' })));
    }
  }; 

return (
<Screen
  {...makeTestAttributes('contactSupport')}
  header={<Header headerTx="contactSupport.header" leftButtons={[{ preset: 'back', onPress: () => navigation.goBack() }]} />}
  footer={
    <>
      <Button {...makeTestAttributes('contactSupport_call_button')} preset="secondary" onPress={() => makeCall(configStore.phone || '')} tx="contactSupport.call" />
      <Button onPress={() => sendEmail(configStore.supportEmail || '')} {...makeTestAttributes('contactSupport_email_button')} tx="contactSupport.email" />
    </>
  }
  preset="fixed"
  style={styles.container}
>
3
  • If you don't know when the button should be displayed how could we know? Commented Oct 20, 2022 at 17:24
  • @Konrad Linkowski thank you just made an edit to clarify Commented Oct 20, 2022 at 17:30
  • It's bad practice to test for a negative condition. For string, you can use something like: if( (typeof string) && value !== null) ...blah. Commented Oct 20, 2022 at 18:38

1 Answer 1

0

Use configStore.phone as the condition for rendering your button. Something like this should work:

export const ContactSupport = observer(({ navigation }: StackScreenProps<AccountParamList, 'contactSupport'>) => {
  const theme = useTheme();
  const styles = makeStyles(theme);
  const { configStore } = useStores();

  const makeCall = (phone: string) => {
    const phoneLink = `tel:${phone || ''}`;
    Linking.canOpenURL(phoneLink)
      .then(() => Linking.openURL(phoneLink))
      .catch(() =>
        Alert.alert('', configStore.supportEmail ? translate('contactSupport.phoneMissingError', { supportEmail: configStore.supportEmail }) : translate('contactSupport.emailAndPhoneMissingError')),
      );
  };

  const sendEmail = (to: string) => {
    if (!to) {
      if (configStore.phone) {
        Alert.alert('', translate('contactSupport.emailMissingError', { supportPhone: configStore.phone || '' }));
      } else {
        Alert.alert('', translate('contactSupport.emailAndPhoneMissingError'));
      }
    } else {
      const emailLink = `mailto:${to}`;

      Linking.canOpenURL(emailLink)
        .then(() => Linking.openURL(emailLink))
        .catch(() => Alert.alert('', translate('contactSupport.emailClientError', { supportEmail: configStore.supportEmail || '' })));
    }
  }; 

return (
<Screen
  {...makeTestAttributes('contactSupport')}
  header={<Header headerTx="contactSupport.header" leftButtons={[{ preset: 'back', onPress: () => navigation.goBack() }]} />}
  footer={
    <>
      {configStore.phone !== null && <Button {...makeTestAttributes('contactSupport_call_button')} preset="secondary" onPress={() => makeCall(configStore.phone || '')} tx="contactSupport.call" />}
      <Button onPress={() => sendEmail(configStore.supportEmail || '')} {...makeTestAttributes('contactSupport_email_button')} tx="contactSupport.email" />
    </>
  }
  preset="fixed"
  style={styles.container}
>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.