hooks.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {getNoticeInfo, semiManufacturesAdd} from '@apis';
  2. import {yupResolver} from '@hookform/resolvers/yup';
  3. import {useQueryDataInfo} from '@hooks';
  4. import {NoticeListData} from '@models';
  5. import {userStore} from '@stores';
  6. import {useMutation, useQueryClient} from '@tanstack/react-query';
  7. import {message} from 'antd';
  8. import {useEffect} from 'react';
  9. import {useForm, useFormContext} from 'react-hook-form';
  10. import {number, object} from 'yup';
  11. import {useStore} from 'zustand';
  12. type FormState = {
  13. putInStoragenum: number,
  14. };
  15. const validate = object({
  16. putInStoragenum: number().required('请输入入库数量')
  17. .typeError('请输入数字').min(1, '不能少于1个'),
  18. });
  19. function useQuery(onClose: () => void, onFetch: () => void) {
  20. return useMutation({
  21. mutationFn: semiManufacturesAdd,
  22. onSuccess({msg}) {
  23. if (msg === '200') {
  24. onClose();
  25. onFetch();
  26. message.success('入库成功');
  27. }
  28. },
  29. });
  30. }
  31. function useInfoData(id: string) {
  32. const client = useQueryClient();
  33. function getData() {
  34. return client.getQueryData<NoticeListData>([getNoticeInfo.name, id]);
  35. }
  36. return getData;
  37. }
  38. export function useFormState(
  39. {id, visible, onClose, onFetch}:
  40. {id: string, visible: boolean, onClose: () => void, onFetch: () => void},
  41. ) {
  42. const formInstance = useForm<FormState>({
  43. defaultValues: {
  44. putInStoragenum: 0,
  45. },
  46. resolver: yupResolver(validate),
  47. });
  48. const {control, handleSubmit, clearErrors} = formInstance;
  49. useEffect(function() {
  50. if (visible)
  51. clearErrors();
  52. }, [clearErrors, visible]);
  53. const {mutate, isLoading} = useQuery(onClose, onFetch);
  54. const getInfoData = useInfoData(id);
  55. const userId = useStore(userStore, state => String(state.id));
  56. const onSubmit = handleSubmit(function({putInStoragenum}) {
  57. const info = getInfoData();
  58. if (!info)
  59. message.error('未获取到信息');
  60. else {
  61. const {wllbClass, companyNumber, wbs, materialId, noticeId} = info;
  62. mutate({
  63. userId,
  64. companyNumber,
  65. wbs,
  66. wllbClass,
  67. noticeId,
  68. materialId,
  69. warehousingNum: String(putInStoragenum),
  70. });
  71. }
  72. });
  73. return [{control, isLoading, formInstance}, {onSubmit}] as const;
  74. }
  75. export function useControl() {
  76. const {control} = useFormContext<FormState>();
  77. return control;
  78. }
  79. export function useWatchId(id: string) {
  80. const {setValue} = useFormContext<FormState>();
  81. const data = useQueryDataInfo({
  82. queryFn: getNoticeInfo,
  83. params: [id],
  84. enabled: Boolean(id),
  85. });
  86. useEffect(function() {
  87. setValue('putInStoragenum', Number(data?.num ?? '0'));
  88. }, [data, setValue]);
  89. }