Commit ddfd57ce authored by AlexandrValgamov's avatar AlexandrValgamov
Browse files

Widget | feat: add api, api-hooks

parent 5d162fa1
Loading
Loading
Loading
Loading

src/api-hooks/index.ts

0 → 100644
+10 −0
Original line number Diff line number Diff line
export * from './use-catalog';
export * from './use-category';
export * from './use-product';
export * from './use-shops';
export * from './use-recommendations';
export * from './use-payment';
export * from './use-user';
export * from './use-logout';
export * from './use-login';
export * from './use-register';
+1 −0
Original line number Diff line number Diff line
export * from './use-catalog';
+9 −0
Original line number Diff line number Diff line
import { useQuery } from '@tanstack/react-query';

import { getCatalog } from '@/api';

export const useCatalog = (shop: string) =>
  useQuery({
    queryKey: ['catalog', shop],
    queryFn: async () => getCatalog(shop),
  });
+1 −0
Original line number Diff line number Diff line
export * from './use-category';
+34 −0
Original line number Diff line number Diff line
import { getCategories } from '@/utils';
import { MenuList, Stylebook } from '@/interfaces';

import { useCatalog } from '../use-catalog';

interface UseCategoryResult {
  newStylebook: Stylebook | null;
  category: MenuList | null;
  isError: boolean;
  error: Error | null;
  isLoading: boolean;
  isSuccess: boolean;
}

export const useCategory = (
  shop: string,
  tabIndex: number,
  categoryIndex: number,
): UseCategoryResult => {
  const { data, isError, error, isLoading, isSuccess } = useCatalog(shop);
  const categories = isSuccess && data ? getCategories(data) : null;
  const category =
    isSuccess && categories ? categories[tabIndex]?.lists[categoryIndex] : null;
  const newStylebook = isSuccess && data ? data.shop?.stylebook : null;

  return {
    newStylebook,
    category,
    isError,
    error,
    isLoading,
    isSuccess,
  };
};
Loading