All files / apps/mealReservation/src/hooks useRequest.ts

100% Statements 21/21
100% Branches 5/5
100% Functions 10/10
100% Lines 21/21

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                                                      1x           4x   4x     2x   2x 1x     1x 1x   1x                 1x                   4x   4x   4x   2x 2x 1x     1x 1x         1x     1x         1x        
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
 
import { API_ENDPOINTS } from '@repo/constants/endpoints';
 
import { USER_QUERY_KEYS } from '@repo/hooks/useUserQueries';
 
import { REQUEST_POLICY, runRequestEffect } from '@repo/services/effectRequest';
 
import { useMealReservationRuntime } from '../screens/MealReservation/runtime';
 
export interface MealReservationPayload {
  breakfast: boolean;
  lunch: boolean;
  vegetarian: boolean;
  office?: string;
  user?: string;
}
 
export interface RoomBase {
  id: string;
  office: string;
}
 
interface RoomsResponse {
  data: RoomBase[];
}
 
export const useGetRooms = (enabled = true) => {
  const {
    state: { baseApiUrl },
    meta: {
      api: { mainHttp, requestWithAuthGuard },
    },
  } = useMealReservationRuntime();
 
  return useQuery<RoomBase[]>({
    queryKey: ['rooms'],
    queryFn: ({ signal: querySignal }) =>
      runRequestEffect(
        async signal => {
          if (!baseApiUrl) {
            throw new Error('baseApiUrl is required to get rooms');
          }
 
          const response = await requestWithAuthGuard(() =>
            mainHttp.get<RoomsResponse>(API_ENDPOINTS.ROOMS, { signal }),
          );
          return response.data.data;
        },
        REQUEST_POLICY.READ,
        querySignal,
      ),
    enabled,
  });
};
 
export const useCreateMealReservation = (
  id: string,
  onSuccess?: () => void,
  onError?: (error?: unknown) => void,
) => {
  const {
    state: { baseApiUrl },
    meta: {
      api: { mainHttp, requestWithAuthGuard },
    },
  } = useMealReservationRuntime();
 
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: (payload: MealReservationPayload) => {
      return runRequestEffect(async signal => {
        if (!baseApiUrl) {
          throw new Error('baseApiUrl is required to submit meal reservation');
        }
 
        await requestWithAuthGuard(() =>
          mainHttp.put(API_ENDPOINTS.MEAL_RESERVATION(id), payload, { signal }),
        );
      }, REQUEST_POLICY.WRITE);
    },
    onSuccess: () => {
      onSuccess?.();
 
      // Invalidate queries in host/MFE context
      queryClient.invalidateQueries({
        queryKey: [USER_QUERY_KEYS.MEAL_RESERVATIONS, id],
      });
    },
    onError: error => {
      onError?.(error);
    },
  });
};