All files / apps/maintenance/src/screens/Preview index.tsx

100% Statements 33/33
91.66% Branches 11/12
100% Functions 11/11
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                                              1x 7x               7x         7x 7x 7x 7x 7x     7x 7x 7x       7x 7x   73x   7x   1x 1x         7x 2x 1x           7x 7x 7x   7x 7x 7x       7x                                                                           7x                                                                    
import { useCallback, useEffect, useMemo } from 'react';
import { ScrollView, Text, View } from 'react-native';
 
import { useFocusEffect } from '@react-navigation/native';
 
import { Button } from '@repo/ui/components/Button';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { textStyles } from '@repo/ui/themes/typography';
import { FAB_SUBMIT_GAP, useFabAnchor } from '@repo/ui/utils/useFabAnchor';
 
import { OFFICES } from '@repo/constants/offices';
 
import { useGetMeInfo } from '@repo/hooks/useUserQueries';
 
import { useCreateMaintenanceRequest } from '@/hooks/useCreateMaintenanceRequest';
import { useMaintenanceScopes } from '@/hooks/useMaintenanceScopes';
 
import {
  useMaintenanceActionsSelector,
  useMaintenanceStateSelector,
} from '@/screens/Maintenance/context';
import { useMaintenanceRuntime } from '@/screens/Maintenance/runtime';
 
const Preview = () => {
  const styles = useStyles();
  const {
    actions: {
      onSubmitSuccess,
      onSubmitError,
      onSubmittingChange,
      setFabOffset,
    },
  } = useMaintenanceRuntime();
  const {
    ref: submitRef,
    onLayout: onSubmitLayout,
    remeasure,
  } = useFabAnchor(setFabOffset, FAB_SUBMIT_GAP);
  useFocusEffect(remeasure);
  const form = useMaintenanceStateSelector(state => state.form);
  const setSubmitting = useMaintenanceActionsSelector(
    actions => actions.setSubmitting,
  );
 
  const { scopes } = useMaintenanceScopes();
  const scopeLabel = useMemo(
    () => scopes.find(option => option.value === form.scope)?.label ?? '',
    [scopes, form.scope],
  );
 
  const { data: meInfo } = useGetMeInfo();
  const location = meInfo?.room?.id ?? '';
  const locationLabel =
    OFFICES.find(office => office.id === location)?.text || location;
 
  const { mutate, isPending: isSubmitting } = useCreateMaintenanceRequest(
    () => {
      setSubmitting(false);
      onSubmitSuccess();
    },
    onSubmitError,
  );
 
  const handleSubmit = useCallback(() => {
    if (!form.scope) return;
    mutate({
      category: 'maintenance',
      data: { scope: form.scope, note: form.note ?? '', location },
    });
  }, [form.scope, form.note, location, mutate]);
 
  useEffect(() => {
    setSubmitting(isSubmitting);
    onSubmittingChange?.(isSubmitting);
 
    return () => {
      setSubmitting(false);
      onSubmittingChange?.(false);
    };
  }, [isSubmitting, setSubmitting, onSubmittingChange]);
 
  return (
    <>
      <ScrollView
        style={styles.container}
        contentContainerStyle={styles.content}
      >
        <Text style={styles.bodyText}>
          I would like to request{' '}
          <Text style={styles.highlightText}>{scopeLabel}</Text> maintenance for
          the following reason:{' '}
          <Text style={styles.highlightText}>{form.note?.toLowerCase()}</Text>.
        </Text>
        {location && (
          <Text style={styles.bodyText}>
            My current location is{' '}
            <Text style={styles.highlightText}>{locationLabel}.</Text>
          </Text>
        )}
        <Text style={styles.bodyText}>Thank you for your assistance!</Text>
      </ScrollView>
      <View ref={submitRef} onLayout={onSubmitLayout}>
        <Button
          onPress={handleSubmit}
          isLoading={isSubmitting}
          style={styles.submitButton}
          textStyle={styles.buttonText}
          accessibilityLabel="Submit"
          testID="submit-btn"
        >
          Submit
        </Button>
      </View>
    </>
  );
};
 
export default Preview;
 
const useStyles = makeStyles(theme => ({
  container: {
    flex: 1,
    paddingTop: theme.metrics.spacing[6],
  },
  content: {
    paddingHorizontal: theme.metrics.spacing[4],
    gap: theme.metrics.spacing[6.5],
    paddingBottom: theme.metrics.spacing[6],
  },
  bodyText: {
    ...textStyles.content.light,
    fontSize: theme.metrics.textSize[18],
    color: theme.colors.text.onBehalf,
  },
  highlightText: {
    ...textStyles.content.semiBold,
    color: theme.colors.text.onBehalf,
  },
  submitButton: {
    ...textStyles.content.semiBold,
    backgroundColor: theme.colors.badge.actionDone,
    alignSelf: 'center',
    width: '90%',
    height: 50,
    borderRadius: 50,
    marginBottom: theme.metrics.spacing[3],
  },
  buttonText: {
    ...textStyles.content.semiBold,
    fontSize: theme.metrics.textSize[16],
    fontWeight: theme.metrics.fontWeight.semiBold,
  },
}));