You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.2 KiB
40 lines
1.2 KiB
1 year ago
|
import { useAsyncState, watchOnce } from '@vueuse/core'
|
||
|
import type { MaybeRef } from 'vue'
|
||
|
import { ref, unref } from 'vue'
|
||
|
import { deleteModelService, getAllModelServices } from '@/api/product/model'
|
||
|
import { useMessage } from '@/hooks/web/useMessage'
|
||
|
import { useModal } from '@/components/Modal'
|
||
|
import type { ModelService } from '@/api/product/types'
|
||
|
|
||
|
export function useModelService(productId: MaybeRef<string>) {
|
||
|
const selectedModelId = ref<string>()
|
||
|
|
||
|
const { state, execute } = useAsyncState(() => getAllModelServices(unref(productId)), [], { resetOnExecute: false })
|
||
|
|
||
|
watchOnce(state, () => {
|
||
|
if (state.value.length > 0)
|
||
|
selectedModelId.value = state.value[0].id
|
||
|
})
|
||
|
|
||
|
const [registerModelServiceModal, { openModal }] = useModal<ModelService>()
|
||
|
|
||
|
async function handleDeleteModelService(id: string) {
|
||
|
try {
|
||
|
await deleteModelService(id)
|
||
|
useMessage().createMessage.success('删除成功')
|
||
|
execute()
|
||
|
}
|
||
|
catch {}
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
selectedModelId,
|
||
|
setSelectedModelId: (id: string) => selectedModelId.value = id,
|
||
|
reloadModelService: execute,
|
||
|
modelServiceList: state,
|
||
|
handleDeleteModelService,
|
||
|
registerModelServiceModal,
|
||
|
openModelServiceModal: openModal,
|
||
|
}
|
||
|
}
|