All files / apps/timeOff/src/screens/TimeOff runtime.tsx

96.42% Statements 27/28
96.42% Branches 27/28
66.66% Functions 4/6
100% Lines 27/27

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                1x 1x                                                                     1x       1x             1x             1x 2x   2x 1x         1x     1x     6x 6x 6x 6x 6x 6x   6x         6x           6x                       6x 2x 2x   1x 1x     1x       6x                                                  
import React, { createContext, useContext } from 'react';
 
import { HttpService } from '@repo/services/httpClients';
 
import { isHttpError } from '@repo/utils/error';
 
import { TimeOffRemoteProps } from './types';
 
const defaultGetAccessToken = async () => null;
const noop = () => {};
 
export interface TimeOffRuntimeState {
  baseApiUrl: string;
  spaceApiUrl: string;
}
 
export interface TimeOffRuntimeActions {
  getAccessToken: () => Promise<string | null>;
  onUnauthorized?: () => void;
  onClose: () => void;
  onSubmitSuccess: () => void;
  onSubmitError: (error: unknown) => void;
  onSubmittingChange?: (isSubmitting: boolean) => void;
  setFabOffset?: (offset: number) => void;
}
 
export interface TimeOffRuntimeApi {
  mainHttp: HttpService;
  spaceHttp: HttpService;
  holidaysHttp: HttpService;
  requestWithAuthGuard: <T>(requestFn: () => Promise<T>) => Promise<T>;
}
 
export interface TimeOffRuntimeMeta {
  featureFlags: Record<string, boolean>;
  api: TimeOffRuntimeApi;
}
 
export interface TimeOffRuntimeContextValue {
  state: TimeOffRuntimeState;
  actions: TimeOffRuntimeActions;
  meta: TimeOffRuntimeMeta;
}
 
const TimeOffRuntimeContext = createContext<TimeOffRuntimeContextValue | null>(
  null,
);
 
export const TimeOffRuntimeProvider = ({
  value,
  children,
}: {
  value: TimeOffRuntimeContextValue;
  children: React.ReactNode;
}) => {
  return (
    <TimeOffRuntimeContext.Provider value={value}>
      {children}
    </TimeOffRuntimeContext.Provider>
  );
};
 
export const useTimeOffRuntime = () => {
  const context = useContext(TimeOffRuntimeContext);
 
  if (!context) {
    throw new Error(
      'useTimeOffRuntime must be used within TimeOffRuntimeProvider',
    );
  }
 
  return context;
};
 
export const createTimeOffRuntimeValue = (
  props: TimeOffRemoteProps,
): TimeOffRuntimeContextValue => {
  const baseApiUrl = props.env?.baseApiUrl ?? '';
  const spaceApiUrl = props.env?.spaceApiUrl ?? baseApiUrl;
  const basicAuthUsername = props.env?.basicAuthUsername ?? '';
  const basicAuthPassword = props.env?.basicAuthPassword ?? '';
  const getAccessToken = props.auth?.getAccessToken ?? defaultGetAccessToken;
  const onUnauthorized = props.auth?.onUnauthorized;
 
  const mainHttp = new HttpService(baseApiUrl, {
    getToken: getAccessToken,
    auth: { type: 'bearer' },
  });
 
  const spaceHttp = new HttpService(spaceApiUrl, {
    getToken: getAccessToken,
    auth: { type: 'bearer' },
  });
 
  const holidaysHttp =
    basicAuthUsername && basicAuthPassword
      ? new HttpService(baseApiUrl, {
          auth: {
            type: 'basic',
            basic: {
              username: basicAuthUsername,
              password: basicAuthPassword,
            },
          },
        })
      : mainHttp;
 
  const requestWithAuthGuard = async <T,>(requestFn: () => Promise<T>) => {
    try {
      return await requestFn();
    } catch (error) {
      Eif (isHttpError(error) && error.status === 401) {
        onUnauthorized?.();
      }
 
      throw error;
    }
  };
 
  return {
    state: {
      baseApiUrl,
      spaceApiUrl,
    },
    actions: {
      getAccessToken,
      onUnauthorized,
      onClose: props.onClose ?? noop,
      onSubmitSuccess: props.onSubmitSuccess ?? noop,
      onSubmitError: props.onSubmitError ?? noop,
      onSubmittingChange: props.onSubmittingChange,
      setFabOffset: props.setFabOffset,
    },
    meta: {
      featureFlags: props.featureFlags ?? {},
      api: {
        mainHttp,
        spaceHttp,
        holidaysHttp,
        requestWithAuthGuard,
      },
    },
  };
};