zhengyiming
昨天 25708e3f81956c1517f495e3303a6c8d08bb730c
packages/hooks/standardOrder.ts
@@ -1,6 +1,7 @@
import { useQuery, useQueryClient } from '@tanstack/vue-query';
import * as standardServiceServices from '@12333/services/apiV2/standardService';
import { computed, MaybeRef, unref } from 'vue';
import { groupBy } from 'lodash';
type UseStandardServiceDetailOptions = {
  id: MaybeRef<string>;
@@ -41,3 +42,56 @@
    minPrice,
  };
}
type UseStandardServiceListOptions = {
  params?: MaybeRef<API.GetOpenStandardServiceListQuery>;
  onSuccess?: (data: API.GetStandardServicesQueryResultItem[]) => any;
};
export function useStandardServiceList(options: UseStandardServiceListOptions = {}) {
  const { params = {}, onSuccess } = options;
  const {
    data: standardServiceList,
    refetch,
    isLoading,
    isError,
  } = useQuery({
    queryKey: ['standardServiceServices/getOpenStandardServiceList', params],
    queryFn: async () => {
      return await standardServiceServices.getOpenStandardServiceList(unref(params), {
        showLoading: false,
      });
    },
    placeholderData: () => [] as API.GetStandardServicesQueryResultItem[],
    onSuccess(data) {
      onSuccess?.(data);
    },
  });
  const standardServiceListForCategory = computed(() => {
    return standardServiceList.value.map((x) => ({
      ...x,
      catName: x.name,
    }));
  });
  const standardServiceListForCategoryMap = computed(() => {
    const group = groupBy(standardServiceListForCategory.value, 'jobCode');
    return group;
  });
  const queryClient = useQueryClient();
  function ensureStandardServiceList() {
    return queryClient.ensureQueryData<API.GetStandardServicesQueryResultItem[]>({
      queryKey: ['standardServiceServices/getOpenStandardServiceList', params],
    });
  }
  return {
    standardServiceList,
    ensureStandardServiceList,
    standardServiceListForCategory,
    standardServiceListForCategoryMap,
  };
}