hooks.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {addStorage, editStorage, getStorageInfo} from '@apis';
  2. import {yupResolver} from '@hookform/resolvers/yup';
  3. import {useQueryDataInfo} from '@hooks';
  4. import {AddStorageParams} from '@models';
  5. import {useMutation} from '@tanstack/react-query';
  6. import {message} from 'antd';
  7. import {useEffect} from 'react';
  8. import {useForm, useFormContext} from 'react-hook-form';
  9. import {number, object, string} from 'yup';
  10. type FormState = {
  11. /** 库位编号 */
  12. storageLocationCode: string
  13. /** 库位名称 */
  14. storageLocationName: string
  15. /** 库位类型 */
  16. storageLocationType: string
  17. /** 是否禁用 */
  18. storageIsNotDisable: string
  19. /** 所在仓库 */
  20. storageWarehouseWhere: string
  21. /** 库位容量 */
  22. storageCapacity: number
  23. };
  24. const validate = object({
  25. storageLocationCode: string().required('请输入库位编号'),
  26. storageLocationName: string().required('请输入库位名称'),
  27. storageLocationType: string().required('请输入库位类型'),
  28. storageWarehouseWhere: string().required('请输入所在仓库'),
  29. storageIsNotDisable: string().required('请选择状态'),
  30. storageCapacity: number().typeError('请输入数字类型').min(0, '库位容量必须大于0')
  31. .required('请输入库位容量'),
  32. });
  33. function useAdd(onClose: () => void, onFetch: () => void) {
  34. const {isLoading, mutate} = useMutation(
  35. addStorage,
  36. {
  37. onSuccess({msg}) {
  38. if (msg === '200') {
  39. message.success('新增成功');
  40. onClose();
  41. onFetch();
  42. }
  43. },
  44. },
  45. );
  46. return [isLoading, mutate] as const;
  47. }
  48. function useEdit(onClose: () => void, onFetch: () => void) {
  49. const {isLoading, mutate} = useMutation(
  50. editStorage,
  51. {
  52. onSuccess({msg}) {
  53. if (msg === '200') {
  54. message.success('修改成功');
  55. onClose();
  56. onFetch();
  57. }
  58. },
  59. },
  60. );
  61. return [isLoading, mutate] as const;
  62. }
  63. export function useFormState(
  64. {onClose, onFetch, id, visible}:
  65. {onClose: () => void, onFetch: () => void, id: string, visible: boolean},
  66. ) {
  67. const formInstance = useForm<FormState>({
  68. defaultValues: {
  69. storageLocationCode: '',
  70. storageLocationName: '',
  71. storageLocationType: '',
  72. storageWarehouseWhere: '',
  73. storageIsNotDisable: '1',
  74. storageCapacity: 0,
  75. },
  76. resolver: yupResolver(validate),
  77. });
  78. const {handleSubmit, clearErrors} = formInstance;
  79. useEffect(function() {
  80. visible && clearErrors();
  81. }, [clearErrors, visible]);
  82. const [isAddLoading, addMutate] = useAdd(onClose, onFetch);
  83. const [isEditLoading, editMutate] = useEdit(onClose, onFetch);
  84. const isLoading = isAddLoading || isEditLoading;
  85. const onSubmit = handleSubmit(function({
  86. storageLocationName,
  87. storageLocationCode,
  88. storageLocationType,
  89. storageIsNotDisable,
  90. storageWarehouseWhere,
  91. storageCapacity,
  92. }) {
  93. const params: AddStorageParams = {
  94. storageLocationName,
  95. storageLocationCode,
  96. storageLocationType,
  97. isNotDisable: storageIsNotDisable,
  98. warehouseWhere: storageWarehouseWhere,
  99. storageLocationCapacity: storageCapacity,
  100. };
  101. id
  102. ? editMutate({...params, id})
  103. : addMutate(params);
  104. });
  105. return [{formInstance, isLoading}, {onSubmit}] as const;
  106. }
  107. export function useFormInstance() {
  108. const {control} = useFormContext<FormState>();
  109. return control;
  110. }
  111. export function useFormInfoValue(id: string) {
  112. const {setValue} = useFormContext<FormState>();
  113. const data = useQueryDataInfo({
  114. queryFn: getStorageInfo,
  115. params: [id],
  116. enabled: Boolean(id),
  117. });
  118. useEffect(function() {
  119. setValue('storageLocationCode', data?.storageLocationCode ?? '');
  120. setValue('storageLocationName', data?.storageLocationName ?? '');
  121. setValue('storageLocationType', data?.storageLocationType ?? '');
  122. setValue('storageIsNotDisable', data?.isNotDisable ?? '1');
  123. setValue('storageWarehouseWhere', data?.warehouseWhere ?? '');
  124. setValue('storageCapacity', Number(data?.storageLocationCapacity ?? 0));
  125. }, [data, setValue]);
  126. }