Skip to content

Commit

Permalink
fix(hooks): 优化hooks部分的重复代码
Browse files Browse the repository at this point in the history
  • Loading branch information
HammCn committed Aug 25, 2023
1 parent 5b1e628 commit 4213d7f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 60 deletions.
2 changes: 1 addition & 1 deletion hook/useAirDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ export function useAirDetail<E extends AirEntity>(props: any, entityClass: Class
const title = computed(() => `${formData.value.getClassName()}详情`)

return {
title, formData, isLoading,
title, formData, isLoading, service,
} as IUseDetailResult<E>
}
37 changes: 9 additions & 28 deletions hook/useAirEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AirAbstractEntityService } from '../base/AirAbstractEntityService'
import { AirEntity } from '../base/AirEntity'
import { IUseEditorOption } from '../interface/IUseEditorOption'
import { IUseEditorResult } from '../interface/IUseEditorResult'
import { useAirDetail } from './useAirDetail'

/**
* # 引入Editor的Hook
Expand All @@ -16,47 +17,27 @@ import { IUseEditorResult } from '../interface/IUseEditorResult'
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useAirEditor<E extends AirEntity>(props: any, entityClass: ClassConstructor<E>, serviceClass: ClassConstructor<AirAbstractEntityService<E>>, option: IUseEditorOption<E> = {}): IUseEditorResult<E> {
const isLoading = ref(false)
const result = useAirDetail(props, entityClass, serviceClass, option)

const service = AirClassTransformer.newInstance(serviceClass)
service.loading = isLoading

const formData: Ref<E> = ref(props.param ? props.param.copy() : AirClassTransformer.newInstance(entityClass))

async function getDetail() {
if (props.param.id) {
formData.value = await service.getDetail(props.param.id)

if (option.afterGetDetail) {
const result = option.afterGetDetail(formData.value)
if (result !== undefined) {
formData.value = result
}
}
}
}

const rules = service.createValidator(props.param, option.customRules || {})
const rules = result.service.createValidator(props.param, option.customRules || {})

const formRef = ref<AirFormInstance>() as Ref<AirFormInstance>

async function onSubmit() {
let postData = AirClassTransformer.copy(formData.value, entityClass)
let postData = AirClassTransformer.copy(result.formData.value, entityClass)
if (option.beforeSubmit) {
const result = option.beforeSubmit(postData)
if (result !== undefined) {
postData = result
}
}
await service.save(postData, option.successMessage || (postData.id ? `修改${formData.value.getClassName()}成功` : `添加${formData.value.getClassName()}成功`))
await result.service.save(postData, option.successMessage || (postData.id ? `修改${result.formData.value.getClassName()}成功` : `添加${result.formData.value.getClassName()}成功`))
props.onConfirm()
}

getDetail()

const title = computed(() => ((formData.value.id ? '修改' : '新增') + formData.value.getClassName()))
const title = computed(() => ((result.formData.value.id ? '修改' : '新增') + result.formData.value.getClassName()))

return {
title, formRef, rules, formData, isLoading, onSubmit,
} as IUseEditorResult<E>
return Object.assign(result, {
title, formRef, rules, onSubmit,
}) as IUseEditorResult<E>
}
10 changes: 8 additions & 2 deletions interface/IUseDetailResult.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Ref } from 'vue'
import { AirEntity } from '../base/AirEntity'
import { AirAbstractEntityService } from '../base/AirAbstractEntityService'

/**
* # 详情的Hook标准返回
*/
export interface IUseDetailResult<E extends AirEntity> {
/**
* # 详情显示的标题
* # 对话框显示的标题
*/
title: Ref<string>,

/**
* # 详情数据
* # 表单或详情数据
*/
formData: Ref<E>,

Expand All @@ -21,4 +22,9 @@ export interface IUseDetailResult<E extends AirEntity> {
* 💡 请随意 ```v-loading``` 到你需要的地方
*/
isLoading: Ref<boolean>,

/**
* # 当前Hook使用的Service实例
*/
service: AirAbstractEntityService<E>
}
13 changes: 2 additions & 11 deletions interface/IUseEditorOption.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable no-unused-vars */
import { AirEntity } from '../base/AirEntity'
import { IUseDetailOption } from './IUseDetailOption'
import { IValidateRule } from './IValidateRule'

/**
* # Editor的Hook可选配置
*/
export interface IUseEditorOption<E extends AirEntity> {
export interface IUseEditorOption<E extends AirEntity> extends IUseDetailOption<E>{
/**
* # 自定义验证
*/
Expand All @@ -24,14 +25,4 @@ export interface IUseEditorOption<E extends AirEntity> {
* @param submitData 实体
*/
beforeSubmit?: (submitData: E) => E | void

/**
* # 查到详情后的事件
* ---
* 💡 参数为响应的数据,请处理后返回
*
* @param detailData 实体
*/
afterGetDetail?: (detailData: E) => E | void

}
20 changes: 2 additions & 18 deletions interface/IUseEditorResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ import { Ref } from 'vue'
import { AirFormInstance } from '../type/AirType'
import { IValidateRule } from './IValidateRule'
import { AirEntity } from '../base/AirEntity'
import { IUseDetailResult } from './IUseDetailResult'

/**
* # Editor的Hook标准返回
*/
export interface IUseEditorResult<E extends AirEntity> {
/**
* # Editor显示的标题
*/
title: Ref<string>,

export interface IUseEditorResult<E extends AirEntity> extends IUseDetailResult<E>{
/**
* # 表单的Ref对象
* ---
Expand All @@ -28,18 +24,6 @@ export interface IUseEditorResult<E extends AirEntity> {
*/
rules: IValidateRule,

/**
* # 表单数据
*/
formData: Ref<E>,

/**
* # 当前绑定的Loading状态
* ---
* 💡 请随意 ```v-loading``` 到你需要的地方
*/
isLoading: Ref<boolean>,

/**
* # 表单提交的方法
* ---
Expand Down

0 comments on commit 4213d7f

Please sign in to comment.