66 lines
2.3 KiB
TypeScript
66 lines
2.3 KiB
TypeScript
import PricePlanModel from "../model/price-plan-model"
|
|
import PaginationModel from "@/lib/helper/pagination"
|
|
import { usePricePlanQuery, usePricePlanTypesQuery, useServiceTypesQuery } from "../queries"
|
|
import { useCreatePricePlanMutation, useDeletePricePlanMutation } from "../mutations"
|
|
|
|
type UsePricePlanReturn = {
|
|
isLoading: boolean
|
|
isError: boolean
|
|
error: unknown
|
|
data: PaginationModel<PricePlanModel[]>
|
|
ppTypes: Array<{[key: string]: string}>
|
|
serviceTypes: Array<{[key: string]: string}>
|
|
extra: {
|
|
deletePricePlan: (id: string) => void
|
|
createPricePlan: (payload: any) => void
|
|
isDeleting: boolean
|
|
isCreating: boolean
|
|
}
|
|
}
|
|
|
|
export const usePricePlan = (
|
|
page: number,
|
|
size: number,
|
|
type: string,
|
|
onCreateSuccess: () => void
|
|
): UsePricePlanReturn => {
|
|
// Queries
|
|
const priceplanQuery = usePricePlanQuery(page, size, type)
|
|
const ppTypesQuery = usePricePlanTypesQuery()
|
|
const servTypesQuery = useServiceTypesQuery()
|
|
// Mutations
|
|
const deleteMutation = useDeletePricePlanMutation()
|
|
const createMutation = useCreatePricePlanMutation(onCreateSuccess)
|
|
// Data
|
|
const dataJson: any = priceplanQuery.data
|
|
const ppTypes: any = ppTypesQuery.data
|
|
const serviceTypes: any = servTypesQuery.data
|
|
let models: PricePlanModel[] = []
|
|
if (dataJson) {
|
|
models = PricePlanModel.fromJSON(dataJson)
|
|
}
|
|
|
|
return {
|
|
isLoading: priceplanQuery.isLoading,
|
|
isError: priceplanQuery.isError,
|
|
error: priceplanQuery.error,
|
|
data: priceplanQuery.data ? new PaginationModel({
|
|
currentPage: dataJson.data.number,
|
|
totalPages: dataJson.data.totalPages,
|
|
pageSize: dataJson.data.size,
|
|
totalElements: dataJson.data.totalElements,
|
|
content: models,
|
|
}) : PaginationModel.initialValue(),
|
|
ppTypes: ppTypes ? ppTypes.data : [],
|
|
serviceTypes: serviceTypes ? serviceTypes?.data.map((item: any) => {{
|
|
return {id: item.id, servTypeName: item.servTypeName}
|
|
}}).slice(0, 100) : [],
|
|
extra: {
|
|
deletePricePlan: deleteMutation.mutate,
|
|
isDeleting: deleteMutation.isPending,
|
|
createPricePlan: createMutation.mutate,
|
|
isCreating: createMutation.isPending
|
|
}
|
|
}
|
|
}
|