All files / apps/reallocation/src/screens Reallocation.tsx

100% Statements 36/36
100% Branches 24/24
100% Functions 12/12
100% Lines 36/36

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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268                                                                                              1x                         22x   22x                     22x 22x   22x 22x 22x 22x   22x 1x 1x 1x               22x                     22x 11x     22x 11x     22x 11x     22x 11x       22x 3x 3x 3x 2x 1x 1x           22x   11x 22x 11x             22x   11x           22x                                                                                                                                                                                                                                        
import { useCallback, useMemo, useRef } from 'react';
import { useForm } from 'react-hook-form';
import { TextInput, View } from 'react-native';
 
import { Button } from '@repo/ui/components/Button';
import { FormDatePicker } from '@repo/ui/components/Form/FormDatePicker';
import { FormEmailInput } from '@repo/ui/components/Form/FormEmailInput';
import { FormInput } from '@repo/ui/components/Form/FormInput';
import { FormSelect } from '@repo/ui/components/Form/FormSelect';
import { InputDatePickerRef } from '@repo/ui/components/InputDatePicker';
import { SelectRef } from '@repo/ui/components/Select';
import { WrapperRequest } from '@repo/ui/components/WrapperRequest';
import { ClockIcon } from '@repo/ui/icons/Clock';
import { LocationIcon } from '@repo/ui/icons/Location';
import { MapIcon } from '@repo/ui/icons/Map';
import { MultipleUserIcon } from '@repo/ui/icons/MultipleUser';
import { useTheme } from '@repo/ui/themes/ThemeContext';
 
import { REALLOCATION_DESTINATION_HOME } from '@repo/constants/request';
import { REALLOCATION_TIME_SLOTS } from '@repo/constants/time';
 
import { useRequestForm } from '@repo/hooks/request';
 
import { EmailOption, ReallocationFormType } from '@repo/types/form';
 
import { REQUEST_FORM_FIELD_RULES } from '@repo/validation/fieldRules';
import { validateReallocationForm } from '@repo/validation/requestForms';
 
interface Room {
  id: string;
  office: string;
}
 
interface Props {
  myRoom: Room;
  rooms: Room[];
  isSubmitting?: boolean;
  employees: EmailOption[];
  holidays?: string[];
  makeUpWorkdays?: string[];
  submitButtonText?: string;
  initialValues?: Partial<ReallocationFormType>;
  onSubmit: (data: ReallocationFormType) => void;
  onBack: () => void;
  onScreenBlur?: (closeModals: () => void) => void;
}
 
