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

88.57% Statements 31/35
86.95% Branches 20/23
81.81% Functions 9/11
87.09% Lines 27/31

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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236                                                            1x 5x 5x     5x 5x 5x 5x     5x         5x                 5x   5x 5x 5x               5x                       5x 5x   5x 5x       5x 1x       1x                                           5x 1x             4x 1x             3x                                                                                                               5x                                                                                                  
import { useCallback, useEffect, useMemo } from 'react';
import { ActivityIndicator, ScrollView, Text, View } from 'react-native';
 
import { useQueryClient } from '@tanstack/react-query';
 
import { AvatarList } from '@repo/ui/components/AvatarList';
import { Button } from '@repo/ui/components/Button';
import { NoteIcon } from '@repo/ui/icons/Note';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { textStyles } from '@repo/ui/themes/typography';
 
import { QUERY_KEYS } from '@repo/hooks';
import { RequestFor } from '@repo/hooks/userQueries';
 
import { DateInfo } from '@/components/DateInfo';
 
import { useTimeOffExtraUsers } from '@/hooks/useTimeOffEmployees';
 
import {
  useTimeOffV2ActionsSelector,
  useTimeOffV2StateSelector,
} from '../TimeOffV2/context';
import { useTimeOffV2Runtime } from '../TimeOffV2/runtime';
 
import {
  useCreateTimeOffRequest,
  usePreviewPeople,
  usePreviewSummary,
} from './hooks';
 
const Preview = () => {
  const queryClient = useQueryClient();
  const styles = useStyles();
  const {
    actions: { onSubmitSuccess, onSubmitError },
  } = useTimeOffV2Runtime();
  const form = useTimeOffV2StateSelector(state => state.form);
  const isOnBehalf = useTimeOffV2StateSelector(state => state.isOnBehalf);
  const setSubmitting = useTimeOffV2ActionsSelector(
    actions => actions.setSubmitting,
  );
  const { summaryDateText, summaryValueText, safeReason } = usePreviewSummary({
    startDate: form.start,
    endDate: form.end,
    reason: form.note,
  });
  const { approversPreview, observersPreview } = usePreviewPeople({
    approvers: form.approvers,
    observers: form.observers,
  });
 
  const {
    data: extraUserOptions = [],
    isLoading: isLoadingExtraUsers,
    isError: isErrorExtraUsers,
  } = useTimeOffExtraUsers({ disabled: !isOnBehalf });
 
  const onBehalfUser = useMemo(() => {
    const user = extraUserOptions.find(item => item.email === form.requester);
    return {
      id: user?.email || '',
      name: user?.name || '',
      uri: user?.avatar,
    };
  }, [extraUserOptions, form.requester]);
 
  const { mutate: createRequest, isPending: isSubmitting } =
    useCreateTimeOffRequest(() => {
      queryClient.invalidateQueries({
        queryKey: [
          QUERY_KEYS.MY_REQUESTS,
          isOnBehalf ? RequestFor.ON_BEHALF : RequestFor.ME,
          'time-off',
          'all',
        ],
      });
      onSubmitSuccess();
    }, onSubmitError);
 
  useEffect(() => {
    setSubmitting(isSubmitting);
 
    return () => {
      setSubmitting(false);
    };
  }, [isSubmitting, setSubmitting]);
 
  const handleSubmit = useCallback(() => {
    Iif (!form.start || !form.end) {
      return;
    }
 
    createRequest({
      category: 'time-off',
      to: form.approvers,
      cc: form.observers,
      ...(form.requester && isOnBehalf && { requester: form.requester }),
      data: {
        start: form.start.toISOString(),
        end: form.end.toISOString(),
        note: form.note,
      },
    });
  }, [
    createRequest,
    form.approvers,
    form.end,
    form.note,
    form.observers,
    form.requester,
    form.start,
    isOnBehalf,
  ]);
 
  if (isLoadingExtraUsers) {
    return (
      <View style={styles.loaderContainer} testID="preview-loader">
        <ActivityIndicator />
      </View>
    );
  }
 
  if (isErrorExtraUsers) {
    return (
      <View style={styles.loaderContainer}>
        <Text>Something went wrong</Text>
      </View>
    );
  }
 
  return (
    <>
      <ScrollView
        style={styles.container}
        contentContainerStyle={styles.content}
      >
        <DateInfo
          summaryDateText={summaryDateText}
          summaryValueText={summaryValueText}
          summaryValueStyle={styles.totalOff}
        />
        <View style={styles.personGroupContainer}>
          {isOnBehalf && form.requester && (
            <View style={styles.personGroup}>
              <Text style={styles.label}>on behalf of</Text>
              <AvatarList
                data={[onBehalfUser]}
                selectedIds={[form.requester]}
              />
            </View>
          )}
          <View style={styles.personGroup}>
            <Text style={styles.label}>approvers</Text>
            <AvatarList data={approversPreview} selectedIds={form.approvers} />
          </View>
          {form.observers.length > 0 && (
            <View style={styles.personGroup}>
              <Text style={styles.label}>observers</Text>
              <AvatarList
                data={observersPreview}
                selectedIds={form.observers}
              />
            </View>
          )}
        </View>
        <View style={styles.reasonGroup}>
          <View>
            <NoteIcon />
          </View>
          <Text style={styles.reasonText}>{safeReason}</Text>
        </View>
      </ScrollView>
      <Button
        style={styles.submitBtn}
        isLoading={isSubmitting}
        onPress={handleSubmit}
        testID="submit-btn"
      >
        Submit
      </Button>
    </>
  );
};
 
export default Preview;
 
const useStyles = makeStyles(theme => ({
  container: {
    flex: 1,
  },
  content: {
    paddingHorizontal: theme.metrics.spacing[4],
    gap: theme.metrics.spacing[4],
    paddingBottom: theme.metrics.spacing[6],
  },
  label: {
    fontWeight: theme.metrics.fontWeight.semiBold,
    textTransform: 'uppercase',
    ...textStyles.content.semiBold,
  },
  personGroupContainer: {
    gap: theme.metrics.spacing[7],
  },
  personGroup: {
    gap: theme.metrics.spacing[2],
  },
  reasonGroup: {
    marginTop: theme.metrics.spacing[4],
    flexDirection: 'row',
    gap: theme.metrics.spacing[2],
  },
  reasonText: {
    flex: 1,
    paddingTop: theme.metrics.spacing[0.25],
    color: theme.colors.text.primary,
    ...textStyles.content.regular,
  },
  submitBtn: {
    backgroundColor: theme.colors.slate97,
    alignSelf: 'center',
    width: '90%',
    height: 50,
    borderRadius: 50,
  },
  loaderContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: theme.colors.background.pageMuted,
  },
  totalOff: {
    color: theme.colors.slate97,
    textTransform: 'uppercase',
  },
}));