All files / app/features/publicTrips useListPublicTrips.ts

86.95% Statements 20/23
50% Branches 2/4
80% Functions 8/10
86.95% Lines 20/23

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 1045x           5x               5x       5x   39x 39x     15x                                                             472x       472x 472x                     1x   1x     1x         631x 631x 631x                       3x   3x 3x 3x        
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
  publicTripsBaseKey,
  publicTripsByUserBaseKey,
  publicTripsByUserKey,
  publicTripsKey,
} from "features/queryKeys";
import { RpcError } from "grpc-web";
import {
  ListPublicTripsByUserRes,
  ListPublicTripsRes,
  PublicTrip as PublicTripPb,
  PublicTripStatus,
} from "proto/public_trips_pb";
import { service } from "service";
 
export type PublicTrip = PublicTripPb.AsObject;
 
const PAGE_SIZE = 10;
 
export function useListPublicTrips(communityId: number, pageToken: string) {
  return useQuery<ListPublicTripsRes.AsObject, RpcError>({
    queryKey: [...publicTripsKey(communityId), pageToken],
    queryFn: () =>
      service.publicTrips.listPublicTrips({
        communityId,
        pageToken: pageToken || undefined,
        pageSize: PAGE_SIZE,
      }),
    enabled: !!communityId,
  });
}
 
export function useListPublicTripsByUser({
  userId,
  pageToken,
  ascending,
}: {
  userId: number;
  pageToken: string;
  ascending?: boolean;
}) {
  return useQuery<ListPublicTripsByUserRes.AsObject, RpcError>({
    queryKey: [...publicTripsByUserKey(userId), pageToken, ascending],
    queryFn: () =>
      service.publicTrips.listPublicTripsByUser({
        userId,
        pageToken: pageToken || undefined,
        pageSize: PAGE_SIZE,
        ascending,
      }),
    enabled: !!userId,
  });
}
 
export function useCreatePublicTrip(
  communityId: number,
  onSuccess?: () => void,
) {
  const queryClient = useQueryClient();
  return useMutation<
    PublicTripPb.AsObject,
    RpcError,
    {
      communityId: number;
      fromDate: string;
      toDate: string;
      description: string;
      sameGenderOnly: boolean;
    }
  >({
    mutationFn: (input) => service.publicTrips.createPublicTrip(input),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: publicTripsKey(communityId),
      });
      onSuccess?.();
    },
  });
}
 
export function useUpdatePublicTrip(onSuccess?: () => void) {
  const queryClient = useQueryClient();
  return useMutation<
    PublicTripPb.AsObject,
    RpcError,
    {
      tripId: number;
      fromDate?: string;
      toDate?: string;
      description?: string;
      status?: PublicTripStatus;
      sameGenderOnly?: boolean;
    }
  >({
    mutationFn: (input) => service.publicTrips.updatePublicTrip(input),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [publicTripsBaseKey] });
      queryClient.invalidateQueries({ queryKey: [publicTripsByUserBaseKey] });
      onSuccess?.();
    },
  });
}