const Reallocation = ({
  myRoom,
  rooms,
  isSubmitting = false,
  employees,
  holidays = [],
  makeUpWorkdays = [],
  submitButtonText = 'Submit Request',
  initialValues,
  onSubmit,
  onBack,
  onScreenBlur,
}: Props) => {
  const { theme } = useTheme();
 
  const methods = useForm<ReallocationFormType>({
    defaultValues: {
      emails: initialValues?.emails ?? [],
      origin: initialValues?.origin ?? myRoom.id,
      dest: initialValues?.dest ?? '',
      note: initialValues?.note ?? '',
      date: initialValues?.date ?? null,
      time: initialValues?.time ?? '',
    },
  });
 
  const { control, getValues, watch } = methods;
  const destValue = watch('dest');
 
  const noteInputRef = useRef<TextInput | null>(null);
  const datePickerRef = useRef<InputDatePickerRef>(null);
  const roomSelectRef = useRef<SelectRef>(null);
  const timeSelectRef = useRef<SelectRef>(null);
 
  const closeModals = useCallback(() => {
    datePickerRef.current?.close();
    roomSelectRef.current?.close();
    timeSelectRef.current?.close();
  }, []);
 
  const {
    handlePressBack,
    handleSendRequest,
    createClearErrorCallback,
    disabledDates,
  } = useRequestForm({
    methods,
    onBack,
    onSubmit,
    onScreenBlur,
    closeModals,
    holidays,
    makeUpWorkdays,
    validateBeforeSubmit: validateReallocationForm,
  });
 
  const handleEmailsChange = useMemo(
    () => createClearErrorCallback('emails'),
    [createClearErrorCallback],
  );
  const handleDestChange = useMemo(
    () => createClearErrorCallback('dest'),
    [createClearErrorCallback],
  );
  const handleDateChange = useMemo(
    () => createClearErrorCallback('date'),
    [createClearErrorCallback],
  );
  const handleNoteChange = useMemo(
    () => createClearErrorCallback('note'),
    [createClearErrorCallback],
  );
 
  const handleTimeChange = useCallback(() => {
    createClearErrorCallback('time')();
    const noteValue = getValues('note');
    if (!noteValue || noteValue.trim() === '') {
      if (noteInputRef.current) {
        setTimeout(() => {
          noteInputRef.current?.focus();
        }, 100);
      }
    }
  }, [createClearErrorCallback, getValues]);
 
  const roomOptions = useMemo(
    () =>
      rooms
        .filter(r => r.id !== myRoom.id)
        .map(r => ({
          label: r.id,
          value: r.id,
        })),
    [rooms, myRoom.id],
  );
 
  const footerButtonLabel = useMemo(
    () =>
      destValue === REALLOCATION_DESTINATION_HOME
        ? 'Submit Request'
        : submitButtonText,
    [destValue, submitButtonText],
  );
 
  return (
    <WrapperRequest
      title="Reallocation"
      onBack={handlePressBack}
      disabled={isSubmitting}
      footerComponent={
        <Button
          onPress={handleSendRequest}
          isLoading={isSubmitting}
          accessibilityLabel="Submit Request"
          accessibilityRole="button"
          accessibilityHint="Sends a reallocation request for the selected room and time"
          testID="submit-button"
        >
          {footerButtonLabel}
        </Button>
      }
    >
      <View style={{ gap: theme.metrics.spacing[4] }}>
        <FormEmailInput
          control={control}
          name="emails"
          rules={REQUEST_FORM_FIELD_RULES.reallocation.emails}
          leftIcon={
            <MultipleUserIcon
              width={theme.metrics.iconSize[24]}
              height={theme.metrics.iconSize[24]}
              color={theme.colors.icon.active}
            />
          }
          options={employees}
          label="Recipients"
          testID="recipients-input"
          disabled={isSubmitting}
          onChange={handleEmailsChange}
        />
        <FormInput
          control={control}
          name="origin"
          leftIcon={
            <LocationIcon
              width={theme.metrics.iconSize[24]}
              height={theme.metrics.iconSize[24]}
              color={theme.colors.icon.active}
            />
          }
          label="Current Location"
          testID="origin-input"
          disabled
        />
        <FormSelect
          ref={roomSelectRef}
          control={control}
          name="dest"
          rules={REQUEST_FORM_FIELD_RULES.reallocation.dest}
          label="Move To"
          testID="move-to-select"
          placeholder="Select Room"
          options={roomOptions}
          disabled={isSubmitting}
          onChange={handleDestChange}
          leftIcon={
            <MapIcon
              width={theme.metrics.iconSize[24]}
              height={theme.metrics.iconSize[24]}
              color={theme.colors.icon.active}
            />
          }
        />
        <FormDatePicker
          ref={datePickerRef}
          control={control}
          name="date"
          rules={REQUEST_FORM_FIELD_RULES.reallocation.date}
          mode="single"
          testID="date-select"
          disabled={isSubmitting}
          disabledDates={disabledDates}
          onConfirm={handleDateChange}
        />
        <FormSelect
          ref={timeSelectRef}
          control={control}
          name="time"
          rules={REQUEST_FORM_FIELD_RULES.reallocation.time}
          label="Time"
          testID="time-select"
          options={REALLOCATION_TIME_SLOTS}
          disabled={isSubmitting}
          placeholder="Select time"
          leftIcon={
            <ClockIcon
              width={theme.metrics.iconSize[24]}
              height={theme.metrics.iconSize[24]}
              color={theme.colors.icon.active}
            />
          }
          onChange={handleTimeChange}
        />
        <FormInput
          ref={noteInputRef}
          control={control}
          name="note"
          rules={REQUEST_FORM_FIELD_RULES.reallocation.note}
          disabled={isSubmitting}
          placeholder="Type something"
          label="Reason"
          testID="reason-input"
          onChangeText={handleNoteChange}
        />
      </View>
    </WrapperRequest>
  );
};
 
export default Reallocation;