feat: services table, base segmented control
This commit is contained in:
@ -0,0 +1,84 @@
|
||||
import { LexoRank } from "lexorank";
|
||||
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
||||
import {
|
||||
CreateServiceCategorySchema,
|
||||
ServiceCategorySchema,
|
||||
UpdateServiceCategorySchema,
|
||||
} from "@/lib/client";
|
||||
import {
|
||||
createServiceCategoryMutation,
|
||||
deleteServiceCategoryMutation,
|
||||
updateServiceCategoryMutation,
|
||||
} from "@/lib/client/@tanstack/react-query.gen";
|
||||
import { ServiceType } from "@/modules/dealModularEditorTabs/FulfillmentBase/shared/types/service";
|
||||
import { getMaxLexorankByKey, getNewLexorank } from "@/utils/lexorank";
|
||||
|
||||
type UseServiceCategoryProps = {
|
||||
queryKey: any[];
|
||||
categories: ServiceCategorySchema[];
|
||||
};
|
||||
|
||||
export type ServiceCategoriesCrud = {
|
||||
onCreate: (category: CreateServiceCategorySchema) => void;
|
||||
onUpdate: (
|
||||
categoryId: number,
|
||||
category: UpdateServiceCategorySchema,
|
||||
onSuccess?: () => void
|
||||
) => void;
|
||||
onDelete: (service: ServiceCategorySchema) => void;
|
||||
};
|
||||
|
||||
export const useServiceCategoriesCrud = ({
|
||||
queryKey,
|
||||
categories,
|
||||
}: UseServiceCategoryProps): ServiceCategoriesCrud => {
|
||||
const getCategoryRank = (serviceType: ServiceType): string => {
|
||||
const lastCategory = getMaxLexorankByKey(
|
||||
categories,
|
||||
serviceType === ServiceType.DEAL_SERVICE
|
||||
? "dealServiceRank"
|
||||
: "productServiceRank"
|
||||
);
|
||||
|
||||
if (!lastCategory) return LexoRank.middle().toString();
|
||||
|
||||
const rank =
|
||||
serviceType === ServiceType.DEAL_SERVICE
|
||||
? lastCategory.dealServiceRank
|
||||
: lastCategory?.productServiceRank;
|
||||
|
||||
return getNewLexorank(LexoRank.parse(rank)).toString();
|
||||
};
|
||||
|
||||
return useCrudOperations<
|
||||
ServiceCategorySchema,
|
||||
UpdateServiceCategorySchema,
|
||||
CreateServiceCategorySchema
|
||||
>({
|
||||
key: "getServiceCategories",
|
||||
queryKey,
|
||||
mutations: {
|
||||
create: createServiceCategoryMutation(),
|
||||
update: updateServiceCategoryMutation(),
|
||||
delete: deleteServiceCategoryMutation(),
|
||||
},
|
||||
getCreateEntity: category => {
|
||||
const dealServiceRank = getCategoryRank(ServiceType.DEAL_SERVICE);
|
||||
const productServiceRank = getCategoryRank(
|
||||
ServiceType.PRODUCT_SERVICE
|
||||
);
|
||||
|
||||
return {
|
||||
dealServiceRank,
|
||||
productServiceRank,
|
||||
name: category.name!,
|
||||
};
|
||||
},
|
||||
getUpdateEntity: (old, update) =>
|
||||
({
|
||||
...old,
|
||||
...update,
|
||||
}) as ServiceCategorySchema,
|
||||
getDeleteConfirmTitle: () => "Удаление категории услуг",
|
||||
});
|
||||
};
|
||||
@ -1,3 +1,4 @@
|
||||
import { LexoRank } from "lexorank";
|
||||
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
|
||||
import {
|
||||
CreateServiceSchema,
|
||||
@ -9,9 +10,11 @@ import {
|
||||
deleteServiceMutation,
|
||||
updateServiceMutation,
|
||||
} from "@/lib/client/@tanstack/react-query.gen";
|
||||
import { getMaxByLexorank, getNewLexorank } from "@/utils/lexorank";
|
||||
|
||||
type UseServicesProps = {
|
||||
queryKey: any[];
|
||||
services: ServiceSchema[];
|
||||
};
|
||||
|
||||
export type ServicesCrud = {
|
||||
@ -26,6 +29,7 @@ export type ServicesCrud = {
|
||||
|
||||
export const useServicesCrud = ({
|
||||
queryKey,
|
||||
services,
|
||||
}: UseServicesProps): ServicesCrud => {
|
||||
return useCrudOperations<
|
||||
ServiceSchema,
|
||||
@ -39,6 +43,15 @@ export const useServicesCrud = ({
|
||||
update: updateServiceMutation(),
|
||||
delete: deleteServiceMutation(),
|
||||
},
|
||||
getCreateEntity: service => {
|
||||
const maxRankStr = getMaxByLexorank(services)?.lexorank;
|
||||
const maxRank = maxRankStr ? LexoRank.parse(maxRankStr) : null;
|
||||
|
||||
return {
|
||||
...(service as CreateServiceSchema),
|
||||
lexorank: getNewLexorank(maxRank).toString(),
|
||||
};
|
||||
},
|
||||
getUpdateEntity: (old, update) =>
|
||||
({
|
||||
...old,
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { ServiceCategorySchema } from "@/lib/client";
|
||||
import {
|
||||
getServiceCategoriesOptions,
|
||||
getServiceCategoriesQueryKey,
|
||||
} from "@/lib/client/@tanstack/react-query.gen";
|
||||
|
||||
export type ServiceCategoriesList = {
|
||||
categories: ServiceCategorySchema[];
|
||||
setCategories: (categories: ServiceCategorySchema[]) => void;
|
||||
refetch: () => void;
|
||||
queryKey: any[];
|
||||
isLoading: boolean;
|
||||
};
|
||||
|
||||
const useServiceCategoriesList = (): ServiceCategoriesList => {
|
||||
const queryClient = useQueryClient();
|
||||
const { data, refetch, isLoading } = useQuery(
|
||||
getServiceCategoriesOptions()
|
||||
);
|
||||
|
||||
const queryKey = getServiceCategoriesQueryKey();
|
||||
|
||||
const setCategories = (categories: ServiceCategorySchema[]) => {
|
||||
queryClient.setQueryData(
|
||||
queryKey,
|
||||
(old: { items: ServiceCategorySchema[] }) => ({
|
||||
...old,
|
||||
items: categories,
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
return {
|
||||
categories: data?.items ?? [],
|
||||
setCategories,
|
||||
refetch,
|
||||
queryKey,
|
||||
isLoading,
|
||||
};
|
||||
};
|
||||
|
||||
export default useServiceCategoriesList;
|
||||
@ -2,3 +2,8 @@ export enum ServiceType {
|
||||
DEAL_SERVICE,
|
||||
PRODUCT_SERVICE,
|
||||
}
|
||||
|
||||
export enum ServicePriceType {
|
||||
DEFAULT,
|
||||
BY_RANGE,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user