init project portal web
This commit is contained in:
65
lib/price-plan/view-model/index.ts
Normal file
65
lib/price-plan/view-model/index.ts
Normal file
@ -0,0 +1,65 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
19
lib/price-plan/view-model/sidebar-view-model.ts
Normal file
19
lib/price-plan/view-model/sidebar-view-model.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import CommonData from "@/lib/helper/query-data"
|
||||
import { pricePlanRepository } from "@/lib/price-plan/data/repository"
|
||||
import { PricePlanMenuModel } from "@/lib/price-plan/model/menu-model"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
|
||||
export const useMenuPricePlan = () => {
|
||||
const query = useQuery({
|
||||
queryKey: ["priceplan-menu"],
|
||||
queryFn: pricePlanRepository.getMenuList,
|
||||
})
|
||||
|
||||
return new CommonData<PricePlanMenuModel[], any>({
|
||||
isLoading: query.isLoading,
|
||||
isError: query.isError,
|
||||
error: query.error,
|
||||
data: query.data ? PricePlanMenuModel.fromJSON(query.data) : [],
|
||||
extra: null
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user