66 lines
1.5 KiB
JavaScript
66 lines
1.5 KiB
JavaScript
import useSWR from 'swr'
|
|
import useSWRMutation from 'swr/mutation'
|
|
import { protocol } from './index'
|
|
import {
|
|
DEL_DELETE_HOUSE,
|
|
GET_HOUSE_DETAIL,
|
|
GET_HOUSES_LIST,
|
|
POST_HOUSE_CREATE,
|
|
PUT_UPDATE_HOUSE,
|
|
} from './url'
|
|
|
|
const getAllHouse = async ({ page, pageSize }) => {
|
|
const url = GET_HOUSES_LIST({ page, pageSize })
|
|
const response = await protocol.get(url, {})
|
|
return response.data
|
|
}
|
|
|
|
const getHouseDetail = async (id) => {
|
|
const url = GET_HOUSE_DETAIL(id)
|
|
const response = await protocol.get(url, {})
|
|
return response.data
|
|
}
|
|
|
|
const postCreateHouse = (payload) => {
|
|
return protocol.post(POST_HOUSE_CREATE, payload)
|
|
}
|
|
|
|
const putUpdateHouse = (payload) => {
|
|
return protocol.put(PUT_UPDATE_HOUSE, payload)
|
|
}
|
|
|
|
const putDeleteHouse = (_action, { arg: id }) => {
|
|
return protocol.delete(DEL_DELETE_HOUSE(id))
|
|
}
|
|
|
|
const createOrUpdateHouse = (_action, { arg }) => {
|
|
const isUpdate = !!arg.id
|
|
const fnWait = isUpdate ? putUpdateHouse : postCreateHouse
|
|
|
|
return fnWait(arg)
|
|
}
|
|
|
|
export function useHouse(page, pageSize) {
|
|
const swrObj = useSWR({ page, pageSize }, getAllHouse)
|
|
const { trigger, isMutating } = useSWRMutation('action', putDeleteHouse)
|
|
|
|
return {
|
|
...swrObj,
|
|
houses: swrObj.data ?? [],
|
|
trigger,
|
|
isMutating,
|
|
}
|
|
}
|
|
|
|
export function useHouseDetail(id) {
|
|
const swrObj = useSWR(id, getHouseDetail)
|
|
const { trigger, isMutating } = useSWRMutation('action', createOrUpdateHouse)
|
|
|
|
return {
|
|
...swrObj,
|
|
house: swrObj.data ?? {},
|
|
trigger,
|
|
isMutating,
|
|
}
|
|
}
|