All files / libs/tenants/companies/state/src/lib queries.ts

53.33% Statements 48/90
100% Branches 1/1
15.38% Functions 2/13
57.97% Lines 40/69

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 701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 142x 4868x 4868x 4868x 4868x 4868x 1x 1x 142x 4200x 142x 4200x 142x 4200x 4200x 4200x 4200x   1x 142x 142x 142x 142x 142x                         1x 142x 142x 142x 142x 142x 142x                                  
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useIntl } from 'react-intl';
 
import { useSnackbars } from '@allshares/studio-design-system';
import { useMutation } from '@amalia/ext/react-query';
import { assert, toError } from '@amalia/ext/typescript';
 
import { CompaniesApiClient } from './api-client/companies.api-client';
import { companiesMutationKeys, companiesQueryKeys } from './queries.keys';
 
export const useCompany = ({ enabled }: { enabled?: boolean } = {}) =>
  useQuery({
    queryKey: companiesQueryKeys.currentCompany(),
    queryFn: CompaniesApiClient.getCompany,
    refetchOnWindowFocus: false,
    enabled,
  });
 
export const useCurrentCompany = () => {
  const { data, ...rest } = useCompany();
 
  assert(data, 'useCurrentCompany: data should be defined');
 
  return {
    data,
    ...rest,
  };
};
 
export const useUpdateCompany = () => {
  const queryClient = useQueryClient();
  const { snackError } = useSnackbars();
 
  return useMutation({
    mutationKey: companiesMutationKeys.updateCompany(),
    mutationFn: (params: Parameters<typeof CompaniesApiClient.updateCompany>[0]) =>
      CompaniesApiClient.updateCompany(params),
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: companiesQueryKeys.currentCompany() });
    },
    throwOnError: false,
    onError: (err) => {
      snackError(err.message);
    },
  });
};
 
export const useUploadLogo = () => {
  const queryClient = useQueryClient();
  const { formatMessage } = useIntl();
  const { snackSuccess, snackError } = useSnackbars();
 
  return useMutation({
    mutationKey: companiesMutationKeys.uploadLogo(),
    mutationFn: CompaniesApiClient.uploadLogo,
    onSuccess: async () => {
      snackSuccess(
        formatMessage({
          defaultMessage: 'Your logo has been successfully updated',
        }),
      );

      await queryClient.invalidateQueries({ queryKey: companiesQueryKeys.all() });
    },
    onError: (e) => {
      snackError(toError(e).message);
    },
  });
};